# aka-sa/medilens architecture

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

- Interactive diagram: https://gitdiagram.com/aka-sa/medilens
- Repository: https://github.com/aka-sa/medilens

## Overview

#### 1. Purpose and Repository Reality
MediLens is specified as a local AI prescription-analysis workflow for Indian handwritten/printed prescriptions: image input becomes structured medication JSON, then patient-facing safety guidance. The checked-in repository is presently documentation/artifact-only; no application, model, validator, UI, dataset, or dependency source paths appear in the tree. Primary available artifacts are `README.md` and `p3/output.json`.

#### 2. Intended Entry Points
- The documented user entry point is a Streamlit prescription-image upload interface, but its implementation is not present.
- The intended programmatic inference entry point uses Hugging Face Transformers with `Qwen2VLForConditionalGeneration`, `AutoProcessor`, and `qwen_vl_utils`.
- `p3/output.json` is the only structured runtime-like artifact available and likely represents a generated or expected output sample.
- `README.md` is the authoritative design specification in this checkout.

#### 3. Image-to-Structure Inference Stage
The core intended stage accepts a prescription image and constructs a multimodal chat request asking Qwen2-VL to extract JSON. Qwen2-VL’s vision encoder, vision-language fusion, and language decoder are responsible for handwriting/OCR interpretation, abbreviation expansion, and schema-like extraction.

Expected boundary: raw, sensitive prescription image → generated text/JSON. The model is intended to be loaded from a local fine-tuned model directory, supporting privacy-oriented local processing. Grounding artifacts: `README.md`, `p3/output.json`.

#### 4. Fine-Tuned Model Asset
The architecture centers on `Qwen/Qwen2-VL-2B-Instruct`, fine-tuned using LoRA on 100+ image-to-structured-JSON prescription pairs. The base/fine-tuned model and LoRA adapters are described but absent from this repository, so deployment requires separately provisioned model weights and processor assets.

Material runtime: Python, PyTorch, Transformers, Qwen VL utility package; optional bitsandbytes 4-bit quantization. GPU acceleration is expected but CPU inference is supported. Grounding path: `README.md`.

#### 5. Structured Prescription Contract
Model output is intended to carry patient demographics, prescription date, clinician/clinic, and an ordered medication list. Each medication captures normalized and brand/generic identities, form/strength, frequency pattern plus human-readable timing, duration, and instructions.

This JSON contract is the critical handoff between probabilistic model inference and deterministic downstream safety/display layers. Parsing and schema validation are architecturally necessary because generated model text must be converted into reliable structured data. Grounding artifacts: `p3/output.json`, `README.md`.

#### 6. Safety Validation Boundary
A Python safety validator is intended to consume extracted medications and produce warnings for drug interactions, allergies, and age/dosage constraints. It relies on a local interaction database described as covering 500+ common interactions, with severity levels from low to critical.

Inputs beyond prescription extraction include a user allergy profile and patient age. The validator is a high-risk boundary: uncertain model extraction must be surfaced alongside safety results and remain subject to pharmacist/clinician verification. No validator/database implementation is checked in. Grounding path: `README.md`.

#### 7. Presentation and External Dependencies
The planned Streamlit UI transforms normalized medications into patient-friendly cards and visible safety warnings. It is not a network service in the documented current design: processing, model execution, and sensitive images are intended to remain local, with images not retained permanently.

External dependencies are model assets from Hugging Face/Qwen and Python ML libraries; the documented future API, mobile app, pharmacy integration, and dashboards are roadmap items rather than current repository components. Grounding path: `README.md`.

