How gafar8281/foodsnap-ai works
Project type & purpose (what you’re diagramming)
FoodSnap AI is a backend-centric ML inference + data lookup API:
• A FastAPI web service exposing an image upload endpoint (/foodsnap/v1/upload).
• An ML classification module (TensorFlow / ResNet50) that predicts the food label from the image.
• A nutrition data layer (SQLAlchemy + PostgreSQL) that retrieves macros for the predicted food, scaled by quantity.
• A data ingestion / population pipeline (scripts using Selenium/BeautifulSoup/Pandas) that scrapes and loads nutrition data into the DB.
Entry / App Wiring
• main.py → FastAPI app creation, router inclusion, startup events.
• config.py → configuration (likely env vars: DB URL, model path, etc.).
API Layer
• routes/foodsnap.py → the REST endpoint(s), request parsing (image + quantity), response formatting.
Service Layer (business logic)
• services/nutrition_service.py → orchestrates nutrition lookup + quantity scaling; likely called by the route.
ML Inference Layer
• ml_model/model.py → loads model once at startup, preprocesses image, runs inference, returns predicted label.
Persistence Layer
• db/database.py → SQLAlchemy engine/session setup.
• db/models.py → ORM models/tables representing nutrition records.
Data Ingestion (offline job)
• scripts/populate_database.py → scraping + transformation + DB upsert/insert.
Dependencies / Runtime
• requirements.txt → confirms frameworks and libraries used.
Readme insights to incorporate (critical behaviors)
• Model loaded once at startup (startup lifecycle hook → cached singleton model).
• Upload endpoint /foodsnap/v1/upload (core contract).
• Quantity-based calculations (service logic after DB lookup).
• Database population via web scraping (separate flow; not on request path).
• Primary tech: FastAPI, TensorFlow, ResNet50, SQLAlchemy, PostgreSQL, Selenium/BS4/Pandas.
Client / API Consumer
• e.g., mobile app, web app, Postman—uploads image + quantity.
FastAPI Application (FoodSnap AI)
• Subdivide internally into:
• Route Controller (routes/foodsnap.py)
• Nutrition Service (services/nutrition_service.py)
• ML Model Service (ml_model/model.py)
• DB Access Layer (db/database.py, db/models.py)
• Config (config.py)
• As internal components within the service boundary.
PostgreSQL Database
• Stores nutrition facts keyed by food name/label (and macros).
Data Population Job (Scraper/ETL)
• scripts/populate_database.py
• External websites as data sources.
External Data Sources
• “Nutrition websites” (scraped via Selenium/BeautifulSoup).
• “Model artifact storage” (local file / saved model path), if ml_model/model.py loads from disk.
Online request path (critical)
Client
→ (HTTP POST multipart/form-data: image + quantity)
→ FastAPI Route (/foodsnap/v1/upload in routes/foodsnap.py)
→ ML Model (ml_model/model.py)
→ (predicted label: e.g., “pizza”)
→ Nutrition Service (services/nutrition_service.py)
→ SQLAlchemy Session/DAO (db/database.py)
→ PostgreSQL (query nutrition by label)
→ back to Nutrition Service (scale macros by quantity)
→ back to Route
→ Client (JSON: calories, carbs, protein, fats, label, etc.)
FastAPI app startup → ML Model load (“loaded once & cached”).
Offline ingestion / DB population path
External Nutrition Websites
→ (Selenium/BeautifulSoup scrape)
→ populate_database.py (Pandas clean/normalize)
→ PostgreSQL (insert/update nutrition table)
Architectural patterns/design principles evident here
• Layered architecture: Routes (controller) → Services → ML/DB layers.
• Singleton model lifecycle: model initialized once at startup for performance.
• Separation of concerns: scraping/ETL is out-of-band vs online API.
• ORM-based persistence: SQLAlchemy models represent DB schema.
Relevant technologies where they matter
• FastAPI (ASGI) in the service boundary
• TensorFlow + ResNet50 in ML component
• SQLAlchemy in DB access
• PostgreSQL in DB
• Selenium/BeautifulSoup/Pandas in ingestion script
Don’t sprinkle every library everywhere—attach them to the component they power.
Recommended “component breakdown” (don’t overthink—split aggressively)
• main.py (app bootstrap + router registration)
• routes/foodsnap.py (HTTP boundary)
• ml_model/model.py (preprocess + predict; model singleton cache)
• services/nutrition_service.py (business rules: scaling, fallback behavior)
• db/database.py (session/engine management)
• db/models.py (schema/ORM)
• config.py (env/config management)
• Shows online path + offline ingestion + startup model load.
• Emphasizes order and data transformations.