Spaces:
Sleeping
Sleeping
A newer version of the Gradio SDK is available: 6.26.0
Migration Summary: Ollama β Hugging Face Spaces
Overview
Successfully migrated the RAG system from local Ollama to Hugging Face Inference API for free, permanent cloud hosting.
Changes Made
1. Core Files Modified
llm_handler.py β
- Before: Used
ollamalibrary for local LLM inference - After: Uses
huggingface_hub.InferenceClientfor cloud inference - Key changes:
- Replaced
ollama.ClientwithInferenceClient - Updated streaming logic for HF API
- Added token authentication
- Changed model to
meta-llama/Llama-3.2-3B-Instruct
- Replaced
config.py β
- Removed:
OLLAMA_MODEL = "llama3.2" OLLAMA_BASE_URL = "http://localhost:11434" - Added:
HF_MODEL = "meta-llama/Llama-3.2-3B-Instruct" HF_TOKEN = os.getenv("HF_TOKEN", "")
requirements.txt β
- Removed:
ollama==0.4.4langchain-ollama==0.2.2
- Added:
huggingface_hub==0.20.3
main.py β
- Added HF token validation on startup
- Updated error messages
- Removed Ollama-specific checks
2. New Files Created
app.py β
- Entry point for Hugging Face Spaces
- Validates HF_TOKEN from environment/secrets
- Provides clear setup instructions if token missing
- Launches Gradio interface with HF-specific settings
README_HF.md β
- Hugging Face Spaces README with YAML frontmatter
- Setup instructions for HF Spaces
- Usage examples
- Architecture diagram
- Rate limits and limitations
- Links to documentation
ENV_SETUP.md β
- Complete environment variables documentation
- Step-by-step token setup guide
- Examples for different platforms
- Verification commands
test_hf_integration.py β
- Comprehensive test suite
- Tests HF token, imports, API connection
- Tests LLM handler and vector store
- Provides clear pass/fail results
DEPLOYMENT_GUIDE.md β
- Step-by-step deployment instructions
- Local testing guide
- HF Spaces setup process
- Troubleshooting section
- Cost considerations
- Security best practices
3. Files Unchanged
document_converter.pyβ (no changes needed)text_splitter.pyβ (no changes needed)vector_store.pyβ (no changes needed)README.mdβ (kept for local development)- All documentation files β
Architecture Comparison
Before (Ollama)
User β Gradio β Vector Store β Ollama (Local) β Response
After (HF Spaces)
User β Gradio β Vector Store β HF Inference API (Cloud) β Response
Key Differences
| Aspect | Ollama (Before) | HF Spaces (After) |
|---|---|---|
| Hosting | Local only | Cloud (free) |
| LLM | llama3.2 (local) | Llama-3.2-3B-Instruct (cloud) |
| Setup | Install Ollama + model | Just HF token |
| Cost | Free (local compute) | Free (with rate limits) |
| Availability | Only when PC on | 24/7 |
| Rate Limits | None | ~1000 req/hour |
| Scalability | Limited by hardware | Managed by HF |
Testing Checklist
Before deploying to HF Spaces, run:
# 1. Set HF token
export HF_TOKEN=hf_your_token_here
# 2. Install dependencies
pip install -r requirements.txt
# 3. Run tests
python test_hf_integration.py
# 4. Test locally
python app.py
Deployment Steps
- β Get HF Token: https://huggingface.co/settings/tokens
- β
Test Locally: Run
python test_hf_integration.py - β³ Create Space: https://huggingface.co/new-space
- β³ Add Secret: Space Settings β Repository secrets β
HF_TOKEN - β³ Push Code:
git push hf main - β³ Verify: Check Space URL and test
Next Steps
Immediate
- Get Hugging Face token
- Run local tests
- Create HF Space
- Deploy and verify
Future Enhancements
- Add file upload functionality
- Support multiple documents
- Add conversation history
- Custom model selection
- Advanced filtering
- Export conversations
Resources
- HF Spaces Docs: https://huggingface.co/docs/hub/spaces
- HF Inference API: https://huggingface.co/docs/api-inference/
- Get Token: https://huggingface.co/settings/tokens
- Pricing: https://huggingface.co/pricing
Rollback Plan
If you need to revert to Ollama:
# Checkout previous commit
git log --oneline # Find commit before migration
git checkout <commit-hash>
# Or restore specific files
git checkout HEAD~1 llm_handler.py config.py requirements.txt
Support
- Documentation: See
DEPLOYMENT_GUIDE.md - Issues: https://github.com/monsara/rag-python-rag/issues
- HF Community: https://discuss.huggingface.co/
Migration completed: β All code changes done Ready to deploy: β³ Awaiting HF Space creation and token setup Status: Ready for testing and deployment