# moizmakadq/moodyfood architecture

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

- Interactive diagram: https://gitdiagram.com/moizmakadq/moodyfood
- Repository: https://github.com/moizmakadq/moodyfood

## Overview

This is a **single-process Python Streamlit web application** that delivers **AI-driven food recommendations**. It:
- Captures **user demographics + preferences** in the UI
- Captures an **image from webcam** and runs **emotion detection** (DeepFace/FER/HSEmotion/custom model)
- Runs a **recommendation pipeline** over a **CSV-backed food dataset** plus **user feedback/history** stored in **SQLite**
- Provides **analytics/visualizations** and a **feedback loop** to adapt future recommendations

So the most accurate “system design” is not microservices; it’s a **modular monolith** with clear internal components and external dependencies.

From the tree + README, the core architectural modules are:

##### Configuration
- `config.py` — central settings for emotion model choice, thresholds, scoring weights, DB path, UI settings.

##### Domain / Application Services
- `emotion_detector.py` — encapsulates face/emotion inference using:
- DeepFace (primary)
- optional FER/HSEmotion/custom model
- depends on OpenCV/Pillow/TensorFlow stack
- `recommendation_engine.py` — the recommendation algorithm:
- mood/diet/cuisine/demographic filtering
- fallback relaxation strategy
- ranking/scoring (incl. randomness + feedback + location bonus)
- `feedback_analyzer.py` — aggregates feedback into cached statistics:
- top-rated foods
- avoid list
- emotion/demographic segmentation

##### Data Access / Persistence
- `database.py` — SQLite access layer (tables: `users`, `user_preferences`, `recommendations_history`, `feedback`)
- `data_loader.py` — loads/cleans/searches dataset from CSV; likely provides in-memory dataframe and search utilities
- `view_database.py` — operational utility (debug/admin-like viewer), not in main runtime path

##### Data Stores (artifacts)
- `data/food_data.csv` — primary item dataset (~1000+ entries)
- `database/food_recommendation.db` — persistent SQLite database file

- Streamlit runtime (web server + session state)
- Browser client
- Webcam device (client-side capture, then image passed to Streamlit app)
- DeepFace + model downloads (first run fetch)
- CV/ML stack (OpenCV, TensorFlow, etc.)

##### `main.py` → `recommendation_engine.py` with
- detected emotion
- demographics/preferences
- dataset access via `data_loader.py`
- feedback stats via `feedback_analyzer.py` (which itself uses `database.py`)
6. `recommendation_engine.py` → `database.py`: record recommendation session into `recommendations_history`
##### User clicks 👍/👎 in UI
- `main.py` → `database.py`: insert into `feedback`
- `main.py` → `feedback_analyzer.py`: invalidate/refresh cache (conceptually)
##### UI renders analytics
- `main.py` reads recommendation outputs + optional aggregated stats (Plotly/Matplotlib/Seaborn)

##### Secondary flows
- `data_loader.py` → `data/food_data.csv` (read-only)
- `database.py` → `database/food_recommendation.db` (read/write)
- `emotion_detector.py` → external model files / downloads (DeepFace assets); optionally `models/best_emotion_model.h5` if present (README mentions it even if not in tree)

##### Architectural patterns worth explicitly encoding
- **Modular monolith**: one Streamlit app process, internally layered modules
- **Pipeline pattern** inside recommendation: filter → relax constraints → score → rank → top-N
- **Feedback loop / adaptive learning**: feedback updates DB → analyzer aggregates → engine consumes aggregates
- **Configuration-driven strategy selection**: `config.py` chooses emotion model backend and scoring weights
- **Persistence separation**:
- Static dataset (CSV) for item candidates
- SQLite for user/session/feedback state

## Diagram (Mermaid)

