# karthick-216/medical-chatbot architecture

> Architecture diagram of the GitHub repository karthick-216/medical-chatbot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/karthick-216/medical-chatbot
- Repository: https://github.com/karthick-216/medical-chatbot

## Overview

#### Purpose and Entry Points
This is a Streamlit-based multilingual medical-information chatbot. Its runtime entry point is `app.py`; launch with `streamlit run app.py`. The UI delegates language handling and response generation to the `src` package. Offline model adaptation is driven by `scripts/prepare_dataset.py` and `scripts/fine_tune.py`.

#### Presentation and Request Boundary
`app.py` owns interactive concerns: question submission, language/model selection, validation, error display, and rendering translation details and medical disclaimers. It is the boundary between untrusted user text and the application pipeline. Streamlit settings live in `.streamlit/config.toml`.

Paths: `app.py`, `.streamlit/config.toml`

#### Configuration and Safety Policy
Central configuration defines supported UI/ISO languages and their NLLB codes, default model IDs, the disclaimer, and conservative medical-generation instructions. Environment configuration, notably `MEDICAL_LLM_MODEL`, can replace the default generation model without code changes. Safety behavior includes emergency/red-flag keyword handling and prompts intended to constrain the model to educational rather than diagnostic guidance.

Paths: `src/config.py`, `src/medical_chatbot.py`

#### Language Normalization Pipeline
User language is detected or manually selected, then non-English questions are translated to English before generation. Generated English guidance is translated back into the original language. This makes English the internal interchange format and isolates multilingual concerns from the medical generation model.

Paths: `src/language.py`, `src/config.py`, `src/medical_chatbot.py`

#### Chatbot Orchestration
The central orchestration layer sequences validation/safety checks, inbound translation, prompt construction, model inference, and outbound translation. It returns content suitable for the UI, including intermediate English text where requested. This component is the key control-flow boundary: UI code should not directly manage Hugging Face pipelines or model-specific prompt behavior.

Paths: `src/medical_chatbot.py`, `src/language.py`, `src/model_registry.py`

#### Model Loading and External Services
Models are loaded through a registry abstraction rather than directly from the UI or orchestration layer. The application uses Hugging Face Transformers models: by default `facebook/nllb-200-distilled-600M` for translation and `google/flan-t5-small` for text generation. Model identifiers may resolve from the Hugging Face Hub or a local fine-tuned directory; private Hub models require deployment-time credentials/environment configuration.

Paths: `src/model_registry.py`, `src/config.py`, `requirements.txt`

#### Training and Model Lifecycle
The optional training pipeline downloads a Hugging Face Datasets medical-QA source, normalizes examples into prompt/response JSONL, then fine-tunes a seq2seq Transformers model. Produced artifacts are consumed by setting `MEDICAL_LLM_MODEL` to the output directory or published Hub model. Training data and artifacts are operational inputs, not part of the online request path.

Paths: `scripts/prepare_dataset.py`, `scripts/fine_tune.py`, `src/model_registry.py`

#### Deployment Shape
The app is a stateful Python/Streamlit process that may download and retain sizeable model weights locally; cold-start time, memory, and inference latency are material operational concerns. It can run locally, in Hugging Face Spaces, Streamlit Community Cloud, or an HTTPS-fronted AWS process/container. No persistent database, API backend, or queue is represented in this repository.

Paths: `app.py`, `requirements.txt`, `.streamlit/config.toml`

