open-notebook / reports /DEPLOYMENT_READY.md
baveshraam's picture
FIX: SurrealDB 2.0 migration syntax and Frontend/CORS link
f871fed

Hugging Face Spaces Deployment - Ready to Deploy! βœ…

Files Created & Verified

βœ… 1. Dockerfile

Status: Ready βœ“

Key features:

  • βœ… Base image: python:3.11-slim
  • βœ… Installed: curl and build-essential
  • βœ… SurrealDB installed via official script
  • βœ… HF_HOME=/tmp set for Hugging Face Spaces compatibility
  • βœ… TRANSFORMERS_CACHE=/tmp for model cache
  • βœ… SENTENCE_TRANSFORMERS_HOME=/tmp for embeddings
  • βœ… Pre-downloads all-MiniLM-L6-v2 model during build
  • βœ… Copies all required directories: api/, open_notebook/, commands/, migrations/, prompts/
  • βœ… Exposes port 7860 (required for HF Spaces)
  • βœ… Environment variables configured for SurrealDB and API

βœ… 2. start.sh

Status: Ready βœ“

Startup sequence:

  1. βœ… Starts SurrealDB with surreal start --log debug --user root --pass root memory &
  2. βœ… Waits 5 seconds for initialization: sleep 5
  3. βœ… Validates SurrealDB is running
  4. βœ… Sets environment variables for API (port 7860, host 0.0.0.0, reload false)
  5. βœ… Launches FastAPI app with python run_api.py

βœ… 3. open_notebook/database/connection.py

Status: Ready βœ“

Features:

  • βœ… 5 retry attempts with exponential backoff
  • βœ… Connects to ws://localhost:8000/rpc
  • βœ… Proper error handling and logging
  • βœ… Works with containerized SurrealDB

βœ… 4. requirements.txt

Status: Ready βœ“

All dependencies extracted from pyproject.toml


Deployment Checklist

Pre-Deployment

  • Dockerfile created with HF_HOME=/tmp
  • start.sh updated with correct startup sequence
  • Connection retry logic implemented
  • All files in git repository

Deploy to Hugging Face Spaces

Step 1: Create Space

  1. Go to https://huggingface.co/new-space
  2. Fill in:
    • Name: open-notebook (or your choice)
    • License: MIT
    • SDK: Docker ⚠️ Important!
    • Hardware: CPU Basic (Free tier)

Step 2: Push Code

# Add Hugging Face remote
git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/open-notebook

# Push to Hugging Face (will trigger build)
git push hf main

Step 3: Configure Secrets

In Space Settings β†’ Repository Secrets, add:

Required (at least one):

OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
GOOGLE_API_KEY=AIza...

Optional:

GROQ_API_KEY=gsk_...
MISTRAL_API_KEY=...
AUTH_PASSWORD=your_secure_password

Step 4: Wait for Build

  • Build takes ~10-15 minutes
  • Check build logs for errors
  • Once "Running", your Space is live!

Accessing Your Deployed App

Once deployed, access at:

https://YOUR_USERNAME-open-notebook.hf.space

API Endpoints

Documentation:

https://YOUR_USERNAME-open-notebook.hf.space/docs

Health Check:

curl https://YOUR_USERNAME-open-notebook.hf.space/health

Create Notebook:

curl -X POST https://YOUR_USERNAME-open-notebook.hf.space/api/notebooks \
  -H "Content-Type: application/json" \
  -d '{"name": "My Research", "description": "First notebook"}'

Important Notes

πŸ”΄ Data Persistence

  • In-Memory Storage: Data is lost on restart
  • Why: Using surreal start ... memory &
  • Solution: For persistent data, use external SurrealDB and update SURREAL_URL

πŸ’‘ Performance Tips

  1. Use Free AI Models:

    • Google Gemini (60 req/min free)
    • OpenAI GPT-3.5 (cheapest)
  2. Optimize Requests:

    • Smaller chunk sizes for embeddings
    • Cache frequent queries
    • Limit concurrent uploads
  3. Monitor Usage:

    • Check HF Spaces logs
    • Watch API rate limits
    • Track token usage

⚠️ Troubleshooting

Build Fails?

  • Check logs in HF Spaces dashboard
  • Verify all COPY paths exist in repo
  • Ensure pyproject.toml is valid

"Connection Refused" Error?

  • SurrealDB didn't start β†’ check logs
  • Increase sleep time in start.sh to 10 seconds
  • Verify port 8000 accessible internally

Model Download Fails?

  • Ensure HF_HOME=/tmp is set
  • Check internet during build
  • Verify sentence-transformers version

API Not Responding?

  • Check port 7860 is exposed
  • Verify run_api.py uses correct port
  • Check FastAPI logs in Space

Testing Locally

Before deploying, test locally:

# Build Docker image
docker build -t open-notebook-test .

# Run container
docker run -p 7860:7860 \
  -e OPENAI_API_KEY=your_key \
  open-notebook-test

# Test in browser
open http://localhost:7860/docs

Next Steps After Deployment

1. Create Frontend (Optional)

Deploy the Next.js frontend separately:

  • Create new Space with Node.js SDK
  • Set NEXT_PUBLIC_API_URL=https://YOUR-backend-SPACE.hf.space
  • Push frontend code

2. Set Up Persistent Storage

For production use:

# Deploy SurrealDB on Railway/Fly.io
# Update Space secrets:
SURREAL_URL=wss://your-db-instance.com/rpc
SURREAL_USER=your_user
SURREAL_PASS=your_password

3. Enable Authentication

Set password in Space secrets:

AUTH_PASSWORD=secure_password_here

4. Monitor and Scale

  • Watch HF Spaces metrics
  • Upgrade to HF Pro ($9/month) for:
    • No cold starts
    • Persistent storage
    • More CPU/RAM
    • Custom domains

Files Summary

c:\Bavesh\Sem6\SE\
β”œβ”€β”€ Dockerfile                          βœ… Ready (port 7860, HF_HOME=/tmp)
β”œβ”€β”€ start.sh                            βœ… Ready (SurrealDB + sleep 5 + run_api.py)
β”œβ”€β”€ requirements.txt                    βœ… Ready (all dependencies)
β”œβ”€β”€ run_api.py                          βœ… Existing (launches on port from env)
β”œβ”€β”€ open_notebook/
β”‚   └── database/
β”‚       └── connection.py               βœ… Ready (retry logic)
β”œβ”€β”€ api/                                βœ… Existing
β”œβ”€β”€ commands/                           βœ… Existing
β”œβ”€β”€ migrations/                         βœ… Existing
β”œβ”€β”€ prompts/                            βœ… Existing
└── pyproject.toml                      βœ… Existing

Support


πŸŽ‰ You're Ready to Deploy!

Your files are configured correctly. Just push to Hugging Face and watch it build!

git add .
git commit -m "Configure for Hugging Face Spaces deployment"
git push hf main

Good luck! πŸš€