Main entry points: [`p3/output.json`](https://github.com/aka-sa/medilens/blob/main/p3/output.json) (Normalized prescription), [`README.md`](https://github.com/aka-sa/medilens/blob/main/README.md) (Architecture specification)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_docs["Checked-in artifacts"]
  node_readme["Architecture specification<br/>Markdown<br/>[README.md]"]
  node_sample_output["Sample prescription JSON<br/>JSON artifact<br/>[output.json]"]
end

subgraph group_local["Intended local workflow"]
  node_upload_ui["Prescription upload UI<br/>Streamlit UI"]
  node_prescription_image["Prescription image<br/>Sensitive input"]
  node_multimodal_request["Multimodal extraction request<br/>Chat prompt"]
  node_generated_text["Generated model text<br/>Untrusted output"]
  node_parser_schema["JSON parsing &amp; schema validation<br/>Structured-output boundary"]
  node_prescription_json["Normalized prescription<br/>Prescription JSON<br/>[output.json]"]
  node_patient_guidance["Patient guidance cards<br/>Streamlit presentation"]
end

subgraph group_model["Model runtime"]
  node_qwen_runtime{{"Qwen2-VL inference<br/>Transformers runtime"}}
  node_model_assets[("Fine-tuned Qwen + LoRA<br/>Local model assets")]
  node_ml_stack["Python ML stack<br/>PyTorch + Transformers"]
end

subgraph group_safety["Safety boundary"]
  node_allergy_age["Allergy profile &amp; age<br/>Patient context"]
  node_safety_validator["Safety validator<br/>Python validation"]
  node_interaction_db[("Interaction database<br/>Local drug data")]
  node_clinical_review(("Pharmacist / clinician review<br/>Human verification"))
end

node_readme -.->|"specifies"| node_upload_ui
node_readme -.->|"specifies"| node_qwen_runtime
node_readme -.->|"specifies"| node_safety_validator
node_sample_output -.->|"illustrates contract"| node_prescription_json
node_upload_ui -->|"accepts"| node_prescription_image
node_prescription_image -->|"attached to"| node_multimodal_request
node_multimodal_request -->|"runs locally"| node_qwen_runtime
node_model_assets -->|"loaded by"| node_qwen_runtime
node_ml_stack -->|"supports"| node_qwen_runtime
node_qwen_runtime -->|"generates"| node_generated_text
node_generated_text -->|"parsed and validated"| node_parser_schema
node_parser_schema -->|"produces"| node_prescription_json
node_prescription_json -->|"medications"| node_safety_validator
node_allergy_age -->|"patient context"| node_safety_validator
node_interaction_db -->|"interaction rules"| node_safety_validator
node_prescription_json -->|"medication details"| node_patient_guidance
node_safety_validator -->|"warnings"| node_patient_guidance
node_patient_guidance -->|"requires verification"| node_clinical_review

click node_readme "https://github.com/aka-sa/medilens/blob/main/README.md"
click node_sample_output "https://github.com/aka-sa/medilens/blob/main/p3/output.json"
click node_prescription_json "https://github.com/aka-sa/medilens/blob/main/p3/output.json"

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_readme,node_sample_output toneBlue
class node_upload_ui,node_prescription_image,node_multimodal_request,node_generated_text,node_parser_schema,node_prescription_json,node_patient_guidance toneAmber
class node_qwen_runtime,node_model_assets,node_ml_stack toneMint
class node_allergy_age,node_safety_validator,node_interaction_db,node_clinical_review toneRose
```

## Components

### Checked-in artifacts

This checkout contains design documentation and a sample output only.

- [Architecture specification](https://github.com/aka-sa/medilens/blob/main/README.md): Markdown · Authoritative description of the intended workflow. · `README.md`
- [Sample prescription JSON](https://github.com/aka-sa/medilens/blob/main/p3/output.json): JSON artifact · Only structured runtime-like artifact in the checkout. · `p3/output.json`

### Intended local workflow

Planned components are not implemented in this repository.

- **Prescription upload UI**: Streamlit UI · Planned local image-upload entry point.
- **Prescription image**: Sensitive input · Handwritten or printed prescription image.
- **Multimodal extraction request**: Chat prompt · Requests structured medication JSON from the image.
- **Generated model text**: Untrusted output · Probabilistic extraction output before parsing.
- **JSON parsing & schema validation**: Structured-output boundary · Converts generated text into reliable prescription data.
- [Normalized prescription](https://github.com/aka-sa/medilens/blob/main/p3/output.json): Prescription JSON · Patient, clinician, date, and ordered medication details. · `p3/output.json`
- **Patient guidance cards**: Streamlit presentation · Medication instructions and visible warnings for review.

### Model runtime

Separately provisioned local model assets.

- **Qwen2-VL inference**: Transformers runtime · Vision, fusion, and language decoding interpret prescriptions.
- **Fine-tuned Qwen + LoRA**: Local model assets · Separately provisioned Qwen2-VL weights, adapters, and processor.
- **Python ML stack**: PyTorch + Transformers · Uses Transformers, PyTorch, qwen_vl_utils, and optional 4-bit quantization.

### Safety boundary

Deterministic checks require clinician or pharmacist verification.

- **Allergy profile & age**: Patient context · Additional inputs for safety checks.
- **Safety validator**: Python validation · Planned interaction, allergy, and age/dosage checks.
- **Interaction database**: Local drug data · Planned local database of common interactions and severity.
- **Pharmacist / clinician review**: Human verification · Required verification for uncertain extraction and safety results.

## Connections

- Architecture specification → Prescription upload UI: specifies
- Architecture specification → Qwen2-VL inference: specifies
- Architecture specification → Safety validator: specifies
- Sample prescription JSON → Normalized prescription: illustrates contract
- Prescription upload UI → Prescription image: accepts
- Prescription image → Multimodal extraction request: attached to
- Multimodal extraction request → Qwen2-VL inference: runs locally
- Fine-tuned Qwen + LoRA → Qwen2-VL inference: loaded by
- Python ML stack → Qwen2-VL inference: supports
- Qwen2-VL inference → Generated model text: generates
- Generated model text → JSON parsing & schema validation: parsed and validated
- JSON parsing & schema validation → Normalized prescription: produces
- Normalized prescription → Safety validator: medications
- Allergy profile & age → Safety validator: patient context
- Interaction database → Safety validator: interaction rules
- Normalized prescription → Patient guidance cards: medication details
- Safety validator → Patient guidance cards: warnings
- Patient guidance cards → Pharmacist / clinician review: requires verification