Main entry points: [`src/medical_chatbot.py`](https://github.com/karthick-216/medical-chatbot/blob/main/src/medical_chatbot.py) (Chatbot orchestration), [`src/model_registry.py`](https://github.com/karthick-216/medical-chatbot/blob/main/src/model_registry.py) (Model registry), [`app.py`](https://github.com/karthick-216/medical-chatbot/blob/main/app.py) (Streamlit UI), [`src/language.py`](https://github.com/karthick-216/medical-chatbot/blob/main/src/language.py) (Language normalization)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_online["Online request path"]
  node_streamlit_config["Streamlit settings<br/>configuration<br/>[config.toml]"]
  node_ui["Streamlit UI<br/>entry point<br/>[app.py]"]
  node_chatbot["Chatbot orchestration<br/>application service<br/>[medical_chatbot.py]"]
  node_config["Safety and model policy<br/>configuration module<br/>[config.py]"]
  node_language["Language normalization<br/>translation service<br/>[language.py]"]
  node_english_question["English interchange text<br/>internal payload"]
  node_prompting["Medical prompt construction<br/>generation stage<br/>[medical_chatbot.py]"]
end

subgraph group_models["Model access"]
  node_registry["Model registry<br/>model access abstraction<br/>[model_registry.py]"]
  node_translation_model{{"NLLB translation model<br/>Transformers model<br/>[config.py]"}}
  node_generation_model{{"Medical generation model<br/>seq2seq model<br/>[config.py]"}}
  node_hf_hub{{"Hugging Face Hub<br/>external model service"}}
  node_dependencies["Python dependencies<br/>runtime dependencies<br/>[requirements.txt]"]
end

subgraph group_training["Offline model lifecycle"]
  node_dataset_prep["Dataset preparation<br/>offline script<br/>[prepare_dataset.py]"]
  node_medical_qa[("Medical-QA dataset<br/>external training data")]
  node_fine_tuning["Seq2seq fine-tuning<br/>offline script<br/>[fine_tune.py]"]
  node_fine_tuned_artifacts[("Fine-tuned model artifacts<br/>local or published model")]
end

node_streamlit_config -->|"configures"| node_ui
node_ui -->|"submits question"| node_chatbot
node_config -->|"languages and disclaimer"| node_ui
node_config -->|"safety policy"| node_chatbot
node_chatbot -->|"detects and translates"| node_language
node_language -->|"inbound English"| node_english_question
node_english_question -->|"normalized question"| node_prompting
node_chatbot -->|"after validation"| node_prompting
node_prompting -->|"requests generation model"| node_registry
node_registry -->|"loads"| node_translation_model
node_registry -->|"loads"| node_generation_model
node_hf_hub -->|"model source"| node_registry
node_generation_model -->|"English guidance"| node_chatbot
node_chatbot -->|"outbound translation"| node_language
node_language -->|"localized response"| node_ui
node_dependencies -.->|"runtime packages"| node_ui
node_medical_qa -->|"downloads examples"| node_dataset_prep
node_dataset_prep -->|"JSONL training data"| node_fine_tuning
node_fine_tuning -->|"produces"| node_fine_tuned_artifacts
node_fine_tuned_artifacts -.->|"optional model override"| node_registry
node_fine_tuned_artifacts -.->|"may publish"| node_hf_hub

click node_streamlit_config "https://github.com/karthick-216/medical-chatbot/blob/main/.streamlit/config.toml"
click node_ui "https://github.com/karthick-216/medical-chatbot/blob/main/app.py"
click node_chatbot "https://github.com/karthick-216/medical-chatbot/blob/main/src/medical_chatbot.py"
click node_config "https://github.com/karthick-216/medical-chatbot/blob/main/src/config.py"
click node_language "https://github.com/karthick-216/medical-chatbot/blob/main/src/language.py"
click node_prompting "https://github.com/karthick-216/medical-chatbot/blob/main/src/medical_chatbot.py"
click node_registry "https://github.com/karthick-216/medical-chatbot/blob/main/src/model_registry.py"
click node_translation_model "https://github.com/karthick-216/medical-chatbot/blob/main/src/config.py"
click node_generation_model "https://github.com/karthick-216/medical-chatbot/blob/main/src/config.py"
click node_dependencies "https://github.com/karthick-216/medical-chatbot/blob/main/requirements.txt"
click node_dataset_prep "https://github.com/karthick-216/medical-chatbot/blob/main/scripts/prepare_dataset.py"
click node_fine_tuning "https://github.com/karthick-216/medical-chatbot/blob/main/scripts/fine_tune.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_streamlit_config,node_ui,node_chatbot,node_config,node_language,node_english_question,node_prompting toneBlue
class node_registry,node_translation_model,node_generation_model,node_hf_hub,node_dependencies toneAmber
class node_dataset_prep,node_medical_qa,node_fine_tuning,node_fine_tuned_artifacts toneMint
```

## Components

### Online request path

- [Streamlit settings](https://github.com/karthick-216/medical-chatbot/blob/main/.streamlit/config.toml): configuration · `.streamlit/config.toml`
- [Streamlit UI](https://github.com/karthick-216/medical-chatbot/blob/main/app.py): entry point · Validates input, presents results, translations, errors, and disclaimers. · `app.py`
- [Chatbot orchestration](https://github.com/karthick-216/medical-chatbot/blob/main/src/medical_chatbot.py): application service · Controls safety checks, translation, prompting, inference, and response assembly. · `src/medical_chatbot.py`
- [Safety and model policy](https://github.com/karthick-216/medical-chatbot/blob/main/src/config.py): configuration module · Defines languages, model defaults, disclaimer, and conservative generation instructions. · `src/config.py`
- [Language normalization](https://github.com/karthick-216/medical-chatbot/blob/main/src/language.py): translation service · Converts requests to English and guidance back to the user language. · `src/language.py`
- **English interchange text**: internal payload
- [Medical prompt construction](https://github.com/karthick-216/medical-chatbot/blob/main/src/medical_chatbot.py): generation stage · `src/medical_chatbot.py`

### Model access

- [Model registry](https://github.com/karthick-216/medical-chatbot/blob/main/src/model_registry.py): model access abstraction · Resolves and loads translation and generation models outside UI code. · `src/model_registry.py`
- [NLLB translation model](https://github.com/karthick-216/medical-chatbot/blob/main/src/config.py): Transformers model · Defaults to facebook/nllb-200-distilled-600M. · `src/config.py`
- [Medical generation model](https://github.com/karthick-216/medical-chatbot/blob/main/src/config.py): seq2seq model · Defaults to google/flan-t5-small; MEDICAL_LLM_MODEL can override it. · `src/config.py`
- **Hugging Face Hub**: external model service · Supplies base, private, or published model artifacts.
- [Python dependencies](https://github.com/karthick-216/medical-chatbot/blob/main/requirements.txt): runtime dependencies · Provides Streamlit, Transformers, and training libraries. · `requirements.txt`

### Offline model lifecycle

- [Dataset preparation](https://github.com/karthick-216/medical-chatbot/blob/main/scripts/prepare_dataset.py): offline script · Normalizes medical-QA examples into prompt/response JSONL. · `scripts/prepare_dataset.py`
- **Medical-QA dataset**: external training data
- [Seq2seq fine-tuning](https://github.com/karthick-216/medical-chatbot/blob/main/scripts/fine_tune.py): offline script · Trains a generation model from prepared examples. · `scripts/fine_tune.py`
- **Fine-tuned model artifacts**: local or published model · Selected at deployment through MEDICAL_LLM_MODEL.

## Connections

- Streamlit settings → Streamlit UI: configures
- Streamlit UI → Chatbot orchestration: submits question
- Safety and model policy → Streamlit UI: languages and disclaimer
- Safety and model policy → Chatbot orchestration: safety policy
- Chatbot orchestration → Language normalization: detects and translates
- Language normalization → English interchange text: inbound English
- English interchange text → Medical prompt construction: normalized question
- Chatbot orchestration → Medical prompt construction: after validation
- Medical prompt construction → Model registry: requests generation model
- Model registry → NLLB translation model: loads
- Model registry → Medical generation model: loads
- Hugging Face Hub → Model registry: model source
- Medical generation model → Chatbot orchestration: English guidance
- Chatbot orchestration → Language normalization: outbound translation
- Language normalization → Streamlit UI: localized response
- Python dependencies → Streamlit UI: runtime packages
- Medical-QA dataset → Dataset preparation: downloads examples
- Dataset preparation → Seq2seq fine-tuning: JSONL training data
- Seq2seq fine-tuning → Fine-tuned model artifacts: produces
- Fine-tuned model artifacts → Model registry: optional model override
- Fine-tuned model artifacts → Hugging Face Hub: may publish
