How karthick-216/medical-chatbot works
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: medical_chatbot.py Chatbot orchestration · model_registry.py Model registry · app.py Streamlit UI · language.py Language normalization
Components 16
Online request path
- Streamlit settings
.streamlit/config.toml - Streamlit UI · Validates input, presents results, translations, errors, and disclaimers.
app.py - Chatbot orchestration · Controls safety checks, translation, prompting, inference, and response assembly.
src/medical_chatbot.py - Safety and model policy · Defines languages, model defaults, disclaimer, and conservative generation instructions.
src/config.py - Language normalization · Converts requests to English and guidance back to the user language.
src/language.py - English interchange text
- Medical prompt construction
src/medical_chatbot.py
Model access
- Model registry · Resolves and loads translation and generation models outside UI code.
src/model_registry.py - NLLB translation model · Defaults to facebook/nllb-200-distilled-600M.
src/config.py - Medical generation model · Defaults to google/flan-t5-small; MEDICAL_LLM_MODEL can override it.
src/config.py - Hugging Face Hub · Supplies base, private, or published model artifacts.
- Python dependencies · Provides Streamlit, Transformers, and training libraries.
requirements.txt
Offline model lifecycle
- Dataset preparation · Normalizes medical-QA examples into prompt/response JSONL.
scripts/prepare_dataset.py - Medical-QA dataset
- Seq2seq fine-tuning · Trains a generation model from prepared examples.
scripts/fine_tune.py - Fine-tuned model artifacts · Selected at deployment through MEDICAL_LLM_MODEL.
Connections 21
- 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