Spaces:
Runtime error
Runtime error
Merge remote-tracking branch 'hf/main'
Browse filesFiles changed (2)
- .gitattributesAdded+35
- README.md+8-308
1234567891011121314151617181920212223242526272829303132333435*.7z filter=lfs diff=lfs merge=lfs -text*.arrow filter=lfs diff=lfs merge=lfs -text*.bin filter=lfs diff=lfs merge=lfs -text*.bz2 filter=lfs diff=lfs merge=lfs -text*.ckpt filter=lfs diff=lfs merge=lfs -text*.ftz filter=lfs diff=lfs merge=lfs -text*.gz filter=lfs diff=lfs merge=lfs -text*.h5 filter=lfs diff=lfs merge=lfs -text*.joblib filter=lfs diff=lfs merge=lfs -text*.lfs.* filter=lfs diff=lfs merge=lfs -text*.mlmodel filter=lfs diff=lfs merge=lfs -text*.model filter=lfs diff=lfs merge=lfs -text*.msgpack filter=lfs diff=lfs merge=lfs -text*.npy filter=lfs diff=lfs merge=lfs -text*.npz filter=lfs diff=lfs merge=lfs -text*.onnx filter=lfs diff=lfs merge=lfs -text*.ot filter=lfs diff=lfs merge=lfs -text*.parquet filter=lfs diff=lfs merge=lfs -text*.pb filter=lfs diff=lfs merge=lfs -text*.pickle filter=lfs diff=lfs merge=lfs -text*.pkl filter=lfs diff=lfs merge=lfs -text*.pt filter=lfs diff=lfs merge=lfs -text*.pth filter=lfs diff=lfs merge=lfs -text*.rar filter=lfs diff=lfs merge=lfs -text*.safetensors filter=lfs diff=lfs merge=lfs -textsaved_model/**/* filter=lfs diff=lfs merge=lfs -text*.tar.* filter=lfs diff=lfs merge=lfs -text*.tar filter=lfs diff=lfs merge=lfs -text*.tflite filter=lfs diff=lfs merge=lfs -text*.tgz filter=lfs diff=lfs merge=lfs -text*.wasm filter=lfs diff=lfs merge=lfs -text*.xz filter=lfs diff=lfs merge=lfs -text*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310# π Food Recognition Backend APIFast and accurate food recognition API powered by Hugging Face Transformers and Open Food Facts nutrition database.## β¨ Features- π― **Accurate Food Recognition** - Uses specialized `nateraw/food` model- π **Nutrition Data** - Integrates with Open Food Facts database- β‘ **Fast Processing** - Optimized for quick inference- π **Multiple Formats** - Upload files or provide image URLs- π **CORS Enabled** - Ready for web integration- π **Auto Documentation** - Built-in FastAPI docs at `/docs`## π Quick Start### Local Development1. **Clone the repository**```bashgit clone https://github.com/YOUR_USERNAME/food_recognition_backend.gitcd food_recognition_backend```2. **Install dependencies**```bashpip install -r requirements.txt```3. **Set up environment**```bashcp .env.example .env# Edit .env with your API keys (optional)```4. **Run the server**```bashpython app.py```Server will start at `http://localhost:8000`5. **Test the API**```bash# Health checkcurl http://localhost:8000/health# Analyze food imagecurl -X POST http://localhost:8000/analyze \-F "file=@path/to/food.jpg"```### Docker```bash# Build imagedocker build -t food-recognition-api .# Run containerdocker run -p 8000:8000 \-e PORT=8000 \food-recognition-api```## π API Documentation### Endpoints#### `GET /` - Health Check```bashcurl http://localhost:8000/```Response:```json{"status": "healthy","message": "Fast Food Recognition Backend","version": "3.0.0","device": "cpu"}```#### `GET /health` - Detailed Health```bashcurl http://localhost:8000/health```Response:```json{"status": "healthy","model_loaded": true,"device": "cpu","food_pipeline_loaded": true,"model_type": "Professional Food Recognition Models"}```#### `POST /analyze` - Analyze Food Image (Upload)```bashcurl -X POST http://localhost:8000/analyze?top_alternatives=3 \-F "file=@pizza.jpg"```Response:```json{"label": "pizza","confidence": 0.95,"nutrition": {"calories": 266.0,"protein": 11.0,"fat": 10.0,"carbs": 33.0,"fiber": 2.3,"sugar": 3.7,"sodium": 598.0},"alternatives": ["flatbread", "focaccia", "calzone"],"source": "Open Food Facts","off_product_id": "1234567890"}```#### `POST /analyze-url` - Analyze Food Image (URL)```bashcurl -X POST "http://localhost:8000/analyze-url?image_url=https://example.com/pizza.jpg&top_alternatives=3"```Response: Same as `/analyze`#### `GET /search-nutrition/{food_name}` - Search Nutrition Only```bashcurl http://localhost:8000/search-nutrition/pizza```Response:```json{"food_name": "pizza","nutrition": {"calories": 266.0,"protein": 11.0,"fat": 10.0,"carbs": 33.0,"fiber": 2.3,"sugar": 3.7,"sodium": 598.0},"source": "Open Food Facts","off_product_id": "1234567890"}```### Interactive DocumentationVisit `http://localhost:8000/docs` for interactive Swagger UI documentation.## ποΈ Architecture```ββββββββββββββββ Client βββββββββ¬βββββββββΌββββββββββββββββββββββββββββ FastAPI Server ββ (app.py) βββββββββ¬βββββββββββββββββββββββββββββββββββββββββ ββΌ βΌββββββββββββββββ βββββββββββββββββ Food Model β β Nutrition ββ (nateraw) β β Client βββββββββββββββββ ββββββββ¬ββββββββββΌβββββββββββββββββ Open Food ββ Facts API βββββββββββββββββ```## π¦ Models Used- **Primary**: `nateraw/food` - Specialized food classifier- **Fallback**: `Kaludi/food-category-classification-v2.0`- **Alternative**: `google/vit-base-patch16-224`## π§ Configuration### Environment Variables```bash# ServerPORT=8000HOST=0.0.0.0# Optional API Keys (defaults included)USDA_API_KEY=your_keyNUTRITIONIX_APP_ID=your_idNUTRITIONIX_API_KEY=your_key```### Model SelectionEdit `app.py` line 48 to switch models:```pythonMODEL_NAME = "nateraw/food" # Current# MODEL_NAME = "dwililiya/food101-model-classification" # Alternative```## π Performance- **First Request**: ~30-60s (model loading)- **Subsequent Requests**: ~0.5-1s per image- **Memory Usage**: ~1.5GB RAM- **Model Size**: ~500MB## π DeploymentSee [DEPLOYMENT.md](DEPLOYMENT.md) for comprehensive deployment guide including:- π Free hosting options (Hugging Face, Render, Railway)- π° Paid options (Coolify, AWS, GCP)- βοΈ Configuration examples- π Next.js integration guide## π οΈ Tech Stack- **Framework**: FastAPI- **ML Library**: PyTorch + Transformers- **Image Processing**: Pillow- **HTTP Client**: aiohttp (async)- **Validation**: Pydantic v2- **Server**: Uvicorn## π Requirements```txtPython >= 3.11torch >= 2.0.0transformers >= 4.30.0fastapi >= 0.104.1uvicorn[standard] >= 0.24.0```See [requirements.txt](requirements.txt) for full dependencies.## π§ͺ Testing```bash# Test with sample imagecurl -X POST http://localhost:8000/analyze \-F "file=@test_images/pizza.jpg"# Test with URLcurl -X POST "http://localhost:8000/analyze-url?image_url=https://example.com/burger.jpg"# Test nutrition searchcurl http://localhost:8000/search-nutrition/pasta```## π Known Issues1. **422 Error - No Nutrition Data**: Some foods don't have data in Open Food Facts- Solution: Implement fallback or manual input2. **Cold Start**: First request is slow due to model loading- Solution: Use paid hosting tier or implement keep-alive pings3. **Memory Usage**: Large model requires 1.5GB+ RAM- Solution: Use cloud hosting with sufficient memory## π Security- β CORS configured for web integration- β Input validation with Pydantic- β File type validation- β Error handling and logging- β οΈ Add rate limiting for production use## π LicenseMIT License - Free for personal and commercial use## π€ ContributingContributions welcome! Areas for improvement:- Add more ML models- Improve nutrition data accuracy- Add caching layer- Implement rate limiting- Add authentication## π§ ContactFor issues or questions, open a GitHub issue.## π Acknowledgments- [Hugging Face](https://huggingface.co) - ML models- [Open Food Facts](https://world.openfoodfacts.org) - Nutrition database- [FastAPI](https://fastapi.tiangolo.com) - Web framework---**Ready to deploy?** Check out [DEPLOYMENT.md](DEPLOYMENT.md) for step-by-step instructions! π12345678910---title: Foodrecognitionapiemoji: πcolorFrom: purplecolorTo: purplesdk: dockerpinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
.gitattributesAdded+35
README.md+8-308