Modal Examples

repository·main·Indexed 23 days ago

https://github.com/modal-labs/modal-examples

A collection of practical examples for building robust and scalable applications on Modal's serverless platform. The repository includes a guided tour of core concepts (folders 01-14) and miscellaneous examples, all tested against Python 3.11. Featured implementations include fine-tuning OpenAI's Whisper model, deploying LangChain/LangGraph code agents in sandboxes, running Hugging Face Text Embeddings Inference (TEI) servers on GPUs, and creating OpenAI-compatible LLM serving clients.

Tokens
6.9K
Snippets
21
Records
34
Agent score
79%

What's inside modal-examples

  1. Understand the Code Agent repository structure

    main

    The project is organized into several modules that separate the web interface, agent logic, and graph structure:

    • codelangchain.py: The main web application definition.
    • agent.py: Contains the core LangChain agent definition.
    • nodes.py: Defines the nodes within the LangGraph agent graph.
    • edges.py: Defines the edges (control flow) within the LangGraph agent graph.
    • retrieval.py: Contains the logic for retrieving documentation to include in the model's prompt.
    • common.py: Contains Modal container image definitions and shared utilities.
  2. Explore Modal examples by category

    main

    The repository is organized into functional categories to help you learn Modal's capabilities:

    • Guided Tour: Folders 01_getting_started/ through 14_clusters/ provide a progressive learning path through Modal's core concepts.
    • Miscellaneous: The misc/ folder contains uncategorized examples.

    Note: All examples are continuously tested for correctness against Python 3.11.

  3. Understand the Miscellaneous Examples directory

    main

    The misc/ directory contains a variety of examples demonstrating different ways to use Modal.

    Note: Unlike other examples in the modal-examples repository, the examples in this directory are not continually monitored for correctness. They may become out of date or incorrect over time as the Modal API evolves.

  4. Verify downloaded files in a Modal Volume

    main

    You can verify that the Wikipedia dataset was successfully saved to the embedding-wikipedia volume by listing its contents using the Modal CLI.

    To list the root of the volume:

    modal volume ls embedding-wikipedia

    To list the contents of the specific /wikipedia directory:

    modal volume ls embedding-wikipedia /wikipedia
    modal volume ls embedding-wikipedia
    modal volume ls embedding-wikipedia /wikipedia
  5. Fine-tune OpenAI's Whisper model for Hindi speech recognition

    main

    You can fine-tune the whisper-small model for improved automatic Hindi speech recognition using Modal. The provided configuration typically runs for approximately 3 hours and achieves a Word Error Rate (WER) of about 55-60. To improve performance and decrease the WER, you should increase the number of training epochs.

    Benchmark performance using the Huggingface autoevaluate leaderboard for the mozilla-foundation/common_voice_11_0 dataset.

    modal run -m train.train --num_train_epochs=10
  6. Embed the Wikipedia dataset

    main

    Once the dataset is downloaded, use main.py to run the embedding job. This script utilizes Modal's built-in parallelization abstractions to process the dataset.

    Note: The embedding process uses two separate Modal volumes: one for reading the source dataset and another for writing the resulting files for upload.

    Run the embedding script using:

    modal run main.py
    modal run main.py
  7. Get started with Modal examples

    main

    To use these examples, first sign up for a free account at modal.com and follow the official setup instructions to install the modal package and configure your API key.

    Once set up, you can run any example script using the modal run command. While you execute the command from your local machine, the code will communicate with Modal to spawn and run serverless containers in the cloud.

    modal run 01_getting_started/hello_world.py
  8. Setup the environment for Wikipedia embedding

    main

    To run this example, you need to set up a Python virtual environment and install the modal package. You must also authenticate your local environment with Modal.

    1. Create and activate a virtual environment:
      python3 -m venv venv
      source venv/bin/activate
    2. Install the Modal client:
      pip3 install modal
    3. Authenticate with Modal:
      modal token new
    python3 -m venv venv
    source venv/bin/activate
    pip3 install modal
    modal token new
  9. Remove files from a Modal Volume

    main

    To manage storage costs, you can remove files or directories from a Modal Volume using the modal volume rm command.

    Important: To remove a directory, you must include the --recursive flag.

    Example to remove the /wikipedia directory from the embedding-wikipedia volume:

    modal volume rm embedding-wikipedia /wikipedia --recursive
    modal volume rm embedding-wikipedia /wikipedia --recursive
  10. Download the Wikipedia dataset to a Modal Volume

    main

    The download.py script downloads the Wikipedia dataset directly into a Modal Volume. This leverages Modal's high internet speeds for rapid data ingestion. To optimize download times, the script uses the num_proc keyword to parallelize downloads; setting this value between 4 and 10 can reduce download time by 30-40%.

    Run the download script using:

    modal run download.py
    modal run download.py
  11. Deploy and run the Code Agent sandbox

    main

    This project demonstrates how to deploy a LangChain/LangGraph code agent as a web function using Modal. The agent can write and execute code within a sandboxed environment to prevent malicious or accidental damage to the host application.

    Prerequisites

    1. Install the Modal client:
      pip install modal
    2. Configure the following Modal Secrets:
      • openai-secret: Contains your OpenAI API key for model queries.
      • langsmith-secret: Contains your LangSmith API key for monitoring agent behavior.

    Deployment Commands

    • Deploy to production: Use this to expose the agent to the internet as a Web Function.

      modal deploy codelangchain.py

      After deployment, navigate to the provided URL to use the interactive playground or visit /docs for OpenAPI/Swagger documentation.

    • Local development: Use this to start a server with hot-reloading.

      modal serve codelangchain.py
    modal deploy codelangchain.py