Hugging Face Deployment Guide
This guide will help you deploy the Smart Budget Recommendation API to Hugging Face Spaces.
Prerequisites
- A Hugging Face account (sign up at https://huggingface.co)
- MongoDB connection string ready
- Git installed (optional, you can also use the web interface)
Step-by-Step Deployment
Step 1: Create a New Space
- Go to https://huggingface.co/spaces
- Click "Create new Space"
- Fill in the details:
- Space name:
smart-budget-recommendation(or your preferred name) - SDK: Select Docker
- Visibility: Choose Public or Private
- Space name:
- Click "Create Space"
Step 2: Add secrets (MongoDB and optional OpenAI)
- In your Space, go to Settings (gear icon)
- Navigate to Variables and secrets section
- Click New secret
- Add the following secrets:
- Name:
MONGODB_URI- Value:
mongodb://expenseuser:Kem_6o%3F%[email protected]:27017/expense?authSource=admin
- Value:
- Name:
OPENAI_API_KEY(optional, enables GPT-based recommendations)- Value:
sk-...
- Value:
- Name:
- Click Add secret after each entry
Step 3: Upload Files
You can upload files in two ways:
Option A: Using Git (Recommended)
- Clone your Space repository:
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
cd YOUR_SPACE_NAME
Copy all files from this project:
app/directory (all Python files)Dockerfilerequirements.txt.dockerignoreREADME.md(optional)
Commit and push:
git add .
git commit -m "Initial deployment"
git push
Option B: Using Web Interface
- In your Space, click Files and versions tab
- Click Add file → Upload files
- Upload all files:
- All files from
app/directory Dockerfilerequirements.txt.dockerignore
- All files from
Step 4: Wait for Build
- Hugging Face will automatically detect the
Dockerfileand start building - You can monitor the build progress in the Logs tab
- The build typically takes 2-5 minutes
Step 5: Verify Deployment
Once the build completes:
- Your API will be available at:
https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space - Test the health endpoint:
https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/health - View API documentation:
https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/docs
Testing Your Deployment
Using cURL
# Health check
curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/health
# Get recommendations (replace user123 with actual user_id)
curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/recommendations/user123
Using Python
import requests
BASE_URL = "https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space"
# Health check
response = requests.get(f"{BASE_URL}/health")
print(response.json())
# Get recommendations
response = requests.get(f"{BASE_URL}/recommendations/user123")
print(response.json())
Important Notes
Port Configuration: Hugging Face Spaces expect apps to serve on port 7860, so the Dockerfile exposes and binds to that port.
Environment Variables: Secrets such as
MONGODB_URI(and optionalOPENAI_API_KEY) are injected automatically into the container environment.Cold Starts: The first request after inactivity may take longer (cold start). Subsequent requests will be faster
Resource Limits: Free Hugging Face Spaces have resource limits. For production use, consider upgrading
Custom Domain: You can add a custom domain in Space settings if needed
Troubleshooting
Build Fails
- Check the Logs tab for error messages
- Ensure
Dockerfileis in the root directory - Verify all required files are uploaded
Connection Errors
- Verify
MONGODB_URIsecret is set correctly - Check MongoDB server is accessible from Hugging Face's servers
- Review connection string encoding (special characters should be URL-encoded)
API Not Responding
- Check the Logs tab for runtime errors
- Verify the application starts successfully
- Test the
/healthendpoint first
Updating Your Deployment
- Make changes to your code locally
- Commit and push to your Space repository
- Hugging Face will automatically rebuild and redeploy
Support
For issues specific to:
- Hugging Face Spaces: Check Hugging Face Documentation
- This Application: Check the main README.md file