Spaces:
Sleeping
A newer version of the Gradio SDK is available: 6.26.0
Hugging Face Spaces Deployment Guide
This guide walks you through deploying the RAG system to Hugging Face Spaces for free, permanent hosting.
Prerequisites
- Hugging Face account (sign up here)
- Git installed on your local machine
- Code migrated to use HF Inference API (already done β )
Step 1: Test Locally (Recommended)
Before deploying, test the application with HF Inference API locally:
1.1 Get Your HF Token
- Go to https://huggingface.co/settings/tokens
- Click "New token"
- Name it (e.g., "RAG System")
- Select
readpermissions - Click "Generate"
- Copy the token (starts with
hf_)
1.2 Set Environment Variable
# Linux/Mac
export HF_TOKEN=hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Windows (PowerShell)
$env:HF_TOKEN="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
1.3 Install Dependencies
cd /Users/v.hirenko/Desktop/DevHubVault/my-ai-projects/rag-python-rag
source venv/bin/activate # or venv\Scripts\activate on Windows
pip install -r requirements.txt
1.4 Run Tests
python test_hf_integration.py
If all tests pass, proceed to deployment!
1.5 Test the Application
python app.py
Open http://localhost:7860 and test with a few questions.
Step 2: Create Hugging Face Space
2.1 Create New Space
- Go to https://huggingface.co/new-space
- Fill in the details:
- Owner: Your username (e.g.,
monsara) - Space name:
rag-python-rag - License: MIT
- Select the Space SDK: Gradio
- Space hardware: CPU basic (free)
- Space visibility: Public (or Private if you prefer)
- Owner: Your username (e.g.,
- Click "Create Space"
2.2 Note Your Space URL
Your Space will be available at:
https://huggingface.co/spaces/YOUR_USERNAME/rag-python-rag
Step 3: Configure Space Secrets
3.1 Add HF_TOKEN Secret
- Go to your Space page
- Click on "Settings" (gear icon)
- Scroll down to "Repository secrets"
- Click "Add a secret"
- Fill in:
- Name:
HF_TOKEN - Value: Your HF token from Step 1.1
- Name:
- Click "Add"
β οΈ Important: The Space will NOT work without this secret!
Step 4: Push Code to Space
You have two options:
Option A: Direct Git Push (Recommended)
cd /Users/v.hirenko/Desktop/DevHubVault/my-ai-projects/rag-python-rag
# Add HF Space as remote
git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/rag-python-rag
# Replace YOUR_USERNAME with your actual HF username
# Push to HF Space
git push hf main
If prompted for credentials:
- Username: Your HF username
- Password: Your HF token (the same one you created)
Option B: Link GitHub Repository
- In your Space Settings
- Find "Linked repositories"
- Click "Link a GitHub repository"
- Select
monsara/rag-python-rag - The Space will automatically sync with your GitHub repo
Step 5: Verify Deployment
5.1 Check Build Logs
- Go to your Space page
- Click on "Logs" tab
- Watch the build process
- Look for:
β HF_TOKEN validated successfully β RAG pipeline setup complete! Running on local URL: http://0.0.0.0:7860
5.2 Test the Space
- Once "Running" status appears
- Click on the Space URL
- Try example questions
- Verify answers are generated correctly
Step 6: Update README (Optional)
Replace the Space README with the HF-specific one:
cd /Users/v.hirenko/Desktop/DevHubVault/my-ai-projects/rag-python-rag
# Backup current README
mv README.md README_LOCAL.md
# Use HF README
cp README_HF.md README.md
# Commit and push
git add README.md
git commit -m "Update README for HF Spaces"
git push hf main
Troubleshooting
Issue: "HF_TOKEN not found"
Solution: Make sure you added HF_TOKEN to Space secrets (Step 3.1)
Issue: "Model not found" or "Rate limit exceeded"
Solution:
- Check if you're using the correct model name in
config.py - Free tier has rate limits (~1000 requests/hour)
- Consider upgrading to HF Pro ($9/month)
Issue: "Build failed"
Solution:
- Check the build logs for specific errors
- Verify
requirements.txthas all dependencies - Make sure Python version is compatible (3.9+)
Issue: "Application crashes on startup"
Solution:
- Check if
app.pyis set as the entry point in Space settings - Verify all imports are correct
- Check logs for Python errors
Issue: Slow responses
Solution:
- Free tier uses shared infrastructure
- Upgrade to better hardware (paid)
- Or optimize chunk size and retrieval count
Monitoring
View Logs
# Real-time logs
# Go to Space page β Logs tab
Check Usage
- Go to your HF profile
- Click on "Usage & billing"
- View API usage statistics
Updating the Space
Update Code
cd /Users/v.hirenko/Desktop/DevHubVault/my-ai-projects/rag-python-rag
# Make your changes
git add .
git commit -m "Your update message"
git push hf main
The Space will automatically rebuild.
Update Dependencies
Edit requirements.txt, then:
git add requirements.txt
git commit -m "Update dependencies"
git push hf main
Update Configuration
Edit config.py, then push changes as above.
Cost Considerations
Free Tier (Current Setup)
- β Cost: $0/month
- β Hosting: Unlimited
- β οΈ Rate limits: ~1000 requests/hour
- β οΈ Performance: Shared CPU
- β οΈ Tokens: 1024 max per response
Upgrade Options
HF Pro ($9/month):
- Higher rate limits
- Faster inference
- Priority support
- Better hardware options
Dedicated Hardware:
- CPU Upgrade: $0.03/hour (~$22/month)
- GPU T4: $0.60/hour (~$432/month)
- GPU A10G: $1.05/hour (~$756/month)
Security Best Practices
- Never commit HF_TOKEN to Git
- Use Space secrets for all sensitive data
- Rotate tokens periodically
- Monitor usage for unexpected spikes
- Set rate limits in your application
Next Steps
After successful deployment:
- β Test thoroughly with various questions
- β Share the Space URL with users
- β Monitor logs for errors
- β Gather user feedback
- β Iterate and improve
Support
If you need help:
- Check HF Spaces documentation
- Visit HF Community forums
- Open an issue on GitHub
Quick Reference
Space URL: https://huggingface.co/spaces/YOUR_USERNAME/rag-python-rag
Settings: https://huggingface.co/spaces/YOUR_USERNAME/rag-python-rag/settings
Logs: https://huggingface.co/spaces/YOUR_USERNAME/rag-python-rag/logs
Push command:
git push hf main
Update secret: Space Settings β Repository secrets β Edit
Good luck with your deployment! π