Deploy FastAPI Backend to Hugging Face Spaces
This Skill teaches how to configure, package, and deploy a FastAPI backend using:
- •FastAPI framework
- •SQLAlchemy models connected to a Neon PostgreSQL database
- •Uvicorn as the ASGI server
- •MCP SDK Python
- •Docker for deployment on Hugging Face Spaces
Instructions
- •
Set up the repository. Ensure the root contains:
- •
Dockerfile - •
requirements.txt - •FastAPI code (application folder)
- •database and MCP initialization files
- •
- •
Define environment variables. Store secrets (e.g.,
DATABASE_URL,MCP_SDK_KEY) in Hugging Face Spaces Secrets. - •
Use the Dockerfile template. See dockerfile-template.txt for a working example.
- •
Configure SQLAlchemy for Neon. Use SSL and correct connection pooling patterns.
- •
Expose Uvicorn on port 7860. Required by Hugging Face Spaces for deployment.
- •
Test locally in Docker. Validate build and exposed endpoints.
- •
Push and deploy the Space. Hugging Face will auto-build and serve the FastAPI app.
Detailed Reference
For deeper configuration details and pitfalls, see reference.md.
Examples
Sample Dockerfile, environment settings, and command usage are provided in examples.md.