Skip to main content
This tutorial covers creating and deploying a Gradio chat interface connected to a Llama 8B language model using Cerebrium’s custom ASGI runtime. The architecture runs the frontend on CPU instances while the model runs separately on GPU instances for optimal resource utilization. You can find the full codebase for deploying your Gradio frontend here.

Architecture Overview

The application consists of two main components:
  1. A frontend interface running on CPU instances using FastAPI and Gradio.
  2. A separate Llama model endpoint running on GPU instances. (For a comprehensive example for deploying Llama 8B with TensorRT here.)
This separation enables:
  • Keep the frontend always available while minimizing costs (CPU-only).
  • Scale the GPU-intensive model independently based on demand.
  • Optimize resource allocation for different components.

Prerequisites

Before starting, you’ll need:
  • A Cerebrium account (sign up here).
  • The Cerebrium CLI installed: pip install --upgrade cerebrium.
  • A Llama model endpoint (or other LLM API endpoint).

Basic Setup

First, create a new directory for your project and initialize it:
Add the following configuration to cerebrium.toml:
This configuration:
  • Disables default JWT authentication, making the Gradio interface publicly accessible.
  • Sets the ASGI server entrypoint to Uvicorn.
  • Sets the default port to 8080.
  • Sets the health endpoint to /health for availability checks.
  • Configures hardware settings for the CPU instance.
  • Defines scaling with min/max replicas, cooldown, and concurrency (10 requests per replica).
  • Specifies required dependencies: Gradio, FastAPI, Requests, HTTPX, Uvicorn, and Starlette.
Set up the main entrypoint file (main.py). Start by creating the FastAPI application:
The above code:
  • Initializes a FastAPI application that forwards requests to the Gradio app running as a subprocess on a different port.
  • Sets up a health check endpoint at /health.
  • Creates a catchall proxy that routes all requests to Gradio, including headers.
Next, set up the Gradio application. Add the following code to main.py:
The code above defines:
  • GradioServer: handles communication with the Llama model endpoint
  • chat_with_llama: sends a message to the Llama model and returns the response
  • run_server: creates a Gradio chat interface
  • start: starts the Gradio server in a separate process
  • stop: stops the Gradio server
  • on_event startup/shutdown: starts and stops the Gradio server respectively
The final main.py file:

Deploy

Deploy the app:
Once deployed, navigate to the following URL in your browser:
The Gradio chat interface appears at this URL.

Conclusion

This architecture provides a scalable chat app using the ASGI custom runtime. The frontend/backend separation improves performance and cost management while maintaining scaling flexibility. Share feedback, challenges, or Gradio apps in the Discord community.