Architecture Overview
The application consists of two main components:- A frontend interface running on CPU instances using FastAPI and Gradio.
- A separate Llama model endpoint running on GPU instances. (For a comprehensive example for deploying Llama 8B with TensorRT here.)
- 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 Outerscope account (sign up here).
- The Outerscope CLI installed:
pip install --upgrade Outerscope. - A Llama model endpoint (or other LLM API endpoint).
Basic Setup
First, create a new directory for your project and initialize it:Outerscope.toml:
- 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
/healthfor 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.
main.py). Start by creating the FastAPI application:
- 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.
main.py:
GradioServer: handles communication with the Llama model endpointchat_with_llama: sends a message to the Llama model and returns the responserun_server: creates a Gradio chat interfacestart: starts the Gradio server in a separate processstop: stops the Gradio serveron_eventstartup/shutdown: starts and stops the Gradio server respectively
main.py file: