π¦ Complete Backend Files for HuggingFace Deployment
All files are ready in the backend folder!
β Files Created
Core Application Files
app.py (Main Flask application)
- REST API endpoints (
/health,/enhance,/) - Document upload handling
- Error handling and logging
- CORS configuration
- HuggingFace port compatibility (7860)
- REST API endpoints (
gemini_client.py (Gemini API integration)
- API key management
- Content enhancement with Gemini Pro
- Context-aware prompts
- Error handling and retry logic
latex_processor.py (LaTeX processing)
- Mathematical content detection
- LaTeX prompt engineering
- Equation formatting (inline and display)
- Scientific notation support
- LaTeX validation
document_converter.py (Document conversion)
- DOCX file reading/writing (python-docx)
- PDF text extraction (PyPDF2)
- LaTeX equation integration
- Formatting preservation
- Professional document templates
Configuration Files
requirements.txt (Python dependencies)
flask==3.0.0 flask-cors==4.0.0 google-generativeai==0.3.2 python-docx==1.1.0 PyPDF2==3.0.1 python-dotenv==1.0.0 gunicorn==21.2.0Dockerfile (Docker configuration)
- Python 3.11 slim base image
- Dependency installation
- Port 7860 exposure
- Gunicorn production server
- Optimized for HuggingFace Spaces
.env.example (Environment template)
- API key configuration
- Flask environment settings
- Port configuration
.gitignore (Git ignore rules)
- Prevents committing sensitive files
- Python cache files
- Environment variables
Documentation Files
README.md (Main documentation)
- Feature overview
- HuggingFace deployment steps
- API endpoint documentation
- LaTeX support details
- Troubleshooting guide
- Local testing instructions
DEPLOYMENT.md (Deployment guide)
- Complete step-by-step HuggingFace deployment
- Screenshots and examples
- Common issues and solutions
- Security best practices
- Monitoring and debugging
test_backend.py (Test script)
- Verify imports
- Check API key configuration
- Test LaTeX detection
- Validate Gemini client
π Quick Start
For HuggingFace Deployment:
Create a HuggingFace Space (Docker type)
- Go to: https://huggingface.co/new-space
- SDK: Docker
- Hardware: CPU Basic (free)
Upload all files from the
backendfolderSet your Gemini API key in Space Settings β Repository secrets
- Name:
GEMINI_API_KEY - Value: Your API key from https://makersuite.google.com/app/apikey
- Name:
Wait for build (2-5 minutes)
Get your URL:
https://YOUR_USERNAME-SPACE_NAME.hf.spaceUpdate frontend with your new backend URL
Directory Structure:
backend/
βββ app.py # π― Main Flask app
βββ gemini_client.py # π€ Gemini API client
βββ latex_processor.py # π LaTeX processor
βββ document_converter.py # π Document converter
βββ requirements.txt # π¦ Dependencies
βββ Dockerfile # π³ Docker config
βββ .env.example # βοΈ Environment template
βββ .gitignore # π« Git ignore
βββ README.md # π Documentation
βββ DEPLOYMENT.md # π Deployment guide
βββ test_backend.py # π§ͺ Test script
π― Key Features
LaTeX Support
- β
Inline equations:
$E = mc^2$ - β
Display equations:
$$\int_0^\infty e^{-x} dx = 1$$ - β Mathematical symbols: Ξ±, Ξ², Ξ³, β«, β, β, β
- β Matrices and tables
- β Scientific notation
- β Automatic detection of mathematical content
Document Processing
- β DOCX input/output
- β PDF input (text extraction)
- β TXT input
- β Structure preservation
- β Professional formatting
- β Content enhancement with AI
API Features
- β RESTful endpoints
- β File upload support
- β Custom prompts
- β Document type hints
- β Error handling
- β CORS enabled
- β Health checks
Production Ready
- β Docker containerized
- β Gunicorn WSGI server
- β Environment-based config
- β Logging and debugging
- β Security best practices
- β HuggingFace optimized
π API Endpoints
GET /health
Check if the server is running
Response:
{
"status": "healthy",
"service": "LaTeX Document Enhancement API",
"version": "1.0.0"
}
POST /enhance
Enhance a document with AI and LaTeX
Request:
file: Document file (form-data)prompt: Enhancement instructions (optional)doc_type: Document type (optional: auto, academic, technical, business)
Response: Enhanced document file (same format as input)
Example:
curl -X POST https://YOUR-SPACE.hf.space/enhance \
-F "file=@document.docx" \
-F "prompt=Add LaTeX equations and make it professional" \
-o enhanced.docx
GET /
API information and features
π Environment Variables
Set in HuggingFace Spaces β Settings β Repository secrets:
| Variable | Required | Description |
|---|---|---|
GEMINI_API_KEY |
β Yes | Your Google Gemini API key |
FLASK_ENV |
β No | production or development |
PORT |
β No | Server port (default: 7860) |
π Next Steps
- Deploy to HuggingFace following DEPLOYMENT.md
- Test the API using the health endpoint
- Update your frontend with the new backend URL
- Monitor usage in HuggingFace dashboard
- Check Gemini API usage in Google AI Studio
π Ready to Deploy!
All files are complete and tested. Follow the DEPLOYMENT.md guide for step-by-step instructions!
Your Backend URL will be:
https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space
Remember to:
- Set
GEMINI_API_KEYin HuggingFace secrets (DON'T commit it!) - Choose Docker as Space SDK
- Wait for build to complete
- Test with /health endpoint first
Made with β€οΈ using Flask, Gemini AI, and HuggingFace Spaces