Spaces:
Sleeping
Sleeping
A newer version of the Gradio SDK is available:
6.1.0
Deploying IntegraChat to Hugging Face Spaces
This guide walks you through deploying IntegraChat as a Hugging Face Space.
π Prerequisites
- Hugging Face Account: Sign up at huggingface.co
- Hugging Face Token: Get your token from Settings β Access Tokens
- Required Services (configure via environment variables):
- PostgreSQL with pgvector (for RAG storage)
- Ollama (local LLM) or Groq API (cloud LLM)
- Optional: Supabase (for production storage)
- Optional: Google Custom Search API (for web search)
π Step-by-Step Deployment
Step 1: Create a New Space
- Go to https://huggingface.co/new-space
- Fill in the form:
- Space name:
integrachat(or your preferred name) - SDK: Select Docker
- Hardware: Choose based on your needs:
- CPU basic - For testing (free tier)
- CPU upgrade - Better performance (paid)
- GPU - If you need GPU acceleration (paid)
- Visibility: Public or Private
- Space name:
- Click Create Space
Step 2: Prepare Your Repository
Your project structure should look like this:
IntegraChat/
βββ Dockerfile β
(created)
βββ .dockerignore β
(created)
βββ README_HF_SPACES.md β
(created)
βββ requirements.txt β
(already exists)
βββ app.py β
(already exists)
βββ env.example β
(already exists)
βββ LICENSE β
(already exists)
βββ README.md β
(already exists)
βββ assets/
β βββ banner.png β
(already exists)
βββ backend/ β
(entire directory)
βββ scripts/ β
(if you have any)
Step 3: Push to Hugging Face
Option A: Using Git (Recommended)
# Initialize git if not already done
git init
# Add Hugging Face remote
git remote add hf https://huggingface.co/spaces/<your-username>/<space-name>
# Add all files (except venv)
git add Dockerfile .dockerignore README_HF_SPACES.md requirements.txt app.py env.example LICENSE README.md assets/ backend/ scripts/
# Commit
git commit -m "Initial commit for HF Spaces deployment"
# Push to Hugging Face
git push hf main
Option B: Using Hugging Face Web Interface
- Go to your Space page
- Click Files and versions tab
- Click Upload files
- Upload all necessary files (drag and drop or select files)
Step 4: Configure Environment Variables
- Go to your Space page
- Click Settings tab
- Scroll to Repository secrets section
- Add the following environment variables:
Required Variables
POSTGRESQL_URL=postgresql://user:password@host:port/database
OLLAMA_URL=http://your-ollama-server:11434
OLLAMA_MODEL=llama3.1:latest
OR (if using Groq instead of Ollama):
GROQ_API_KEY=your_groq_api_key
LLM_BACKEND=groq
Optional Variables
# Supabase (for production storage)
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_SERVICE_KEY=your_service_role_key
# Google Custom Search (for web search)
GOOGLE_SEARCH_API_KEY=your_google_api_key
GOOGLE_SEARCH_CX_ID=your_search_engine_id
# MCP Server Configuration
MCP_PORT=8900
MCP_HOST=0.0.0.0
# API Configuration
API_PORT=8000
BACKEND_BASE_URL=http://localhost:8000
# Memory Configuration
MCP_MEMORY_MAX_ITEMS=10
MCP_MEMORY_TTL_SECONDS=900
# Logging
LOG_LEVEL=info
APP_ENV=production
Step 5: Wait for Build
- After pushing, Hugging Face will automatically start building your Docker image
- You can monitor the build progress in the Logs tab
- Build typically takes 5-10 minutes for the first time
- Once built, your Space will be available at:
https://huggingface.co/spaces/<your-username>/<space-name>
Step 6: Verify Deployment
- Check Logs: Go to the Logs tab to see if all services started correctly
- Test UI: Open your Space URL and verify the Gradio UI loads
- Test API: Try accessing
https://<your-space-url>/api/docsfor FastAPI docs - Test Health: Check
https://<your-space-url>/api/healthfor backend health
π§ Troubleshooting
Build Fails
- Check Dockerfile syntax: Ensure all commands are valid
- Check requirements.txt: Verify all packages are available on PyPI
- Check logs: Review build logs for specific errors
- Common issues:
- Missing system dependencies β Add to
apt-get installin Dockerfile - Python version mismatch β Update
FROM python:3.10-slimif needed - Port conflicts β Ensure ports 7860, 8000, 8900 are exposed
- Missing system dependencies β Add to
Services Not Starting
- Check environment variables: Ensure all required vars are set
- Check service logs: Review logs for MCP server and FastAPI errors
- Database connection: Verify PostgreSQL URL is correct and accessible
- LLM connection: Verify Ollama URL or Groq API key is valid
UI Not Loading
- Check Gradio logs: Look for errors in the Logs tab
- Check port binding: Ensure Gradio binds to
0.0.0.0:7860 - Check backend connection: Verify
BACKEND_BASE_URLis correct
API Endpoints Not Working
- Check FastAPI logs: Review backend startup logs
- Check MCP server: Ensure MCP server is running on port 8900
- Check CORS: Verify CORS middleware is configured correctly
- Check headers: Ensure
x-tenant-idandx-user-roleheaders are sent
π Important Notes
Port Configuration:
- Hugging Face Spaces automatically maps port 7860 to the public URL
- Internal services (FastAPI on 8000, MCP on 8900) are accessible within the container
- Use
localhostfor inter-service communication
Database Access:
- Your PostgreSQL database must be accessible from Hugging Face's servers
- Consider using a cloud database (Supabase, AWS RDS, etc.)
- Ensure firewall rules allow connections from Hugging Face IPs
LLM Access:
- If using Ollama, it must be accessible from Hugging Face servers
- Consider using Groq API for cloud-based LLM access
- Or use Hugging Face's Inference API
Resource Limits:
- Free tier has CPU and memory limits
- Consider upgrading for production use
- Monitor resource usage in the Space settings
Secrets Management:
- Never commit
.envfiles with secrets - Use Hugging Face Space secrets for sensitive data
- Use
env.exampleas a template
- Never commit
π― Next Steps
- Customize README: Update
README_HF_SPACES.mdwith your specific details - Add Banner: Upload a banner image to
assets/banner.png - Test Thoroughly: Test all features in the deployed environment
- Monitor Usage: Check Space analytics for usage patterns
- Update Documentation: Keep documentation in sync with deployment
π Additional Resources
β Checklist
Before deploying, ensure:
- Dockerfile is present and valid
- .dockerignore excludes venv and .env
- requirements.txt includes all dependencies
- Environment variables are documented
- Database is accessible from Hugging Face servers
- LLM service is configured (Ollama or Groq)
- README_HF_SPACES.md is customized
- All code is committed and pushed
Need Help? Check the Troubleshooting section or open an issue in the repository.