# lucifer55555555/safe-scan architecture

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

- Interactive diagram: https://gitdiagram.com/lucifer55555555/safe-scan
- Repository: https://github.com/lucifer55555555/safe-scan

## Overview

SafeScan AI assesses food products against a user’s allergies, diet, and avoid-list. The main workflow runs from barcode or label input through normalization, deterministic safety rules, evidence retrieval, confidence scoring, explanation, and saved assessment. The React interface also exposes history, comparison, knowledge-base, corpus, evaluation, and regulatory tools. The README and sampled TypeScript sources document the primary app; the tree and backend entry-point references also indicate a separate Python backend, whose internal relationships are only partly shown.

Main entry points: [`backend/main.py`](https://github.com/lucifer55555555/safe-scan/blob/main/backend/main.py) (Python API), [`src/App.tsx`](https://github.com/lucifer55555555/safe-scan/blob/main/src/App.tsx) (App shell), [`server.ts`](https://github.com/lucifer55555555/safe-scan/blob/main/server.ts) (HTTP API), [`src/services/apiClient.ts`](https://github.com/lucifer55555555/safe-scan/blob/main/src/services/apiClient.ts) (API client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experience["User experience"]
  node_app["App shell<br/>[App.tsx]"]
  node_scan["Scan screen<br/>[ScanScreen.tsx]"]
  node_result["Assessment result"]
  node_history["Scan history<br/>[HistoryScreen.tsx]"]
  node_comparison["Product comparison"]
  node_profile["Profile settings<br/>[ProfileScreen.tsx]"]
  node_knowledge_ui["Knowledge base"]
  node_corpus_ui["Corpus browser<br/>[CorpusScreen.tsx]"]
  node_evaluation_ui["Evaluation tools"]
  node_regulatory_ui["Regulatory lab"]
end

subgraph group_assessment["Assessment services"]
  node_api_client["API client<br/>[apiClient.ts]"]
  node_api["HTTP API<br/>[server.ts]"]
  node_barcode["Barcode lookup<br/>[openFoodFacts.ts]"]
  node_normalizer["Ingredient normalizer<br/>[normalizer.ts]"]
  node_rules["Safety rules<br/>[rulesEngine.ts]"]
  node_retrieval["Evidence retrieval<br/>[ragService.ts]"]
  node_explanation["Grounded explanation<br/>[geminiService.ts]"]
  node_evaluator["Benchmark evaluator<br/>[evaluator.ts]"]
end

subgraph group_knowledge["Knowledge and state"]
  node_store[("App data store<br/>[database.ts]")]
  node_ingredients["Ingredient catalog<br/>[seedIngredients.ts]"]
  node_corpus["Scientific corpus"]
  node_products["Sample products<br/>[sampleProducts.ts]"]
end

subgraph group_python["Python backend"]
  node_python_entry["Python API<br/>[main.py]"]
  node_python_config["Python settings<br/>[config.py]"]
  node_python_models["Python models<br/>[models.py]"]
  node_python_parser["Label parser<br/>[parser.py]"]
  node_python_ocr["OCR service<br/>[ocr_service.py]"]
  node_python_rules["Python safety rules<br/>[rules_engine.py]"]
  node_python_rag["Python evidence engine<br/>[rag_engine.py]"]
  node_python_store[("Python database<br/>[database.py]")]
end

node_consumer(("Consumer"))

node_consumer -->|"uses"| node_app
node_app -->|"shows"| node_scan
node_app -->|"shows"| node_result
node_app -->|"shows"| node_history
node_app -->|"shows"| node_comparison
node_app -->|"shows"| node_profile
node_app -->|"shows"| node_regulatory_ui
node_scan -->|"requests assessment"| node_api_client
node_result -->|"loads assessment data"| node_api_client
node_comparison -->|"requests product data"| node_api_client
node_history -->|"loads history"| node_api_client
node_profile -->|"updates profile"| node_api_client
node_corpus_ui -->|"loads corpus data"| node_api_client
node_evaluation_ui -->|"requests evaluation"| node_api_client
node_regulatory_ui -->|"opens"| node_knowledge_ui
node_regulatory_ui -->|"opens"| node_corpus_ui
node_regulatory_ui -->|"opens"| node_evaluation_ui
node_api_client -->|"calls HTTP routes"| node_api
node_api -->|"looks up barcode"| node_barcode
node_api -->|"normalizes ingredients"| node_normalizer
node_api -->|"evaluates safety"| node_rules
node_api -->|"retrieves evidence"| node_retrieval
node_api -->|"generates explanation"| node_explanation
node_api -->|"persists scans"| node_store
node_normalizer -->|"resolves aliases"| node_ingredients
node_rules -->|"resolves ingredient"| node_normalizer
node_retrieval -->|"searches chunks"| node_corpus
node_store -->|"seeds catalog"| node_ingredients
node_store -->|"seeds evidence"| node_corpus
node_store -->|"seeds products"| node_products
node_evaluator -->|"uses normalization"| node_normalizer
node_evaluator -->|"evaluates cases"| node_rules
node_evaluator -->|"checks retrieval"| node_retrieval
node_comparison -->|"uses examples"| node_products
node_python_entry -->|"loads settings"| node_python_config
node_python_entry -->|"uses database"| node_python_store
node_python_entry -->|"uses schemas"| node_python_models
node_python_entry -->|"uses OCR"| node_python_ocr
node_python_entry -->|"parses labels"| node_python_parser
node_python_entry -->|"evaluates safety"| node_python_rules
node_python_entry -->|"retrieves evidence"| node_python_rag
node_python_store -->|"uses settings"| node_python_config
node_python_parser -->|"uses schemas"| node_python_models
node_python_rules -->|"uses schemas"| node_python_models
node_python_rag -->|"configures store"| node_python_config
node_python_rag -->|"uses schemas"| node_python_models

click node_app "https://github.com/lucifer55555555/safe-scan/blob/main/src/App.tsx"
click node_scan "https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/ScanScreen.tsx"
click node_result "https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/AssessmentResultScreen.tsx"
click node_history "https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/HistoryScreen.tsx"
click node_comparison "https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/ComparisonScreen.tsx"
click node_profile "https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/ProfileScreen.tsx"
click node_knowledge_ui "https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/KnowledgeBaseScreen.tsx"
click node_corpus_ui "https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/CorpusScreen.tsx"
click node_evaluation_ui "https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/EvaluationScreen.tsx"
click node_regulatory_ui "https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/RegulatoryLabScreen.tsx"
click node_api_client "https://github.com/lucifer55555555/safe-scan/blob/main/src/services/apiClient.ts"
click node_api "https://github.com/lucifer55555555/safe-scan/blob/main/server.ts"
click node_barcode "https://github.com/lucifer55555555/safe-scan/blob/main/src/services/openFoodFacts.ts"
click node_normalizer "https://github.com/lucifer55555555/safe-scan/blob/main/src/services/normalizer.ts"
click node_rules "https://github.com/lucifer55555555/safe-scan/blob/main/src/services/rulesEngine.ts"
click node_retrieval "https://github.com/lucifer55555555/safe-scan/blob/main/src/services/ragService.ts"
click node_explanation "https://github.com/lucifer55555555/safe-scan/blob/main/src/services/geminiService.ts"
click node_evaluator "https://github.com/lucifer55555555/safe-scan/blob/main/src/services/evaluator.ts"
click node_store "https://github.com/lucifer55555555/safe-scan/blob/main/src/services/database.ts"
click node_ingredients "https://github.com/lucifer55555555/safe-scan/blob/main/src/data/seedIngredients.ts"
click node_corpus "https://github.com/lucifer55555555/safe-scan/blob/main/src/data/scientificCorpus.ts"
click node_products "https://github.com/lucifer55555555/safe-scan/blob/main/src/data/sampleProducts.ts"
click node_python_entry "https://github.com/lucifer55555555/safe-scan/blob/main/backend/main.py"
click node_python_config "https://github.com/lucifer55555555/safe-scan/blob/main/backend/config.py"
click node_python_models "https://github.com/lucifer55555555/safe-scan/blob/main/backend/models.py"
click node_python_parser "https://github.com/lucifer55555555/safe-scan/blob/main/backend/parser.py"
click node_python_ocr "https://github.com/lucifer55555555/safe-scan/blob/main/backend/ocr_service.py"
click node_python_rules "https://github.com/lucifer55555555/safe-scan/blob/main/backend/rules_engine.py"
click node_python_rag "https://github.com/lucifer55555555/safe-scan/blob/main/backend/rag_engine.py"
click node_python_store "https://github.com/lucifer55555555/safe-scan/blob/main/backend/database.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_app,node_scan,node_result,node_history,node_comparison,node_profile,node_knowledge_ui,node_corpus_ui,node_evaluation_ui,node_regulatory_ui toneBlue
class node_api_client,node_api,node_barcode,node_normalizer,node_rules,node_retrieval,node_explanation,node_evaluator toneAmber
class node_store,node_ingredients,node_corpus,node_products toneMint
class node_python_entry,node_python_config,node_python_models,node_python_parser,node_python_ocr,node_python_rules,node_python_rag,node_python_store toneRose
class node_consumer toneIndigo
```

## Components

### User experience

- [App shell](https://github.com/lucifer55555555/safe-scan/blob/main/src/App.tsx): `src/App.tsx`
- [Scan screen](https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/ScanScreen.tsx): `src/components/screens/ScanScreen.tsx`
- [Assessment result](https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/AssessmentResultScreen.tsx): `src/components/screens/AssessmentResultScreen.tsx`
- [Scan history](https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/HistoryScreen.tsx): `src/components/screens/HistoryScreen.tsx`
- [Product comparison](https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/ComparisonScreen.tsx): `src/components/screens/ComparisonScreen.tsx`
- [Profile settings](https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/ProfileScreen.tsx): `src/components/screens/ProfileScreen.tsx`
- [Knowledge base](https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/KnowledgeBaseScreen.tsx): `src/components/screens/KnowledgeBaseScreen.tsx`
- [Corpus browser](https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/CorpusScreen.tsx): `src/components/screens/CorpusScreen.tsx`
- [Evaluation tools](https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/EvaluationScreen.tsx): `src/components/screens/EvaluationScreen.tsx`
- [Regulatory lab](https://github.com/lucifer55555555/safe-scan/blob/main/src/components/screens/RegulatoryLabScreen.tsx): `src/components/screens/RegulatoryLabScreen.tsx`

### Assessment services

- [API client](https://github.com/lucifer55555555/safe-scan/blob/main/src/services/apiClient.ts): `src/services/apiClient.ts`
- [HTTP API](https://github.com/lucifer55555555/safe-scan/blob/main/server.ts): `server.ts`
- [Barcode lookup](https://github.com/lucifer55555555/safe-scan/blob/main/src/services/openFoodFacts.ts): `src/services/openFoodFacts.ts`
- [Ingredient normalizer](https://github.com/lucifer55555555/safe-scan/blob/main/src/services/normalizer.ts): `src/services/normalizer.ts`
- [Safety rules](https://github.com/lucifer55555555/safe-scan/blob/main/src/services/rulesEngine.ts): `src/services/rulesEngine.ts`
- [Evidence retrieval](https://github.com/lucifer55555555/safe-scan/blob/main/src/services/ragService.ts): `src/services/ragService.ts`
- [Grounded explanation](https://github.com/lucifer55555555/safe-scan/blob/main/src/services/geminiService.ts): `src/services/geminiService.ts`
- [Benchmark evaluator](https://github.com/lucifer55555555/safe-scan/blob/main/src/services/evaluator.ts): `src/services/evaluator.ts`

### Knowledge and state

- [App data store](https://github.com/lucifer55555555/safe-scan/blob/main/src/services/database.ts): `src/services/database.ts`
- [Ingredient catalog](https://github.com/lucifer55555555/safe-scan/blob/main/src/data/seedIngredients.ts): `src/data/seedIngredients.ts`
- [Scientific corpus](https://github.com/lucifer55555555/safe-scan/blob/main/src/data/scientificCorpus.ts): `src/data/scientificCorpus.ts`
- [Sample products](https://github.com/lucifer55555555/safe-scan/blob/main/src/data/sampleProducts.ts): `src/data/sampleProducts.ts`

### Python backend

- [Python API](https://github.com/lucifer55555555/safe-scan/blob/main/backend/main.py): `backend/main.py`
- [Python settings](https://github.com/lucifer55555555/safe-scan/blob/main/backend/config.py): `backend/config.py`
- [Python models](https://github.com/lucifer55555555/safe-scan/blob/main/backend/models.py): `backend/models.py`
- [Label parser](https://github.com/lucifer55555555/safe-scan/blob/main/backend/parser.py): `backend/parser.py`
- [OCR service](https://github.com/lucifer55555555/safe-scan/blob/main/backend/ocr_service.py): `backend/ocr_service.py`
- [Python safety rules](https://github.com/lucifer55555555/safe-scan/blob/main/backend/rules_engine.py): `backend/rules_engine.py`
- [Python evidence engine](https://github.com/lucifer55555555/safe-scan/blob/main/backend/rag_engine.py): `backend/rag_engine.py`
- [Python database](https://github.com/lucifer55555555/safe-scan/blob/main/backend/database.py): `backend/database.py`

### Other components

- **Consumer**

## Connections

- Consumer → App shell: uses
- App shell → Scan screen: shows (evidence: `src/App.tsx`)
- App shell → Assessment result: shows (evidence: `src/App.tsx`)
- App shell → Scan history: shows (evidence: `src/App.tsx`)
- App shell → Product comparison: shows (evidence: `src/App.tsx`)
- App shell → Profile settings: shows (evidence: `src/App.tsx`)
- App shell → Regulatory lab: shows (evidence: `src/App.tsx`)
- Scan screen → API client: requests assessment (evidence: `src/components/screens/ScanScreen.tsx`)
- Assessment result → API client: loads assessment data (evidence: `src/components/screens/AssessmentResultScreen.tsx`)
- Product comparison → API client: requests product data (evidence: `src/components/screens/ComparisonScreen.tsx`)
- Scan history → API client: loads history (evidence: `src/components/screens/HistoryScreen.tsx`)
- Profile settings → API client: updates profile (evidence: `src/components/screens/ProfileScreen.tsx`)
- Corpus browser → API client: loads corpus data (evidence: `src/components/screens/CorpusScreen.tsx`)
- Evaluation tools → API client: requests evaluation (evidence: `src/components/screens/EvaluationScreen.tsx`)
- Regulatory lab → Knowledge base: opens (evidence: `src/components/screens/RegulatoryLabScreen.tsx`)
- Regulatory lab → Corpus browser: opens (evidence: `src/components/screens/RegulatoryLabScreen.tsx`)
- Regulatory lab → Evaluation tools: opens (evidence: `src/components/screens/RegulatoryLabScreen.tsx`)
- API client → HTTP API: calls HTTP routes (evidence: `src/services/apiClient.ts`)
- HTTP API → Barcode lookup: looks up barcode (evidence: `server.ts`)
- HTTP API → Ingredient normalizer: normalizes ingredients (evidence: `server.ts`)
- HTTP API → Safety rules: evaluates safety (evidence: `server.ts`)
- HTTP API → Evidence retrieval: retrieves evidence (evidence: `server.ts`)
- HTTP API → Grounded explanation: generates explanation (evidence: `server.ts`)
- HTTP API → App data store: persists scans (evidence: `server.ts`)
- Ingredient normalizer → Ingredient catalog: resolves aliases (evidence: `src/services/normalizer.ts`)
- Safety rules → Ingredient normalizer: resolves ingredient (evidence: `src/services/rulesEngine.ts`)
- Evidence retrieval → Scientific corpus: searches chunks (evidence: `src/services/ragService.ts`)
- App data store → Ingredient catalog: seeds catalog (evidence: `src/services/database.ts`)
- App data store → Scientific corpus: seeds evidence (evidence: `src/services/database.ts`)
- App data store → Sample products: seeds products (evidence: `src/services/database.ts`)
- Benchmark evaluator → Ingredient normalizer: uses normalization (evidence: `src/services/evaluator.ts`)
- Benchmark evaluator → Safety rules: evaluates cases (evidence: `src/services/evaluator.ts`)
- Benchmark evaluator → Evidence retrieval: checks retrieval (evidence: `src/services/evaluator.ts`)
- Product comparison → Sample products: uses examples (evidence: `src/components/screens/ComparisonScreen.tsx`)
- Python API → Python settings: loads settings (evidence: `backend/main.py`)
- Python API → Python database: uses database (evidence: `backend/main.py`)
- Python API → Python models: uses schemas (evidence: `backend/main.py`)
- Python API → OCR service: uses OCR (evidence: `backend/main.py`)
- Python API → Label parser: parses labels (evidence: `backend/main.py`)
- Python API → Python safety rules: evaluates safety (evidence: `backend/main.py`)
- Python API → Python evidence engine: retrieves evidence (evidence: `backend/main.py`)
- Python database → Python settings: uses settings (evidence: `backend/database.py`)
- Label parser → Python models: uses schemas (evidence: `backend/parser.py`)
- Python safety rules → Python models: uses schemas (evidence: `backend/rules_engine.py`)
- Python evidence engine → Python settings: configures store (evidence: `backend/rag_engine.py`)
- Python evidence engine → Python models: uses schemas (evidence: `backend/rag_engine.py`)
