Spaces:
Sleeping
Hugging Face Spaces Deployment Guide
This guide explains how to deploy Open Notebook to Hugging Face Spaces using the Docker SDK.
Files Created
Dockerfile.huggingface- Docker configuration for single-container deploymentstart.sh- Startup script that launches SurrealDB and FastAPIopen_notebook/database/connection.py- SurrealDB connection with retry logicrequirements.txt- Python dependencies extracted from pyproject.tomlREADME_HUGGINGFACE.md- Hugging Face Spaces README
Deployment Steps
1. Rename Dockerfile
# Rename the Hugging Face Dockerfile to the standard name
mv Dockerfile.huggingface Dockerfile
2. Create Hugging Face Space
- Go to huggingface.co/new-space
- Choose:
- Space name:
open-notebook(or your preferred name) - License: MIT
- SDK: Docker
- Visibility: Public or Private
- Space name:
3. Push Code to Hugging Face
# Add Hugging Face remote
git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/open-notebook
# Push to Hugging Face
git push hf main
4. Configure Secrets
In your Hugging Face Space settings, add these secrets:
Required (add at least one):
OPENAI_API_KEY- Your OpenAI API keyANTHROPIC_API_KEY- Your Anthropic (Claude) API keyGOOGLE_API_KEY- Your Google (Gemini) API key
Optional:
GROQ_API_KEY- Groq API keyMISTRAL_API_KEY- Mistral API key
5. Wait for Build
Hugging Face will automatically build your Docker container. This takes about 10-15 minutes.
Important Notes
Port Configuration
Hugging Face Spaces requires port 7860. The Dockerfile is configured to use this port.
In-Memory Storage
This deployment uses SurrealDB in memory mode (memory). This means:
- ✅ Fast performance
- ✅ No disk space issues
- ❌ Data is lost when container restarts
- ❌ Not suitable for production
For persistent storage, modify start.sh:
# Change from:
surreal start --log debug --user root --pass root memory &
# To:
surreal start --log debug --user root --pass root file://data/database.db &
Retry Logic
The connection module (open_notebook/database/connection.py) includes:
- 5 retry attempts with exponential backoff
- 2-second initial delay, increasing with each retry
- Ensures SurrealDB is ready before FastAPI starts accepting requests
Resource Limits
Hugging Face Spaces free tier:
- 2 CPU cores
- 16GB RAM
- 50GB disk (ephemeral)
The sentence-transformer model (all-MiniLM-L6-v2) is pre-downloaded during build to avoid startup delays.
Testing Your Deployment
Once deployed, test these endpoints:
# Health check
curl https://YOUR_USERNAME-open-notebook.hf.space/health
# API documentation
https://YOUR_USERNAME-open-notebook.hf.space/docs
# Create a notebook
curl -X POST https://YOUR_USERNAME-open-notebook.hf.space/api/notebooks \
-H "Content-Type: application/json" \
-d '{"name": "Test Notebook", "description": "My first notebook"}'
Troubleshooting
Build Fails
Check the build logs in Hugging Face. Common issues:
- Missing dependencies: Verify all packages in requirements.txt
- SurrealDB install fails: Check internet connectivity during build
- Out of memory: Reduce the size of pre-downloaded models
Runtime Errors
Check the runtime logs:
- "Connection refused": SurrealDB didn't start - increase wait time in start.sh
- "Database migration failed": Check SURREAL_* environment variables
- "Model not found": Ensure sentence-transformers model downloaded during build
Performance Issues
On free tier:
- Limit concurrent requests
- Use lighter LLM models (Gemini, GPT-3.5-turbo)
- Reduce chunk size for embeddings
- Enable caching for repeated queries
Upgrading to Persistent Storage
To use external SurrealDB with persistent storage:
- Deploy SurrealDB separately (Railway, Fly.io, etc.)
- Update environment variables in Hugging Face settings:
SURREAL_URL=wss://your-surrealdb-instance.com/rpc SURREAL_USER=your_username SURREAL_PASS=your_password - Remove SurrealDB startup from
start.sh:#!/bin/bash set -e echo "Starting FastAPI application on port 7860..." exec uvicorn api.main:app --host 0.0.0.0 --port 7860
Alternative Deployment: Split Services
For better performance, consider splitting frontend and backend:
Backend Space (this configuration):
- Python Docker SDK
- FastAPI + SurrealDB
- Port 7860
Frontend Space (separate):
- Node.js SDK or Static
- Next.js frontend
- Points to backend API
Cost Optimization
Free Tier Recommendations:
- Use Google Gemini (free tier: 60 requests/min)
- Pre-generate embeddings during low traffic
- Implement request queuing
- Cache LLM responses
Paid Tier Benefits ($9/month):
- No cold starts
- More CPU/RAM
- Persistent storage
- Custom domains
Security Considerations
- Never commit API keys - Use Hugging Face Secrets
- Enable authentication - Modify
api/auth.pyto add user login - Rate limiting - Add rate limits to prevent abuse
- CORS configuration - Restrict allowed origins in production
- Input validation - All file uploads should be validated
Support
- Discord: https://discord.gg/37XJPXfz2w
- GitHub Issues: https://github.com/baveshraam/software-eng-proj/issues
- Documentation: https://www.open-notebook.ai