How mrtig-afk/nutritionaltracker works
1. Two-tier application boundary
- The repo is split into a React frontend in frontend/ and a Python FastAPI backend in backend/.
- The frontend is a thin presentation layer; the backend owns the pipeline, external integrations, and persistence.
2. User interaction flow
- Users upload 1–10 nutrition label images in the UI.
- The frontend sends them as multipart/form-data to the backend’s batch analysis endpoint.
- The backend returns structured nutrition JSON, which the frontend renders immediately.
3. Frontend responsibilities
- Built with React + Vite + Tailwind CSS.
- It handles image preview/order, toggling between nutrient views, and a client-side macro scaling calculator.
- It also normalizes inconsistent OCR/schema variants coming back from the model, so the UI can tolerate non-uniform keys.
4. Backend orchestration
- The backend is a single FastAPI service that exposes:
- POST /analyze-labels for batch processing
- POST /analyze-label as a legacy single-image path
- GET /health for service/config checks
- backend/init_db.py initializes the DuckDB warehouse schema.
5. Primary extraction path
- The core pipeline uses Google Gemini 2.5 Flash as a multimodal OCR/extraction engine.
- All uploaded images are packed into one Gemini request rather than one request per image.
- Gemini returns a JSON array in submission order, so the backend can preserve image-to-result mapping.
6. Storage architecture
- Raw images are archived to AWS S3 and treated as the source of truth for the unmodified assets.
- Structured extraction output is stored in DuckDB as an analytical warehouse.
- The warehouse schema is intentionally flexible: it stores a raw_json payload rather than forcing a rigid relational model.
7. Data model boundary
- The DuckDB table is centered on an extractions record with:
- an ID
- timestamp
- S3 URL
- full JSON payload
- This is a schema-on-read pattern: downstream queries can project fields out of raw_json without requiring migration-heavy schema changes.
8. End-to-end data flow
- Upload images in browser → backend receives files → raw images saved to S3 → Gemini extracts structured nutrition data → backend persists result in DuckDB → response returned to frontend → frontend scales/render macros locally.
- The latency-sensitive work is concentrated server-side; the “serving size” math is intentionally client-side to avoid extra round trips.
9. Deployment boundary
- The README indicates the frontend is meant for Vercel and the backend for Render.
- Environment variables connect the pieces: frontend points to the backend via VITE_API_URL, while backend env vars provide Gemini, AWS, and DuckDB configuration.
10. Repository extras
- At the repo root there is a checked-in nutriscan.duckdb file plus Python utility scripts (main.py, init_db.py, check.py, model_eval.py), suggesting local experimentation/support tooling alongside the main backend/ service.
- The main architecture, however, is clearly the frontend UI + FastAPI orchestration + S3/Gemini/DuckDB pipeline.
Main entry points: main.py Pipeline · App.jsx App UI · main.jsx Entry · init_db.py Init DB
Components 19
Frontend
React UI and client-side presentation
- App UI · Main browser experience for image upload, preview, and nutrition rendering
frontend/src/App.jsx - Entry · Bootstraps the frontend application
frontend/src/main.jsx - Styles · Component and app styling
frontend/src/App.css - Global CSS · Base stylesheet and utility layer
frontend/src/index.css
Backend
FastAPI orchestration, model calls, and persistence
- API service · HTTP surface for analysis and health checks
backend/main.py - Pipeline · Coordinates upload handling, extraction, and persistence
backend/main.py - Batch analyze · Processes multiple uploaded images in one request
backend/main.py - Legacy analyze · Single-image compatibility path
backend/main.py - Health check · Service and configuration probe
backend/main.py - Gemini OCR · Sends one ordered batch request to extract nutrition JSON
backend/main.py - S3 archive · Stores raw images as source-of-truth assets
backend/main.py - Warehouse · Persists extraction records with flexible raw JSON payloads
backend/main.py - Init DB · Initializes the DuckDB warehouse schema
backend/init_db.py
Root tools
Checked-in local scripts and warehouse artifact
- DB file · Checked-in local warehouse artifact
nutriscan.duckdb - Local app · Repository-level runner or experiment entrypoint
main.py - Check tool · Repository-level validation or inspection helper
check.py - Eval tool · Model or pipeline evaluation helper
model_eval.py
External services
Third-party model and storage dependencies
- Gemini 2.5 · Multimodal extraction service used by the backend
- AWS S3 · Remote archive for raw uploaded images
Connections 21
- Entry → App UI: mounts · Boot entry renders the React app
- App UI → Batch analyze: uploads images · Sends multipart batch requests from the browser
- App UI → Legacy analyze: fallback path · Supports older single-image analysis flow
- App UI → Styles: uses · App-specific styling
- App UI → Global CSS: uses · Global and utility styling
- API service → Batch analyze: exposes · Primary batch analysis endpoint
- API service → Legacy analyze: exposes · Compatibility endpoint
- API service → Health check: exposes · Health and configuration endpoint
- Batch analyze → Pipeline: delegates · Hands requests to the orchestration pipeline
- Legacy analyze → Pipeline: delegates · Routes single-image requests into the same pipeline
- Pipeline → S3 archive: archives · Stores raw uploaded images before extraction
- Pipeline → Gemini OCR: extracts · Calls the multimodal model on the image batch
- Pipeline → Warehouse: persists · Writes structured extraction records
- Pipeline → API service: returns JSON · Sends extracted nutrition data back to the client
- Gemini OCR → Gemini 2.5: uses · External multimodal model service
- S3 archive → AWS S3: uses · External object storage service
- Init DB → Warehouse: initializes · Creates or prepares the warehouse schema
- DB file → Warehouse: seed data · Checked-in local database artifact mirrors the warehouse
- Local app → Pipeline: experiments · Repository-level script likely exercises the backend flow
- Check tool → Health check: checks · Local validation or status checks
- Eval tool → Gemini OCR: evaluates · Model evaluation utility targets the extraction layer