### Install Modal and ipywidgets Source: https://github.com/modal-labs/modal-examples/blob/main/11_notebooks/basic.ipynb Installs or upgrades the Modal library and installs ipywidgets for notebook interactivity. ```python %pip install --upgrade modal %pip install ipywidgets ``` -------------------------------- ### Run Modal Example with Test Runner Source: https://github.com/modal-labs/modal-examples/blob/main/internal/CLAUDE.md Use this command to run a specific example using the internal test runner. Replace `` with the filename of the example without the .py extension. ```bash MODAL_ENVIRONMENT=examples python -m internal.run_example ``` -------------------------------- ### Install Modal Dependencies Source: https://github.com/modal-labs/modal-examples/blob/main/06_gpu_and_ml/embeddings/wikipedia/README.md Install the necessary packages for using Modal, including the Modal library itself. It's recommended to use a virtual environment. ```bash python3 -m venv venv source venv/bin/activate pip3 install modal ``` -------------------------------- ### Example Frontmatter for Deploying a Web App Source: https://github.com/modal-labs/modal-examples/blob/main/internal/readme.md This YAML frontmatter configures deployment and testing for a Modal application. It specifies deployment as true, uses 'modal serve' for testing to avoid production deployment, and sets a timeout for the serve command in testing environments. ```yaml --- deploy: true cmd: ["modal", "serve", "10_integrations/pushgateway.py"] --- ``` -------------------------------- ### Import Modal and Check Version Source: https://github.com/modal-labs/modal-examples/blob/main/11_notebooks/basic.ipynb Imports the Modal library and asserts that the installed version is greater than 0.49.0. ```python import modal assert modal.__version__ > "0.49.0" modal.__version__ ``` -------------------------------- ### Literate Programming Style in Modal Examples Source: https://github.com/modal-labs/modal-examples/blob/main/internal/CLAUDE.md Examples use a literate programming style where prose is written as markdown comments on their own lines. Ensure prose is updated when code changes affect its description. ```python # # This is a Title # # This is a paragraph of prose that becomes documentation. # It continues on multiple lines. import modal # This comment stays with the code # ## This is a Section Header # # More prose explaining the next code block. app = modal.App("example-name") ``` -------------------------------- ### App Naming Convention Source: https://github.com/modal-labs/modal-examples/blob/main/internal/CLAUDE.md Use the 'example-' prefix followed by kebab-case for naming Modal applications to maintain consistency. ```python app = modal.App("example-vllm-inference") ``` -------------------------------- ### Use Descriptive Volume Names and Create If Missing Source: https://github.com/modal-labs/modal-examples/blob/main/internal/CLAUDE.md Employ descriptive kebab-case names for volumes, such as 'huggingface-cache' or 'vllm-cache'. Always use `create_if_missing=True` for user convenience on the first run. ```python cache_vol = modal.Volume.from_name("huggingface-cache", create_if_missing=True) ``` -------------------------------- ### Linting and Formatting Commands Source: https://github.com/modal-labs/modal-examples/blob/main/internal/CLAUDE.md Run these commands to check and format your code before committing changes. Ensure your virtual environment is activated. ```bash source venv/bin/activate ruff check --fix ruff format ``` -------------------------------- ### Configure Hugging Face Downloads for Performance Source: https://github.com/modal-labs/modal-examples/blob/main/internal/CLAUDE.md Use the high-performance download flag and pin the huggingface-hub version for faster downloads. Set the HF_XET_HIGH_PERFORMANCE environment variable to '1'. ```python .uv_pip_install("huggingface-hub==0.36.0") .env({"HF_XET_HIGH_PERFORMANCE": "1"}) # faster downloads ``` -------------------------------- ### Define and Run a GPU-Accelerated Modal Function Source: https://github.com/modal-labs/modal-examples/blob/main/11_notebooks/basic.ipynb Defines a Modal function `hello_gpu` configured to run on a GPU. It executes `nvidia-smi` to show GPU details and returns a confirmation string. ```python # Define a Modal function with a GPU attached. @app.function(gpu="any") def hello_gpu(): import subprocess subprocess.run("nvidia-smi", shell=True, check=True) return "hello from a remote GPU!" # Start and run an ephemeral modal.App and execute the GPU-powered modal Function! with app.run(): result = hello_gpu.remote() assert result == "hello from a remote GPU!" # After the app is finished you can continue executing other function's defined in your notebook and # use the results of your GPU functions! "This is the remote GPU's return value: " + result ``` -------------------------------- ### Define a Modal App Source: https://github.com/modal-labs/modal-examples/blob/main/11_notebooks/basic.ipynb Initializes a Modal App instance with a specific name. ```python app = modal.App(name="example-basic") ``` -------------------------------- ### Use Fully-Qualified Modal Names Source: https://github.com/modal-labs/modal-examples/blob/main/internal/CLAUDE.md Always use `modal.X` instead of importing names directly to avoid potential naming conflicts and improve clarity. ```python # ✅ Good import modal image = modal.Image.debian_slim() vol = modal.Volume.from_name("my-vol", create_if_missing=True) # ❌ Bad from modal import Image, Volume image = Image.debian_slim() ``` -------------------------------- ### Deploying the Code Agent Application Source: https://github.com/modal-labs/modal-examples/blob/main/13_sandboxes/codelangchain/README.md Deploy the LangChain code agent application using the Modal CLI. This command initiates the deployment process for the web application defined in `codelangchain.py`. ```bash modal deploy codelangchain.py ``` -------------------------------- ### Disable NVIDIA License Logging Source: https://github.com/modal-labs/modal-examples/blob/main/internal/CLAUDE.md Run `.entrypoint([])` on NVIDIA container images to disable the verbose NVIDIA license logging, ensuring cleaner output. ```python modal.Image.from_registry("nvidia/cuda:12.8.0-devel-ubuntu22.04", add_python="3.12") .entrypoint([]) # disable noisy NVIDIA license logging ``` -------------------------------- ### HTML Structure for WebRTC YOLO Demo Source: https://github.com/modal-labs/modal-examples/blob/main/07_web/webrtc/frontend/index.html Sets up the basic HTML elements for video display, controls, and status updates for the WebRTC YOLO demo. Includes styling for video elements, buttons, and status display. ```html
``` -------------------------------- ### Run Wikipedia Download Script Source: https://github.com/modal-labs/modal-examples/blob/main/06_gpu_and_ml/embeddings/wikipedia/README.md Execute the `download.py` script using the Modal CLI to download the Wikipedia dataset into a Modal volume. This script leverages Modal's high internet speeds for efficient downloads. ```bash modal run download.py ``` -------------------------------- ### Run Whisper Model Fine-tuning Source: https://github.com/modal-labs/modal-examples/blob/main/06_gpu_and_ml/openai_whisper/finetuning/readme.md Execute the fine-tuning process for the Whisper model. Adjust the number of training epochs to potentially improve performance and decrease word error rate. ```bash modal run -m train.train --num_train_epochs=10 ``` -------------------------------- ### List Top-Level Files in Modal Volume Source: https://github.com/modal-labs/modal-examples/blob/main/06_gpu_and_ml/embeddings/wikipedia/README.md Verify that the Wikipedia dataset has been downloaded by listing the top-level files in the 'embedding-wikipedia' volume using the Modal CLI. ```bash modal volume ls embedding-wikipedia ``` -------------------------------- ### Running the Agent in Isolation Source: https://github.com/modal-labs/modal-examples/blob/main/13_sandboxes/codelangchain/README.md Test the LangChain agent in isolation by running the `agent.py` script. Provide a question about Python programming as input to the agent. ```bash modal run agent.py --question "What is a list comprehension?" ``` -------------------------------- ### Define and Run Modal Functions Source: https://github.com/modal-labs/modal-examples/blob/main/11_notebooks/basic.ipynb Defines Modal functions `double_with_modal` and `quadruple`. `quadruple` demonstrates calling both local and remote Modal functions, with conditional execution based on input size. ```python @app.function() def double_with_modal(x: int) -> int: return x + x @app.function() def quadruple(x: int) -> int: if x <= 1_000_000: return double(x) + double(x) else: return double_with_modal.remote(x) + double_with_modal.remote(x) with app.run(): print(quadruple.local(100)) # running locally print(quadruple.remote(100)) # run remotely print("Doing a very inefficient remote multiplication just for fun!") result = quadruple.remote(10_000_000) ``` -------------------------------- ### End-to-End Check for Whisper Fine-tuning Source: https://github.com/modal-labs/modal-examples/blob/main/06_gpu_and_ml/openai_whisper/finetuning/readme.md Perform a comprehensive test of the fine-tuning program, including training, serialization, saving, loading, and prediction. This check ensures the program's functionality within a short timeframe. ```bash modal run -m train.end_to_end_check ``` -------------------------------- ### Serving the Code Agent Application with Hot-Reloading Source: https://github.com/modal-labs/modal-examples/blob/main/13_sandboxes/codelangchain/README.md Serve the LangChain code agent application locally with hot-reloading enabled. This is useful for development, allowing changes to be reflected without manual restarts. ```bash modal serve codelangchain.py ``` -------------------------------- ### Run Wikipedia Embedding Script Source: https://github.com/modal-labs/modal-examples/blob/main/06_gpu_and_ml/embeddings/wikipedia/README.md Execute the `main.py` script using the Modal CLI to embed the downloaded Wikipedia dataset. This script utilizes Modal's parallelization abstractions and two volumes for reading and writing. ```bash modal run main.py ``` -------------------------------- ### Pinning Container Image Dependencies Source: https://github.com/modal-labs/modal-examples/blob/main/internal/CLAUDE.md Follow these rules for pinning dependencies in container images. Pin to SemVer minor versions, patch versions for packages < 1.0, and stable tags for base images. Always specify python_version when supported. ```python image = ( modal.Image.from_registry("nvidia/cuda:12.8.0-devel-ubuntu22.04", add_python="3.12") .uv_pip_install( "vllm==0.13.0", "huggingface-hub==0.36.0", ) ) ``` -------------------------------- ### Run a Modal Python Script Source: https://github.com/modal-labs/modal-examples/blob/main/README.md Use this command to run Modal Python scripts locally. The script will execute in Modal's cloud environment. ```bash modal run 01_getting_started/hello_world.py ``` -------------------------------- ### List Files in Specific Directory of Modal Volume Source: https://github.com/modal-labs/modal-examples/blob/main/06_gpu_and_ml/embeddings/wikipedia/README.md View the contents of the '/wikipedia' directory within the 'embedding-wikipedia' volume using the Modal CLI to confirm the dataset structure. ```bash modal volume ls embedding-wikipedia /wikipedia ``` -------------------------------- ### JavaScript State Management for LLM Engine Frontend Source: https://github.com/modal-labs/modal-examples/blob/main/06_gpu_and_ml/llm-frontend/index.html Manages the state of the LLM engine frontend, including prompts, results, and API interaction logic. Use this for handling user input and displaying streaming completions. ```javascript function state() { return { nextPrompt: "", items: [], info: { backlog: 0, num_total_runners: 0 }, callApi() { console.log(this.nextPrompt); if (!this.nextPrompt) return; let item = { id: Math.random(), prompt: this.nextPrompt, completion: "", loading: true, markdownCompletion: "", }; this.nextPrompt = ""; this.items.push(item); const eventSource = new EventSource( `/completion/${encodeURIComponent(item.prompt)}`, ); console.log("Created event source ..."); eventSource.onmessage = (event) => { item.completion += JSON.parse(event.data).text; item.markdownCompletion = marked.parse(item.completion, { mangle: false, headerIds: false, }); // Hacky way to notify element to update this.items = this.items.map((i) => i.id === item.id ? { ...item } : i, ); }; eventSource.onerror = (event) => { eventSource.close(); item.loading = false; this.items = this.items.map((i) => i.id === item.id ? { ...item } : i, ); console.log(item.completion); }; }, refreshInfo() { fetch("/stats") .then((response) => response.json()) .then((data) => { this.info = { ...data, loaded: true }; }) .catch((error) => console.log(error)); }, }; } ``` -------------------------------- ### JavaScript State Management for Image-to-Video Source: https://github.com/modal-labs/modal-examples/blob/main/06_gpu_and_ml/image-to-video/frontend/index.html This JavaScript function manages the state for the image-to-video application. It handles user input for prompts, file uploads, and triggers the video generation process. Use this for client-side logic to interact with the backend inference endpoint. ```javascript function state() { return { prompt: "{{ default_prompt }}", submitted: "", loading: false, imageURL: "", videoURL: "", selectedFile: null, previewImage(event) { const file = event.target.files[0]; if (file) { this.selectedFile = file; this.imageURL = URL.createObjectURL(file); this.videoURL = ""; } }, async submitPrompt() { if (!this.prompt || !this.selectedFile) return; this.submitted = this.prompt; this.loading = true; try { const formData = new FormData(); formData.append("image_bytes", this.selectedFile); url = `{{ inference_url }}?prompt=${this.prompt}`; const res = await fetch(url, { method: "POST", headers: { accept: "application/json", }, body: formData, }); if (!res.ok) { throw new Error("Inference failed"); } const blob = await res.blob(); this.videoURL = URL.createObjectURL(blob); this.imageURL = ""; } catch (error) { console.error("Fetch failed:", error); alert("There was an error generating the video."); } finally { this.loading = false; } }, }; } ``` -------------------------------- ### Define and Call a Standard Python Function Source: https://github.com/modal-labs/modal-examples/blob/main/11_notebooks/basic.ipynb Defines a simple Python function `double` and calls it locally. This function can be used independently of Modal. ```python def double(x: int) -> int: return x + x double(5) ``` -------------------------------- ### Use Blank Lines for Visual Separation Source: https://github.com/modal-labs/modal-examples/blob/main/internal/CLAUDE.md Add a blank line before code blocks to match the visual separation in rendered documentation, improving readability. ```python # ✅ Good # We define an image with our dependencies. image = modal.Image.debian_slim().pip_install("torch") # ❌ Bad # We define an image with our dependencies. image = modal.Image.debian_slim().pip_install("torch") ``` -------------------------------- ### Configure Tailwind CSS Source: https://github.com/modal-labs/modal-examples/blob/main/09_job_queues/doc_ocr_frontend/index.html Configure Tailwind CSS theme extensions for custom colors and fonts. Ensure JavaScript is enabled for the app to function. ```javascript tailwind.config = { theme: { extend: { colors: { modal: { primary: "#4F46E5", secondary: "#818CF8", }, }, fontFamily: { inter: ["Inter", "sans-serif"], arial: ["Arial", "sans-serif"], }, }, }, }; ``` -------------------------------- ### JavaScript State Management for Image Generation Source: https://github.com/modal-labs/modal-examples/blob/main/06_gpu_and_ml/stable_diffusion/frontend/index.html Manages the state for prompt submission, loading, and image display. Use this to handle user input and display results from the Stable Diffusion model. ```javascript function state() { return { prompt: "{{ default_prompt }}", submitted: "", loading: false, imageURL: "", async submitPrompt() { if (!this.prompt) return; this.submitted = this.prompt; this.loading = true; try { const res = await fetch( `{{ inference_url }}?prompt=${this.submitted}`, ); if (!res.ok) { throw new Error("Inference failed"); } const blob = await res.blob(); this.imageURL = URL.createObjectURL(blob); } catch (error) { console.error("Fetch failed:", error); alert("There was an error generating the image."); } finally { this.loading = false; console.log(this.imageURL); } }, }; } ``` -------------------------------- ### JavaScript Session Management and Polling Source: https://github.com/modal-labs/modal-examples/blob/main/06_gpu_and_ml/world-models/frontend/index.html This JavaScript code manages the state for a world generation job. It handles submitting a prompt, polling the backend for session status, and applying updated assets like video URLs. Use this for initiating and tracking generation tasks. ```javascript function state() { const cfg = JSON.parse(document.body.dataset.config); return { prompt: cfg.default_prompt, loading: false, sessionId: null, status: "", error: "", sourceURL: "", worldURL: "", get statusLabel() { const labels = { running_ltx: "Stage 1/2: LTX reference video…", running_inspatio: "Stage 2/2: InSpatio world generation…", done: "Done", }; return labels[this.status] || this.status || "Working…"; }, async submitJob() { if (!this.prompt) return; this.loading = true; this.error = ""; this.sourceURL = ""; this.worldURL = ""; this.sessionId = null; try { const formData = new FormData(); formData.append("prompt", this.prompt); const res = await fetch("/api/sessions", { method: "POST", body: formData, }); if (!res.ok) throw new Error("Failed to start session"); const data = await res.json(); this.sessionId = data.session_id; this.status = "running_ltx"; await this.pollSession(); } catch (err) { console.error(err); this.error = "There was an error starting the session."; this.loading = false; } }, async pollSession() { if (!this.sessionId) return; try { const res = await fetch(`/api/sessions/${this.sessionId}`); if (!res.ok) { setTimeout(() => this.pollSession(), 10000); return; } const state = await res.json(); this.status = state.status || ""; this.applyAssets(state.assets || {}); if (state.status === "done") { this.loading = false; return; } } catch (err) { console.warn("poll error", err); } setTimeout(() => this.pollSession(), 10000); }, applyAssets(assets) { if (assets.source_video) this.sourceURL = assets.source_video; if (assets.world_video) this.worldURL = assets.world_video; }, retryVideo(el) { const maxRetries = 10; const base = (el.getAttribute("src") || "").split("?")[0]; if (!base) return; const n = (parseInt(el.dataset.retries || "0", 10) || 0) + 1; if (n > maxRetries) return; el.dataset.retries = n; setTimeout(() => { el.src = base + "?r=" + Date.now(); el.load(); }, 1500); }, }; } ``` -------------------------------- ### Define Time Constants for Readability Source: https://github.com/modal-labs/modal-examples/blob/main/internal/CLAUDE.md Define time constants like MINUTES = 60 (seconds) at the module level to make timeout values more readable in function definitions. ```python MINUTES = 60 # seconds @app.function(timeout=10 * MINUTES) ``` -------------------------------- ### Reference Paths Relative to Script Location Source: https://github.com/modal-labs/modal-examples/blob/main/internal/CLAUDE.md Use `Path(__file__).parent` to construct paths relative to the current script's directory. This ensures correct path resolution regardless of where the script is executed. ```python from pathlib import Path here = Path(__file__).parent input_path = here / "data" / "config.yaml" ``` -------------------------------- ### CSS Styling for WebRTC YOLO Demo Source: https://github.com/modal-labs/modal-examples/blob/main/07_web/webrtc/frontend/index.html Provides CSS rules for layout and appearance of the WebRTC YOLO demo interface. Styles video elements, buttons, radio button groups, and the status display area. ```css video { width: 320px; height: 240px; margin: 10px; border: 1px solid black; } button { margin: 10px; padding: 10px; } #videos { display: flex; flex-wrap: wrap; } .radio-group { margin: 10px; padding: 10px; border: 1px solid #ccc; border-radius: 4px; } .radio-group label { margin-right: 15px; } #statusDisplay { margin: 10px; padding: 10px; border: 1px solid #ccc; border-radius: 4px; background-color: #f5f5f5; min-height: 20px; max-height: 150px; overflow-y: auto; font-family: monospace; white-space: pre-wrap; word-wrap: break-word; } .status-line { margin: 2px 0; padding: 2px; border-bottom: 1px solid #eee; } ``` -------------------------------- ### Capitalize Modal Product Features Source: https://github.com/modal-labs/modal-examples/blob/main/internal/CLAUDE.md Modal's features are proper nouns and should be capitalized. Use monospace only when referring to the actual Python object. ```python # ✅ "Modal Volumes provide distributed storage." # ✅ "`modal.Volume` has a `from_name` method." # ❌ "Modal volumes provide distributed storage." ``` -------------------------------- ### Pin Hugging Face Model Revisions Source: https://github.com/modal-labs/modal-examples/blob/main/internal/CLAUDE.md Always pin model revisions using a specific commit hash to prevent unexpected changes when upstream repositories are updated. ```python MODEL_NAME = "Qwen/Qwen3-4B-Thinking-2507-FP8" MODEL_REVISION = "953532f942706930ec4bb870569932ef63038fdf" # pin to avoid surprises! ``` -------------------------------- ### Remove Directory Recursively from Modal Volume Source: https://github.com/modal-labs/modal-examples/blob/main/06_gpu_and_ml/embeddings/wikipedia/README.md Remove the '/wikipedia' directory and its contents from the 'embedding-wikipedia' volume using the Modal CLI. The `--recursive` flag is required for directory removal. ```bash modal volume rm embedding-wikipedia /wikipedia --recursive ``` -------------------------------- ### Evaluate Remote Function Result Source: https://github.com/modal-labs/modal-examples/blob/main/11_notebooks/basic.ipynb Evaluates the `result` variable, which holds the output from the previously executed remote Modal function call. ```python # Evaluate the result created in above cell result ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.