```mermaid
flowchart TD

%% =========================
%% Context / Actors
%% =========================
subgraph "Context (C4 L1)"
  U["User\n(Browser Client)"]:::actor
  W["Webcam Device\n(Client-side capture)"]:::actor
end

%% =========================
%% Application (Single Process)
%% =========================
subgraph "Moody Food Streamlit App (Python)\nModular Monolith • Single Streamlit server process"
  direction TB

  subgraph "Presentation / Orchestration"
    MAIN["main.py\nStreamlit UI + workflow orchestration\n(3-step flow, analytics rendering)"]:::app
  end

  subgraph "Configuration"
    CFG["config.py\nSettings: model choice, thresholds,\nscoring weights, DB path, UI flags"]:::config
  end

  subgraph "Domain / Application Services"
    ED["emotion_detector.py\nEmotion inference (DeepFace + optional FER/HSEmotion/custom)\nReturns label+confidence+distribution"]:::app
    RE["recommendation_engine.py\nRecommender pipeline:\nfilter → relax constraints → score → rank → top-N"]:::app
    FA["feedback_analyzer.py\nAggregates feedback stats:\ntop-rated, avoid-list, segments (cached)"]:::app
  end

  subgraph "Data Access / Persistence"
    DL["data_loader.py\nCSV → DataFrame utilities (pandas/numpy)\nload/clean/search catalog"]:::app
    DB["database.py\nSQLite gateway (sqlite3)\nusers, preferences, history, feedback"]:::app
    VDB["view_database.py\nOps/debug DB viewer\n(not in main runtime path)"]:::ops
  end
end

%% =========================
%% Data Stores (Artifacts)
%% =========================
subgraph "Data Stores"
  CSV[("data/food_data.csv\nStatic food catalog (read-only)")]:::data
  SQL[("database/food_recommendation.db\nSQLite state: users/preferences/history/feedback")]:::data
end

%% =========================
%% External ML / Runtime Dependencies
%% =========================
subgraph "External Dependencies"
  ST["Streamlit Runtime\nHTTP/WebSocket + Session State"]:::external
  ML["DeepFace / TensorFlow-Keras / OpenCV / Pillow\n(+ optional FER/HSEmotion)\nModel assets/downloads on first run"]:::ml
end

%% =========================
%% High-level container interactions (C4 L2)
%% =========================
U <-->|"HTTP/WebSocket • UI events"| ST
ST -->|"runs"| MAIN

W -->|"image frame / bytes"| MAIN

%% =========================
%% Configuration-driven wiring
%% =========================
MAIN -->|"reads settings"| CFG
ED -->|"uses model config"| CFG
RE -->|"uses scoring weights / thresholds"| CFG
DB -->|"reads DB path"| CFG

%% =========================
%% Primary runtime flow (happy path)
%% =========================
MAIN -->|"upsert user profile/preferences"| DB
MAIN -->|"image → infer()"| ED
ED -->|"emotion label + confidence + distribution"| MAIN

MAIN -->|"request recommendations\n(emotion + demographics + preferences)"| RE

RE -->|"load/search candidates"| DL
DL -->|"read"| CSV

RE -->|"get aggregated feedback stats"| FA
FA -->|"query feedback/history"| DB

RE -->|"record session/history"| DB
RE -->|"recommendation list (top-N)"| MAIN

MAIN -->|"feedback event (👍/👎)"| DB
MAIN -.->|"invalidate/refresh cache (conceptual)"| FA

%% =========================
%% Persistence flows
%% =========================
DB <-->|"read/write"| SQL

%% =========================
%% External ML dependency
%% =========================
ED -->|"inference + model assets/download"| ML

%% =========================
%% Secondary / ops path
%% =========================
VDB -.->|"inspect/query"| DB

%% =========================
%% Styling
%% =========================
classDef actor fill:#E5E7EB,stroke:#6B7280,color:#111827,stroke-width:1px
classDef app fill:#DBEAFE,stroke:#1D4ED8,color:#0B1B3A,stroke-width:1px
classDef config fill:#E0E7FF,stroke:#4338CA,color:#111827,stroke-width:1px
classDef data fill:#DCFCE7,stroke:#16A34A,color:#052e16,stroke-width:1px
classDef ml fill:#FFEDD5,stroke:#C2410C,color:#2A1506,stroke-width:1px
classDef external fill:#F3F4F6,stroke:#374151,color:#111827,stroke-width:1px
classDef ops fill:#FCE7F3,stroke:#BE185D,color:#3B0A22,stroke-width:1px

%% =========================
%% Click Events (component mapping)
%% =========================
click MAIN "https://github.com/moizmakadq/moodyfood/blob/main/main.py"
click CFG "https://github.com/moizmakadq/moodyfood/blob/main/config.py"
click ED "https://github.com/moizmakadq/moodyfood/blob/main/emotion_detector.py"
click RE "https://github.com/moizmakadq/moodyfood/blob/main/recommendation_engine.py"
click FA "https://github.com/moizmakadq/moodyfood/blob/main/feedback_analyzer.py"
click DL "https://github.com/moizmakadq/moodyfood/blob/main/data_loader.py"
click DB "https://github.com/moizmakadq/moodyfood/blob/main/database.py"
click VDB "https://github.com/moizmakadq/moodyfood/blob/main/view_database.py"
click CSV "https://github.com/moizmakadq/moodyfood/blob/main/data/food_data.csv"
click SQL "https://github.com/moizmakadq/moodyfood/blob/main/database/food_recommendation.db"
```
