Spaces:
Sleeping
A newer version of the Gradio SDK is available: 6.14.0
π Complete Guide: Deploying Khmer OCR to Hugging Face Spaces
Overview
This guide walks you through deploying your Khmer OCR model as an interactive web app on Hugging Face Spaces.
π Required Files
You need these 6 files:
- app.py - Main Gradio application
- requirements.txt - Python dependencies
- packages.txt - System dependencies (poppler for PDF)
- README.md - Documentation and model card
- .gitattributes - Git LFS configuration for large files
- crnn_khmer_official_natural_v4.pth - Your trained model (you need to provide this)
π§ Step-by-Step Deployment
Step 1: Create a Hugging Face Account
- Go to https://huggingface.co/
- Click "Sign Up" (top right)
- Create your account
- Verify your email
Step 2: Create a New Space
Click your profile picture β "New Space"
Fill in the form:
- Space name:
khmer-ocr(or your preferred name) - License: MIT
- Select the Space SDK: Choose "Gradio"
- Space hardware: CPU Basic (free) or upgrade if needed
- Visibility: Public or Private
- Space name:
Click "Create Space"
Step 3: Set Up Git LFS (Large File Storage)
Your model file is large, so you need Git LFS:
# Install Git LFS (one-time setup)
# On Ubuntu/Debian:
sudo apt-get install git-lfs
# On macOS:
brew install git-lfs
# On Windows:
# Download from https://git-lfs.github.com/
# Initialize Git LFS
git lfs install
Step 4: Clone Your Space Repository
# Clone the repository (replace YOUR_USERNAME and khmer-ocr)
git clone https://huggingface.co/spaces/YOUR_USERNAME/khmer-ocr
cd khmer-ocr
Step 5: Add Your Files
Copy all the files into the cloned directory:
# Copy the files
cp /path/to/app.py .
cp /path/to/requirements.txt .
cp /path/to/packages.txt .
cp /path/to/README.md .
cp /path/to/.gitattributes .
# Copy your model file
cp /path/to/crnn_khmer_official_natural_v4.pth .
IMPORTANT: Make sure your model file is named exactly crnn_khmer_official_natural_v4.pth or update the filename in app.py (line 24).
Step 6: Commit and Push
# Track the model file with Git LFS
git lfs track "*.pth"
# Add all files
git add .
# Commit
git commit -m "Initial commit: Khmer OCR model and app"
# Push to Hugging Face
git push
Step 7: Wait for Build
- Go to your Space URL:
https://huggingface.co/spaces/YOUR_USERNAME/khmer-ocr - The Space will automatically build (takes 2-5 minutes)
- You'll see a "Building..." status
- Once done, you'll see "Running" and the app will be live!
π¨ Customization Options
Change Model Name
If your model file has a different name, edit app.py:
# Line 24
MODEL_PATH = "your_model_name.pth"
Adjust Line Segmentation Parameters
In app.py, modify the LineSegmenter class parameters:
segmenter = LineSegmenter(
min_line_height=20, # Minimum line height in pixels
max_line_height=100, # Maximum line height in pixels
min_line_width=100, # Minimum line width in pixels
min_aspect_ratio=2.0, # Width/height ratio
header_margin=0.12, # Skip top 12% of page
footer_margin=0.10, # Skip bottom 10% of page
side_margin=0.05 # Skip left/right 5% of page
)
Change App Title and Description
Edit the gr.Markdown() sections in app.py to customize the interface text.
Add Example Files
Create an examples folder with sample PDFs/images:
gr.Examples(
examples=[
["examples/sample1.pdf"],
["examples/sample2.jpg"],
],
inputs=file_input
)
π Hardware Options
Free Tier (CPU Basic)
- RAM: 16GB
- CPU: 2 vCPU
- Storage: 50GB
- Cost: Free
- Best for: Testing, low-traffic apps
Upgraded (CPU Upgrade)
- RAM: 32GB
- CPU: 8 vCPU
- Storage: 100GB
- Cost: ~$0.60/hour
- Best for: Production, higher traffic
GPU Options
- T4 GPU: ~$0.60/hour (faster inference)
- A10G GPU: ~$3.15/hour (much faster)
To upgrade: Space Settings β Hardware β Select plan
π Troubleshooting
Problem: "Model file not found"
Solution: Make sure the model file is in the root directory and the filename matches exactly in app.py.
Problem: "Out of memory"
Solution:
- Reduce batch processing
- Upgrade to CPU Upgrade or GPU hardware
- Process PDFs page-by-page instead of all at once
Problem: "pdf2image not working"
Solution: Make sure packages.txt includes poppler-utils
Problem: "Build failed"
Solution: Check the Space logs:
- Go to your Space
- Click "Logs" tab
- Look for error messages
- Fix the issue and push again
Problem: "App is slow"
Solution:
- Upgrade hardware to GPU
- Reduce image DPI in
app.py(currently 300) - Implement caching for repeated requests
π Monitoring
View Usage Stats
- Go to your Space
- Click "Analytics" tab
- See: visitors, requests, errors
View Logs
- Click "Logs" tab
- See real-time app logs
- Debug issues
π Privacy & Security
For Private Models
If you want to keep your model private:
- Set Space visibility to "Private"
- Only you can access it
- Share with specific users: Settings β Collaborators
API Access
Your Space automatically gets an API endpoint:
from gradio_client import Client
client = Client("YOUR_USERNAME/khmer-ocr")
result = client.predict(
file_path="path/to/document.pdf"
)
print(result)
π‘ Pro Tips
Test Locally First
python app.py # Access at http://localhost:7860Use Git LFS for All Large Files
git lfs track "*.pth" git lfs track "*.bin"Add .gitignore
__pycache__/ *.pyc .DS_Store .vscode/ *.logVersion Control
- Tag releases:
git tag v1.0.0 - Use branches for experiments
- Keep main branch stable
- Tag releases:
Documentation
- Update README.md with examples
- Add model performance metrics
- Include usage examples
π Next Steps
Improve the Model
- Collect More Data: Add diverse documents
- Fine-tune: Continue training on specific domains
- Data Augmentation: Add more variations
Enhance the App
- Add Language Selection: Support multiple languages
- Batch Processing: Process multiple files at once
- Export Options: Save as TXT, JSON, DOCX
- Confidence Scores: Show OCR confidence per line
- Interactive Editing: Let users correct mistakes
Share Your Work
- Write a blog post on Hugging Face
- Share on social media
- Add to model hub with proper documentation
- Create tutorials and examples
π Resources
- Hugging Face Docs: https://huggingface.co/docs/hub/spaces
- Gradio Docs: https://gradio.app/docs/
- Git LFS: https://git-lfs.github.com/
- Community: https://discuss.huggingface.co/
β Need Help?
- Hugging Face Forum: https://discuss.huggingface.co/
- Gradio Discord: https://discord.gg/gradio
- GitHub Issues: Open issues on your repo
Good luck with your deployment! π