A newer version of the Gradio SDK is available: 6.15.2
Deploying to Hugging Face Spaces β Step-by-step guide
What you need
- A free Hugging Face account β https://huggingface.co/join
- Git installed on your machine (or use the HF web UI)
- Optional: a free USDA API key β https://fdc.nal.usda.gov/api-key-signup.html
Option A β Upload via web UI (easiest, no git needed)
1. Create the Space
- Go to https://huggingface.co/new-space
- Fill in:
- Space name:
recipe-health-analyzer(or anything you like) - License: MIT
- SDK: Gradio
- SDK version: 4.15.0
- Hardware: CPU basic (free)
- Space name:
- Click Create Space
2. Upload files
- In your new Space, click Files β Add file β Upload files
- Upload every file from this zip, preserving the folder structure:
app.py requirements.txt README.md utils/__init__.py utils/config.py utils/logger.py speech_module/__init__.py speech_module/transcriber.py recipe_nlp/__init__.py recipe_nlp/parser.py recipe_nlp/extractor.py nutrition_engine/__init__.py nutrition_engine/usda_client.py nutrition_engine/mapper.py health_classifier/__init__.py health_classifier/feature_engineering.py health_classifier/model.py health_classifier/explainer.py - Click Commit changes to main
HF will automatically detect app.py and start building.
3. Add your USDA API key (optional but recommended)
- Go to Settings β Variables and secrets
- Click New secret
- Name:
USDA_API_KEYValue: your key from fdc.nal.usda.gov - Click Save
- The Space will restart and pick up the key automatically
Option B β Deploy via Git (recommended for ongoing development)
1. Create the Space (same as Option A step 1)
2. Clone the Space repo
git clone https://huggingface.co/spaces/YOUR_USERNAME/recipe-health-analyzer
cd recipe-health-analyzer
3. Copy all files into the repo
# From wherever you unzipped the deployment package:
cp -r /path/to/hf_space/* .
4. Push
git add .
git commit -m "Initial deployment"
git push
5. Add your USDA API key
Same as Option A step 3 β use the web UI under Settings β Secrets.
What happens on first startup
The Space build takes about 3β5 minutes the first time because:
- pip installs all dependencies from
requirements.txt torch(CPU-only wheels) is ~800 MB β biggest downloadopenai-whisperdownloads thetinymodel (~75 MB) on first audio request
On subsequent cold starts (Space wakes from sleep):
- Dependencies are cached β startup is ~30 s
- The trained RandomForest classifier is saved to
models/and reloaded automatically - The spaCy model is cached after first download
Hardware tier recommendation
| Tier | RAM | Cost | Notes |
|---|---|---|---|
| CPU basic | 2 GB | Free | Works for text input; audio transcription is slow (~20 s) |
| CPU upgrade | 8 GB | $0.03/hr | Recommended β comfortable for both text and audio |
| T4 GPU | 16 GB | $0.60/hr | Overkill for this app; no GPU-specific code used |
The app is optimised for CPU β Whisper uses tiny model + fp16=False for CPU compatibility.
Troubleshooting
Space is stuck on "Building" β Check the build logs (Logs tab in the Space). Usually a missing file or bad import.
"No module named spacy"
β Make sure spacy>=3.7.0 is in requirements.txt (it is β check the file uploaded correctly).
"Error loading en_core_web_sm"
β The app auto-downloads it on startup via spacy.cli.download. Check Logs to confirm.
Audio transcription returns empty text
β Whisper needs audio at 16 kHz mono. The app handles conversion via librosa automatically.
If you get an error, confirm librosa and soundfile are in your requirements.txt.
USDA API returns 403
β Your USDA_API_KEY secret is not set or incorrect. The app will fall back to the
built-in nutrition database automatically β functionality is not broken.
Space sleeps after 48 hours (free tier) β Free CPU Spaces sleep when inactive. First request after sleep takes ~30 s to wake up. This is normal HF free-tier behaviour.
Sharing your Space
Once deployed, your Space URL is:
https://huggingface.co/spaces/YOUR_USERNAME/recipe-health-analyzer
You can embed it in any webpage with:
<iframe
src="https://YOUR_USERNAME-recipe-health-analyzer.hf.space"
width="100%" height="800"
frameborder="0">
</iframe>
Updating after deployment
Edit files locally and push:
# Edit a file, then:
git add .
git commit -m "Update something"
git push
The Space rebuilds automatically on every push.