Skip to content

Instantly share code, notes, and snippets.

@usametov
Created June 30, 2025 18:07
Show Gist options
  • Save usametov/405743806db429105550e64a901a7e85 to your computer and use it in GitHub Desktop.
Save usametov/405743806db429105550e64a901a7e85 to your computer and use it in GitHub Desktop.
run transformers with http server

https://grok.com/share/bGVnYWN5_b34d4dfb-2047-4266-87c7-12cb79e37f4a

The Hugging Face Transformers library now includes a built-in HTTP server, introduced via pull request #38443, enabling users to serve machine learning models over HTTP for inference tasks. Below is a concise tutorial on how to install and run the HTTP server, along with details on hardware requirements based on available information and general knowledge about running transformer models.


Tutorial: Installing and Running the Transformers HTTP Server

The HTTP server in the Transformers library allows you to serve models for inference via HTTP endpoints, making it easier to integrate models into applications. The following steps guide you through setting up and running the server.

Step 1: Prerequisites

  • Python: Ensure you have Python 3.9+ installed, as Transformers requires it.
  • Virtual Environment: It’s recommended to use a virtual environment to manage dependencies and avoid conflicts.
  • Dependencies: The server relies on additional packages like fastapi and uvicorn for HTTP functionality.

Step 2: Install Transformers with HTTP Server Support

