# hamsa-dn/safemedtwin architecture

> Architecture diagram of the GitHub repository hamsa-dn/safemedtwin, 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/hamsa-dn/safemedtwin
- Repository: https://github.com/hamsa-dn/safemedtwin

## Overview

SafeMedTwin is a multi-disease health risk assessment and decision-support app. In the recommended unified mode, a user signs in, reviews or scans health information, submits patient features, and receives risk estimates, explanations, safety checks, and follow-up tools including history, a 3D twin, what-if simulations, comparisons, chat, and diet guidance. The optional two-service path uses a Streamlit dashboard and FastAPI API. README-described capabilities without sampled wiring are represented, but their connections are omitted where source evidence was unavailable.

Main entry points: [`streamlit_app.py`](https://github.com/hamsa-dn/safemedtwin/blob/main/streamlit_app.py) (Unified App), [`backend/main.py`](https://github.com/hamsa-dn/safemedtwin/blob/main/backend/main.py) (REST API), [`llm_slm/llm_provider.py`](https://github.com/hamsa-dn/safemedtwin/blob/main/llm_slm/llm_provider.py) (LLM Provider), [`safety/medguard.py`](https://github.com/hamsa-dn/safemedtwin/blob/main/safety/medguard.py) (MedGuard)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_identity["Identity &amp; History"]
  node_account[("Accounts &amp; History<br/>[auth.py]")]
end

subgraph group_assessment["Risk Assessment"]
  node_datasets["Disease Datasets"]
  node_model["Risk Models<br/>[train_models.py]"]
  node_explain["SHAP Explanations<br/>[explainability.py]"]
  node_twin["Patient Twin<br/>[simulator.py]"]
end

subgraph group_guidance["Safety &amp; Guidance"]
  node_guard["MedGuard<br/>[medguard.py]"]
  node_alerts["Risk Alerts<br/>[alert_engine.py]"]
  node_diet["Diet Guidance<br/>[recommendations.py]"]
end

subgraph group_interaction["Patient Tools"]
  node_body["3D Body View<br/>[body_twin.py]"]
  node_compare["LLM-SLM Comparison<br/>[compare.py]"]
  node_chat["Health Chatbot<br/>[chatbot.py]"]
  node_provider["LLM Provider<br/>[llm_provider.py]"]
  node_slm["Small Language Model"]
  node_image_scan["Image Report Scanner<br/>[report_reader.py]"]
  node_pdf_scan["PDF Report Scanner<br/>[pdf_reader.py]"]
end

subgraph group_integration["Service Interfaces"]
  node_unified["Unified App<br/>[streamlit_app.py]"]
  node_dashboard["API Dashboard<br/>[app.py]"]
  node_api["REST API<br/>[main.py]"]
end

node_user(("Patient"))
node_external_llm(("LLM Service"))

node_user -->|"uses"| node_unified
node_user -->|"uses"| node_dashboard
node_unified -.->|"authenticates"| node_account
node_unified -.->|"predicts"| node_model
node_unified -.->|"explains"| node_explain
node_unified -.->|"validates"| node_guard
node_unified -.->|"checks alerts"| node_alerts
node_unified -.->|"simulates"| node_twin
node_unified -.->|"renders"| node_body
node_unified -.->|"compares"| node_compare
node_unified -.->|"offers chat"| node_chat
node_unified -.->|"requests guidance"| node_diet
node_unified -.->|"scans images"| node_image_scan
node_unified -.->|"scans PDFs"| node_pdf_scan
node_dashboard -->|"sends requests"| node_api
node_api -->|"loads models"| node_model
node_api -->|"explains predictions"| node_explain
node_api -->|"validates predictions"| node_guard
node_api -->|"runs simulations"| node_twin
node_api -->|"compares models"| node_compare
node_dashboard -->|"renders twin"| node_body
node_datasets -->|"supplies training data"| node_model
node_chat -->|"calls"| node_provider
node_chat -->|"validates replies"| node_guard
node_diet -->|"personalizes via"| node_provider
node_diet -.->|"checks guidance"| node_guard
node_image_scan -->|"requests vision"| node_provider
node_image_scan -->|"validates summary"| node_guard
node_pdf_scan -->|"requests vision"| node_provider
node_pdf_scan -->|"validates summary"| node_guard
node_provider -.->|"calls"| node_external_llm
node_compare -.->|"compares with"| node_slm
node_provider -.->|"uses configured service"| node_external_llm

click node_unified "https://github.com/hamsa-dn/safemedtwin/blob/main/streamlit_app.py"
click node_dashboard "https://github.com/hamsa-dn/safemedtwin/blob/main/frontend/app.py"
click node_api "https://github.com/hamsa-dn/safemedtwin/blob/main/backend/main.py"
click node_account "https://github.com/hamsa-dn/safemedtwin/blob/main/auth/auth.py"
click node_datasets "https://github.com/hamsa-dn/safemedtwin/blob/main/data/generate_datasets.py"
click node_model "https://github.com/hamsa-dn/safemedtwin/blob/main/models/train_models.py"
click node_explain "https://github.com/hamsa-dn/safemedtwin/blob/main/models/explainability.py"
click node_guard "https://github.com/hamsa-dn/safemedtwin/blob/main/safety/medguard.py"
click node_alerts "https://github.com/hamsa-dn/safemedtwin/blob/main/safety/alert_engine.py"
click node_twin "https://github.com/hamsa-dn/safemedtwin/blob/main/digital_twin/simulator.py"
click node_body "https://github.com/hamsa-dn/safemedtwin/blob/main/frontend/body_twin.py"
click node_compare "https://github.com/hamsa-dn/safemedtwin/blob/main/llm_slm/compare.py"
click node_chat "https://github.com/hamsa-dn/safemedtwin/blob/main/llm_slm/chatbot.py"
click node_provider "https://github.com/hamsa-dn/safemedtwin/blob/main/llm_slm/llm_provider.py"
click node_slm "https://github.com/hamsa-dn/safemedtwin/blob/main/llm_slm/slm_transformers.py"
click node_diet "https://github.com/hamsa-dn/safemedtwin/blob/main/diet/recommendations.py"
click node_image_scan "https://github.com/hamsa-dn/safemedtwin/blob/main/vision/report_reader.py"
click node_pdf_scan "https://github.com/hamsa-dn/safemedtwin/blob/main/vision/pdf_reader.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_account toneBlue
class node_datasets,node_model,node_explain,node_twin toneAmber
class node_guard,node_alerts,node_diet toneMint
class node_body,node_compare,node_chat,node_provider,node_slm,node_image_scan,node_pdf_scan toneRose
class node_unified,node_dashboard,node_api,node_user,node_external_llm toneIndigo
```

## Components

### Identity & History

- [Accounts & History](https://github.com/hamsa-dn/safemedtwin/blob/main/auth/auth.py): `auth/auth.py`

### Risk Assessment

- [Disease Datasets](https://github.com/hamsa-dn/safemedtwin/blob/main/data/generate_datasets.py): `data/generate_datasets.py`
- [Risk Models](https://github.com/hamsa-dn/safemedtwin/blob/main/models/train_models.py): `models/train_models.py`
- [SHAP Explanations](https://github.com/hamsa-dn/safemedtwin/blob/main/models/explainability.py): `models/explainability.py`
- [Patient Twin](https://github.com/hamsa-dn/safemedtwin/blob/main/digital_twin/simulator.py): `digital_twin/simulator.py`

### Safety & Guidance

- [MedGuard](https://github.com/hamsa-dn/safemedtwin/blob/main/safety/medguard.py): `safety/medguard.py`
- [Risk Alerts](https://github.com/hamsa-dn/safemedtwin/blob/main/safety/alert_engine.py): `safety/alert_engine.py`
- [Diet Guidance](https://github.com/hamsa-dn/safemedtwin/blob/main/diet/recommendations.py): `diet/recommendations.py`

### Patient Tools

- [3D Body View](https://github.com/hamsa-dn/safemedtwin/blob/main/frontend/body_twin.py): `frontend/body_twin.py`
- [LLM-SLM Comparison](https://github.com/hamsa-dn/safemedtwin/blob/main/llm_slm/compare.py): `llm_slm/compare.py`
- [Health Chatbot](https://github.com/hamsa-dn/safemedtwin/blob/main/llm_slm/chatbot.py): `llm_slm/chatbot.py`
- [LLM Provider](https://github.com/hamsa-dn/safemedtwin/blob/main/llm_slm/llm_provider.py): `llm_slm/llm_provider.py`
- [Small Language Model](https://github.com/hamsa-dn/safemedtwin/blob/main/llm_slm/slm_transformers.py): `llm_slm/slm_transformers.py`
- [Image Report Scanner](https://github.com/hamsa-dn/safemedtwin/blob/main/vision/report_reader.py): `vision/report_reader.py`
- [PDF Report Scanner](https://github.com/hamsa-dn/safemedtwin/blob/main/vision/pdf_reader.py): `vision/pdf_reader.py`

### Service Interfaces

- [Unified App](https://github.com/hamsa-dn/safemedtwin/blob/main/streamlit_app.py): `streamlit_app.py`
- [API Dashboard](https://github.com/hamsa-dn/safemedtwin/blob/main/frontend/app.py): `frontend/app.py`
- [REST API](https://github.com/hamsa-dn/safemedtwin/blob/main/backend/main.py): `backend/main.py`

### Other components

- **Patient**
- **LLM Service**

## Connections

- Patient → Unified App: uses (evidence: `README.md`)
- Patient → API Dashboard: uses (evidence: `README.md`)
- Unified App → Accounts & History: authenticates
- Unified App → Risk Models: predicts
- Unified App → SHAP Explanations: explains
- Unified App → MedGuard: validates
- Unified App → Risk Alerts: checks alerts
- Unified App → Patient Twin: simulates
- Unified App → 3D Body View: renders
- Unified App → LLM-SLM Comparison: compares
- Unified App → Health Chatbot: offers chat
- Unified App → Diet Guidance: requests guidance
- Unified App → Image Report Scanner: scans images
- Unified App → PDF Report Scanner: scans PDFs
- API Dashboard → REST API: sends requests (evidence: `frontend/app.py`)
- REST API → Risk Models: loads models (evidence: `backend/main.py`)
- REST API → SHAP Explanations: explains predictions (evidence: `backend/main.py`)
- REST API → MedGuard: validates predictions (evidence: `backend/main.py`)
- REST API → Patient Twin: runs simulations (evidence: `backend/main.py`)
- REST API → LLM-SLM Comparison: compares models (evidence: `backend/main.py`)
- API Dashboard → 3D Body View: renders twin (evidence: `frontend/app.py`)
- Disease Datasets → Risk Models: supplies training data (evidence: `models/train_models.py`)
- Health Chatbot → LLM Provider: calls (evidence: `llm_slm/chatbot.py`)
- Health Chatbot → MedGuard: validates replies (evidence: `llm_slm/chatbot.py`)
- Diet Guidance → LLM Provider: personalizes via (evidence: `diet/recommendations.py`)
- Diet Guidance → MedGuard: checks guidance (evidence: `diet/recommendations.py`)
- Image Report Scanner → LLM Provider: requests vision (evidence: `vision/report_reader.py`)
- Image Report Scanner → MedGuard: validates summary (evidence: `vision/report_reader.py`)
- PDF Report Scanner → LLM Provider: requests vision (evidence: `vision/pdf_reader.py`)
- PDF Report Scanner → MedGuard: validates summary (evidence: `vision/pdf_reader.py`)
- LLM Provider → LLM Service: calls
- LLM-SLM Comparison → Small Language Model: compares with
- LLM Provider → LLM Service: uses configured service (evidence: `README.md`)
