# mrtig-afk/nutritionaltracker architecture

> Architecture diagram of the GitHub repository mrtig-afk/nutritionaltracker, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mrtig-afk/nutritionaltracker
- Repository: https://github.com/mrtig-afk/nutritionaltracker

## Overview

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: [`backend/main.py`](https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py) (Pipeline), [`frontend/src/App.jsx`](https://github.com/mrtig-afk/nutritionaltracker/blob/main/frontend/src/App.jsx) (App UI), [`frontend/src/main.jsx`](https://github.com/mrtig-afk/nutritionaltracker/blob/main/frontend/src/main.jsx) (Entry), [`backend/init_db.py`](https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/init_db.py) (Init DB)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["App UI<br/>React app<br/>[App.jsx]"]
  node_frontend_main["Entry<br/>[main.jsx]"]
  node_frontend_styles["Styles<br/>CSS<br/>[App.css]"]
  node_frontend_global["Global CSS<br/>[index.css]"]
end

subgraph group_backend["Backend"]
  node_backend_api["API service<br/>FastAPI app<br/>[main.py]"]
  node_backend_pipeline["Pipeline<br/>orchestrator<br/>[main.py]"]
  node_backend_batch["Batch analyze<br/>endpoint<br/>[main.py]"]
  node_backend_legacy["Legacy analyze<br/>endpoint<br/>[main.py]"]
  node_backend_health["Health check<br/>endpoint<br/>[main.py]"]
  node_backend_gemini["Gemini OCR<br/>multimodal extractor<br/>[main.py]"]
  node_backend_s3[("S3 archive<br/>object storage<br/>[main.py]")]
  node_backend_duckdb[("Warehouse<br/>DuckDB store<br/>[main.py]")]
  node_backend_init["Init DB<br/>schema setup<br/>[init_db.py]"]
end

subgraph group_root["Root tools"]
  node_root_duckdb[("DB file<br/>[nutriscan.duckdb]")]
  node_root_main["Local app<br/>python script<br/>[main.py]"]
  node_root_check["Check tool<br/>python script<br/>[check.py]"]
  node_root_eval["Eval tool<br/>python script<br/>[model_eval.py]"]
end

subgraph group_external["External services"]
  node_external_gemini{{"Gemini 2.5<br/>LLM service"}}
  node_external_s3[("AWS S3<br/>object storage")]
end

node_frontend_main -->|"mounts"| node_frontend_app
node_frontend_app -->|"uploads images"| node_backend_batch
node_frontend_app -.->|"fallback path"| node_backend_legacy
node_frontend_app -->|"uses"| node_frontend_styles
node_frontend_app -->|"uses"| node_frontend_global
node_backend_api -->|"exposes"| node_backend_batch
node_backend_api -->|"exposes"| node_backend_legacy
node_backend_api -->|"exposes"| node_backend_health
node_backend_batch -->|"delegates"| node_backend_pipeline
node_backend_legacy -->|"delegates"| node_backend_pipeline
node_backend_pipeline -->|"archives"| node_backend_s3
node_backend_pipeline -->|"extracts"| node_backend_gemini
node_backend_pipeline -->|"persists"| node_backend_duckdb
node_backend_pipeline -->|"returns JSON"| node_backend_api
node_backend_gemini -.->|"uses"| node_external_gemini
node_backend_s3 -.->|"uses"| node_external_s3
node_backend_init -->|"initializes"| node_backend_duckdb
node_root_duckdb -.->|"seed data"| node_backend_duckdb
node_root_main -.->|"experiments"| node_backend_pipeline
node_root_check -.->|"checks"| node_backend_health
node_root_eval -.->|"evaluates"| node_backend_gemini

click node_frontend_app "https://github.com/mrtig-afk/nutritionaltracker/blob/main/frontend/src/App.jsx"
click node_frontend_main "https://github.com/mrtig-afk/nutritionaltracker/blob/main/frontend/src/main.jsx"
click node_frontend_styles "https://github.com/mrtig-afk/nutritionaltracker/blob/main/frontend/src/App.css"
click node_frontend_global "https://github.com/mrtig-afk/nutritionaltracker/blob/main/frontend/src/index.css"
click node_backend_api "https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py"
click node_backend_pipeline "https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py"
click node_backend_batch "https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py"
click node_backend_legacy "https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py"
click node_backend_health "https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py"
click node_backend_gemini "https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py"
click node_backend_s3 "https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py"
click node_backend_duckdb "https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py"
click node_backend_init "https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/init_db.py"
click node_root_duckdb "https://github.com/mrtig-afk/nutritionaltracker/blob/main/nutriscan.duckdb"
click node_root_main "https://github.com/mrtig-afk/nutritionaltracker/blob/main/main.py"
click node_root_check "https://github.com/mrtig-afk/nutritionaltracker/blob/main/check.py"
click node_root_eval "https://github.com/mrtig-afk/nutritionaltracker/blob/main/model_eval.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend_app,node_frontend_main,node_frontend_styles,node_frontend_global toneBlue
class node_backend_api,node_backend_pipeline,node_backend_batch,node_backend_legacy,node_backend_health,node_backend_gemini,node_backend_s3,node_backend_duckdb,node_backend_init toneAmber
class node_root_duckdb,node_root_main,node_root_check,node_root_eval toneMint
class node_external_gemini,node_external_s3 toneRose
```

## Components

### Frontend

React UI and client-side presentation

- [App UI](https://github.com/mrtig-afk/nutritionaltracker/blob/main/frontend/src/App.jsx): React app · Main browser experience for image upload, preview, and nutrition rendering · `frontend/src/App.jsx`
- [Entry](https://github.com/mrtig-afk/nutritionaltracker/blob/main/frontend/src/main.jsx): React entry · Bootstraps the frontend application · `frontend/src/main.jsx`
- [Styles](https://github.com/mrtig-afk/nutritionaltracker/blob/main/frontend/src/App.css): CSS · Component and app styling · `frontend/src/App.css`
- [Global CSS](https://github.com/mrtig-afk/nutritionaltracker/blob/main/frontend/src/index.css): CSS · Base stylesheet and utility layer · `frontend/src/index.css`

### Backend

FastAPI orchestration, model calls, and persistence

- [API service](https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py): FastAPI app · HTTP surface for analysis and health checks · `backend/main.py`
- [Pipeline](https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py): orchestrator · Coordinates upload handling, extraction, and persistence · `backend/main.py`
- [Batch analyze](https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py): endpoint · Processes multiple uploaded images in one request · `backend/main.py`
- [Legacy analyze](https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py): endpoint · Single-image compatibility path · `backend/main.py`
- [Health check](https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py): endpoint · Service and configuration probe · `backend/main.py`
- [Gemini OCR](https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py): multimodal extractor · Sends one ordered batch request to extract nutrition JSON · `backend/main.py`
- [S3 archive](https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py): object storage · Stores raw images as source-of-truth assets · `backend/main.py`
- [Warehouse](https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/main.py): DuckDB store · Persists extraction records with flexible raw JSON payloads · `backend/main.py`
- [Init DB](https://github.com/mrtig-afk/nutritionaltracker/blob/main/backend/init_db.py): schema setup · Initializes the DuckDB warehouse schema · `backend/init_db.py`

### Root tools

Checked-in local scripts and warehouse artifact

- [DB file](https://github.com/mrtig-afk/nutritionaltracker/blob/main/nutriscan.duckdb): DuckDB file · Checked-in local warehouse artifact · `nutriscan.duckdb`
- [Local app](https://github.com/mrtig-afk/nutritionaltracker/blob/main/main.py): python script · Repository-level runner or experiment entrypoint · `main.py`
- [Check tool](https://github.com/mrtig-afk/nutritionaltracker/blob/main/check.py): python script · Repository-level validation or inspection helper · `check.py`
- [Eval tool](https://github.com/mrtig-afk/nutritionaltracker/blob/main/model_eval.py): python script · Model or pipeline evaluation helper · `model_eval.py`

### External services

Third-party model and storage dependencies

- **Gemini 2.5**: LLM service · Multimodal extraction service used by the backend
- **AWS S3**: object storage · Remote archive for raw uploaded images

## Connections

- 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
