Spaces:
Configuration error
Configuration error
Deploying Backend on Docker for Hugging Face Spaces
This guide provides step-by-step instructions for deploying your RAG Chatbot backend to Hugging Face Spaces using Docker.
Prerequisites
- Hugging Face account
- Access to a Qdrant vector database (hosted or self-hosted)
- API keys for your LLM provider (e.g., OpenAI)
- Git and Docker installed on your local machine
Step 1: Prepare Your Hugging Face Repository
- Go to Hugging Face Spaces
- Click "Create New Space"
- Fill in the details:
- Name: Choose a name for your space (e.g.,
your-username/physical-ai-chatbot-backend) - License: Select an appropriate license
- SDK: Choose "Docker"
- Hardware: Select the hardware specifications based on your needs (CPU is usually sufficient for a demo, GPU if you're running inference locally)
- Spot Instance: Optional, for cost savings (interruptible hardware)
- Name: Choose a name for your space (e.g.,
- Click "Create Space"
Step 2: Clone Your Space Repository
- Copy the git clone URL for your new space
- Clone the repository to your local machine:
git clone [your-space-git-url]
cd [your-space-directory]
Step 3: Add Backend Files to Your Space Repository
In your cloned space directory, copy all the files from the backend directory of your project:
# Copy backend files to the space directory
cp -r /path/to/your/HackthoneI_Book_ChatBot/backend/* ./
Your space directory should now have the following key files:
Dockerfile- Defines the Docker imageapi.py- Main Flask applicationrequirements.txt- Python dependenciesapp.py- Hugging Face Spaces entry point- Other backend files
Step 4: Configure Environment Variables
Hugging Face Spaces allows you to set secrets for environment variables:
- In your space repository on Hugging Face, go to the "Files" tab
- Click "Settings" (or "Edit Settings" if visible)
- In the "Secrets" section, add the following environment variables:
QDRANT_URL- Your Qdrant database URLQDRANT_API_KEY- Your Qdrant API keyOPENAI_API_KEY- Your OpenAI API key (or your chosen LLM provider's key)OPENAI_BASE_URL- Base URL for OpenAI API (if applicable, otherwise leave empty)EMBEDDING_MODEL- The embedding model you're using (default: text-embedding-ada-002)
⚠️ Important Security Note: Never commit actual API keys to the repository. Always use Hugging Face Secrets for sensitive information.
Step 5: Commit and Push Your Changes
git add .
git commit -m "Add backend files for RAG Chatbot"
git push origin main
Step 6: Monitor the Build Process
- After pushing, Hugging Face will automatically build and deploy your Docker container
- You can monitor the build progress in the "Build logs" tab of your Space
- The first build may take 5-15 minutes depending on the size of your dependencies
- Once the build is complete, your backend will be accessible at
https://[your-username]-[space-name].hf.space
Step 7: Test Your Deployment
- Once deployed, verify that your backend is running by checking:
- Health endpoint:
https://[your-username]-[space-name].hf.space/health - If you see
{"status": "healthy"}, your backend is running correctly
- Health endpoint:
Step 8: Connect Frontend (Optional)
If you have a frontend application, update it to use the Hugging Face Space URL as the backend endpoint instead of localhost.
Troubleshooting
Common Issues and Solutions
Build Fails:
- Check the build logs for specific error messages
- Ensure all dependencies in requirements.txt are valid
- Make sure your Dockerfile is properly formatted
Container Crashes:
- Check the runtime logs in the "Logs" tab
- Verify all required environment variables are set in Secrets
- Check that your Qdrant instance is accessible
API Keys Not Working:
- Verify that secrets are correctly set in the Hugging Face Space settings
- Ensure environment variables are referenced correctly in your application code
High Resource Usage:
- Consider upgrading hardware for your Space if needed
- Optimize embedding model size if possible
- Implement caching to reduce API calls
Optimizing for Production
For production use, consider these improvements:
- Add Authentication: Implement authentication for your API endpoints
- Rate Limiting: Add rate limiting to prevent abuse
- Caching: Implement caching for frequent queries
- Health Monitoring: Add more comprehensive health checks
- Logging: Implement structured logging for easier debugging
Updating Your Deployment
To update your backend:
- Make changes to your local files
- Commit and push the changes:
git add . git commit -m "Describe your changes" git push origin main - Hugging Face will automatically rebuild and redeploy your container
Cost Considerations
- Free tier: Limited compute hours per month
- Paid tier: For more intensive usage or guaranteed uptime
- Compute costs vary based on hardware selection
Security Best Practices
- Never commit API keys or other secrets to the repository
- Use Hugging Face Secrets for all sensitive information
- Implement proper authentication for your endpoints in production
- Regularly rotate your API keys
- Monitor API usage to detect potential abuse