Since the HTTP server is a recent addition (via PR #38443), you’ll need to install the Transformers library from the source to access the latest changes, as they may not yet be in a stable release.

  1. Create and Activate a Virtual Environment:

    python -m venv .my-env
    source .my-env/bin/activate  # On Windows: .my-env\Scripts\activate
  2. Clone the Transformers Repository:

    git clone https://github.com/huggingface/transformers.git
    cd transformers
  3. Install Transformers from Source: Install the library with the necessary dependencies for the HTTP server.

    pip install .[torch,http-server]
    • The [torch] extra ensures PyTorch is installed, which is required for most transformer models.
    • The [http-server] extra installs dependencies like fastapi and uvicorn for the HTTP server.

    Alternatively, if you want to use a specific preview release that includes the HTTP server, you can install a tagged version (check the latest tag in the repository if available):

    pip install git+https://github.com/huggingface/[email protected]
  4. Verify Installation: Ensure the installation was successful by checking the Transformers version:

    python -c "import transformers; print(transformers.__version__)"

Step 3: Running the HTTP Server

The HTTP server can be launched using a command-line interface provided by the Transformers library. You’ll need to specify the model you want to serve and configure the server settings.

  1. Basic Command to Start the Server: Run the following command to start the HTTP server with a model from the Hugging Face Hub (e.g., distilbert/distilgpt2 for text generation):

    python -m transformers.serve --model-id distilbert/distilgpt2 --task text-generation
    • --model-id: Specifies the model to serve (e.g., distilbert/distilgpt2).
    • --task: Specifies the task (e.g., text-generation, text-classification, question-answering, etc.).
    • Additional options (e.g., --port, --host) can be used to customize the server’s address (default is localhost:8000).
  2. Example with Custom Port: To run the server on a specific port (e.g., 8080):

    python -m transformers.serve --model-id distilbert/distilgpt2 --task text-generation --port 8080
  3. Test the Server: Once the server is running, it exposes HTTP endpoints (typically /generate for text generation or similar, depending on the task). You can test it using curl or a tool like Postman:

    curl -X POST http://localhost:8000/generate \
         -H "Content-Type: application/json" \
         -d '{"inputs": "Once upon a time"}'

    The response will contain the model’s output (e.g., generated text).

  4. Accessing the API Documentation: The server uses FastAPI, which provides an interactive API documentation page. Open your browser and navigate to:

    http://localhost:8000/docs
    

    This page lists available endpoints and allows you to test them interactively.

Step 4: Example with a Different Model

To serve a different model, such as bert-base-uncased for text classification:

python -m transformers.serve --model-id bert-base-uncased --task text-classification

Then, send a request:

curl -X POST http://localhost:8000/predict \
     -H "Content-Type: application/json" \
     -d '{"inputs": "This is a great movie!"}'

Step 5: Stopping the Server

To stop the server, press Ctrl+C in the terminal where it’s running.


Hardware Requirements

The hardware requirements for running the Transformers HTTP server depend on the model size, task, and whether you’re using a CPU or GPU. Below are general guidelines based on the Transformers library and typical model deployment needs.

Minimum Requirements

  • CPU: A modern multi-core CPU (e.g., 4+ cores, Intel i5/i7 or AMD Ryzen equivalent) is sufficient for small models like distilbert/distilgpt2 or bert-base-uncased.
  • RAM: At least 8 GB of RAM for small models. Larger models (e.g., meta-llama/Llama-2-7b) may require 16–32 GB or more.
  • Storage: 10–20 GB of free disk space for model weights, cached files, and dependencies. Models are downloaded from the Hugging Face Hub and cached locally (default: ~/.cache/huggingface/hub).
  • Operating System: Linux, macOS, or Windows (Python 3.9+ compatible).

GPU Requirements (Optional but Recommended)

  • GPU: An NVIDIA GPU with CUDA support (e.g., GTX 1060, RTX 3060, or higher) significantly speeds up inference for larger models or high-throughput scenarios.
  • VRAM:
    • Small models (e.g., DistilBERT, ~66M parameters): 2–4 GB VRAM.
    • Medium models (e.g., BERT-base, ~110M parameters): 4–8 GB VRAM.
    • Large models (e.g., Llama-7B, ~7B parameters): 12–24 GB VRAM for full precision, or 8–12 GB with quantization (e.g., 4-bit or 8-bit).
  • CUDA Drivers: Ensure CUDA and cuDNN are installed for PyTorch GPU support. Check for GPU availability:
    python -c "import torch; print(torch.cuda.is_available())"
    Install CUDA drivers if needed, following PyTorch’s installation guide.

Scaling Considerations

  • Model Size: Larger models (e.g., Llama-70B) require significantly more VRAM (40–80 GB) or multi-GPU setups. Use quantization (e.g., bitsandbytes library) to reduce memory usage.
  • Concurrent Requests: The HTTP server’s performance depends on the number of concurrent requests. For high traffic, a multi-core CPU or GPU with higher VRAM is beneficial.
  • Network: A stable internet connection is needed to download models from the Hugging Face Hub initially. For offline use, pre-download models using snapshot_download:
    from huggingface_hub import snapshot_download
    snapshot_download(repo_id="distilbert/distilgpt2", local_dir="./path/to/local/directory")
    Then, serve the model offline:
    python -m transformers.serve --model-id ./path/to/local/directory --task text-generation --local-files-only

Optimizations

  • Quantization: Use 4-bit or 8-bit quantization to reduce memory usage for large models (requires bitsandbytes or similar).
  • Mixed Precision: Enable mixed-precision inference (e.g., FP16) to lower memory and speed up inference on GPUs.
  • Batching: Configure the server to handle batch requests for better throughput, especially for text-generation tasks.

Additional Information About the HTTP Server

  • Purpose: The HTTP server simplifies deploying transformer models for inference, providing a RESTful API to interact with models without needing to write custom server code. It’s built on FastAPI and Uvicorn, ensuring high performance and asynchronous request handling.
  • Supported Tasks: The server supports various tasks, including text generation, text classification, question answering, and more, depending on the model.
  • Customization: You can extend the server by modifying the source code in the Transformers repository or adding custom endpoints for specific use cases.
  • Limitations: The HTTP server is a new feature (as of PR #38443) and may not be fully stable in early releases. Check the GitHub repository for updates or bug fixes.

For more details, refer to the official documentation at:

If you encounter issues, check the GitHub issues page or ask on the Hugging Face forum/Discord, as recommended in the contribution guidelines.


Next Steps

Let me know if you need help with a specific model, task, or advanced configuration!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment