Deploying to Hugging Face Spaces
This guide explains how to deploy the FastAPI backend to Hugging Face Spaces using Docker.
Prerequisites
- A Hugging Face account
- Git installed on your machine
- Docker installed (for local testing)
Steps to Deploy
Create a new Space on Hugging Face:
- Go to https://huggingface.co/spaces
- Click "Create new Space"
- Select "Docker" as the SDK
- Choose a name for your space
- Set visibility (public/private)
Clone the new Space repository:
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
Copy the following files to the cloned repository:
Dockerfile
requirements.txt
app/
directorytemplates/
directoryrun.py
tabble_new.db
Update your environment variables:
- Go to your Space's Settings
- Add any necessary environment variables
- Make sure to add any sensitive information as secrets
Commit and push your changes:
git add .
git commit -m "Initial commit"
git push
Local Testing
Before deploying to Hugging Face, you can test locally using Docker Compose:
- Build and run the container:
docker-compose up --build
- Access the API at
http://localhost:7860
Important Notes
- The Dockerfile uses port 7860 as required by Hugging Face Spaces
- Make sure your database is properly initialized before deployment
- Static files and images should be stored in the appropriate directories
- Any changes to the code require a new commit and push to update the Space
Monitoring
- Monitor your Space's logs through the Hugging Face interface
- Check the Space's metrics for usage statistics
- Use the Space's terminal for debugging if needed
Troubleshooting
If you encounter issues:
- Check the Space's logs for error messages
- Verify all environment variables are set correctly
- Ensure all required files are present in the repository
- Test the application locally using Docker Compose before deploying