A newer version of the Gradio SDK is available: 6.14.0
Deploying to HuggingFace Spaces
This guide walks you through deploying the HPMOR Q&A chatbot to HuggingFace Spaces.
Prerequisites
- HuggingFace account (create at https://huggingface.co)
- Groq API key (get free at https://console.groq.com)
- HuggingFace CLI installed and authenticated (
hf auth login)
Quick Deployment Steps
1. Create a HuggingFace Space
cd hpmor_qa
# Create a new Space
hf space create chat-with-harry --sdk gradio --space_sdk_version 5.49.1
Or manually create it on the HuggingFace website:
- Go to https://huggingface.co/new-space
- Choose a name (e.g., "chat-with-harry")
- Select SDK: Gradio
- Select SDK version: 5.49.1
- Choose Public or Private
2. Prepare Files for Deployment
The following files are needed for HuggingFace Spaces:
app.py- Main application file β (created)requirements_hf.txt- Python dependencies β (created, rename torequirements.txt)README_HF.md- Space README with metadata β (created, rename toREADME.md)src/- Source code directory β (existing)data/- Data directory β (existing).env- Environment variables (will be set as Space secrets)
3. Set Up Space Secrets
Add your Groq API key as a secret in your Space:
Option A: Via Web Interface
- Go to your Space settings
- Navigate to "Repository secrets"
- Add secret:
- Name:
GROQ_API_KEY - Value:
your_actual_groq_api_key
- Name:
Option B: Via CLI
# Set the GROQ_API_KEY secret
hf space secret set GROQ_API_KEY your_actual_groq_api_key --space-id YOUR_USERNAME/chat-with-harry
4. Deploy to HuggingFace
cd hpmor_qa
# Initialize git repo if not already done
git init
git remote add space https://huggingface.co/spaces/YOUR_USERNAME/chat-with-harry
# Copy and rename files for deployment
cp README_HF.md README.md
cp requirements_hf.txt requirements.txt
# Add files
git add app.py README.md requirements.txt src/ data/ .gitignore
# Commit
git commit -m "Initial deployment: HPMOR Q&A chatbot"
# Push to HuggingFace
git push --force space main
5. Alternative: Automated Deployment Script
Save this as deploy.sh and run it:
#!/bin/bash
set -e
# Configuration
SPACE_NAME="chat-with-harry"
HF_USERNAME="YOUR_USERNAME" # Change this!
echo "π Deploying HPMOR Q&A to HuggingFace Spaces..."
# Check if logged in
if ! hf whoami &> /dev/null; then
echo "β Not logged in to HuggingFace. Run: hf auth login"
exit 1
fi
# Create Space if it doesn't exist
echo "π¦ Creating HuggingFace Space..."
hf space create $SPACE_NAME --sdk gradio --space_sdk_version 5.49.1 || echo "Space may already exist"
# Copy files for deployment
echo "π Preparing deployment files..."
cp README_HF.md README.md
cp requirements_hf.txt requirements.txt
# Initialize git if needed
if [ ! -d .git ]; then
git init
fi
# Add HuggingFace remote
git remote remove space 2>/dev/null || true
git remote add space https://huggingface.co/spaces/${HF_USERNAME}/${SPACE_NAME}
# Stage files
echo "π¦ Staging files..."
git add app.py README.md requirements.txt src/ data/ .gitignore
# Commit
git commit -m "Deploy: HPMOR Q&A chatbot" || echo "Nothing new to commit"
# Push
echo "π Pushing to HuggingFace Spaces..."
git push --force space main
echo "β
Deployment complete!"
echo "π Your Space: https://huggingface.co/spaces/${HF_USERNAME}/${SPACE_NAME}"
echo ""
echo "β οΈ Don't forget to set GROQ_API_KEY in Space secrets!"
Make it executable and run:
chmod +x deploy.sh
./deploy.sh
Post-Deployment Checklist
- Space is building successfully
- GROQ_API_KEY secret is set
- Data files are present (check Space files tab)
- App loads without errors
- Can ask questions and get responses
- Sources are displayed correctly
Troubleshooting
Build Failures
Problem: Dependencies fail to install
- Check
requirements.txtfor version conflicts - Try removing version pins
- Check Space build logs
Problem: Out of memory during build
- Reduce chunk size in config
- Use smaller embedding model
- Request GPU Space (Settings β Hardware)
Runtime Errors
Problem: "GROQ_API_KEY not found"
- Set the secret in Space settings
- Restart the Space
Problem: "No processed documents found"
- Ensure
data/raw/hpmor.htmlis included - Check file paths in app.py
- The app should auto-setup on first run
Problem: Slow responses
- Upgrade to GPU Space (Settings β Hardware)
- Reduce TOP_K_RETRIEVAL
- Consider using smaller model
File Structure for Deployment
hpmor_qa/
βββ app.py # Main Gradio app (HF Spaces entry point)
βββ README.md # Space description with metadata
βββ requirements.txt # Python dependencies
βββ .gitignore # Git ignore rules
βββ src/
β βββ __init__.py
β βββ config.py # Configuration
β βββ document_processor.py
β βββ vector_store.py
β βββ model_chain.py # Uses Groq only
β βββ rag_engine.py
β βββ harry_personality.py
βββ data/
βββ raw/
βββ hpmor.html # Source text (ensure this is included!)
Important Notes
- No Ollama: The cloud version only uses Groq API (no local models)
- Groq API Key: Must be set as a Space secret
- Data Files: Ensure
data/raw/hpmor.htmlis committed to the repo - First Run: The app will auto-process documents on first launch
- Persistent Storage: ChromaDB will persist in Space storage
Updating Your Space
To update your deployed Space:
# Make changes to your code
git add .
git commit -m "Update: description of changes"
git push space main
Custom Domain (Optional)
HuggingFace provides a default URL, but you can:
- Use HF's subdomain:
your-username-chat-with-harry.hf.space - Request a custom domain (Pro feature)
Monitoring
- View logs: Space interface β "Logs" tab
- Check status: Space interface β "Settings" tab
- Usage stats: HuggingFace dashboard
Getting Help
If you encounter issues:
- Check Space build logs
- Review HuggingFace Spaces documentation: https://huggingface.co/docs/hub/spaces
- Check Gradio documentation: https://gradio.app/docs/
- Open an issue on the project repository
Cost Considerations
Free Tier:
- CPU Spaces are free
- 2 CPU cores, 16GB RAM
- May be slow for large queries
Upgraded Hardware (Paid):
- GPU Spaces for faster inference
- More RAM for larger embeddings
- Pricing: https://huggingface.co/pricing
Next Steps
After deployment:
- Test the chatbot thoroughly
- Share your Space link!
- Gather feedback and iterate
- Consider adding analytics
- Add more features (streaming responses, etc.)
Enjoy your deployed HPMOR chatbot! π§ββοΈ