Spaces:
Runtime error
Runtime error
metadata
title: Medical Diagnosis System
emoji: π₯
colorFrom: yellow
colorTo: green
sdk: docker
pinned: false
license: mit
short_description: Group project for uni work
python_version: 3.11
app_port: 7860
Medical AI Assistant
AI-powered medical chatbot with patient-centric memory, MongoDB persistence, and a fast, modern UI.
π Key Features
- Patient-centric RAG memory
- Short-term: last 3 QA summaries in in-memory LRU (fast context)
- Long-term: last 20 QA summaries per patient persisted in MongoDB (continuity)
- Chat history persistence per session in MongoDB
- Patient/Doctor context saved on all messages and summaries
- Patient search typeahead (name or ID) with instant session hydration
- Doctor dropdown with builtβin "Create doctor user..." flow
- Modern UI: sidebar sessions, modals (doctor, settings, patient profile), dark/light mode, mobile-friendly
- Model integration: Gemini responses, NVIDIA summariser fallback via key rotators
ποΈ Architecture (high-level)
Medical Features
- Medical Knowledge Base: Built-in medical information for common symptoms, conditions, and medications
- Context Awareness: Remembers previous conversations and provides relevant medical context
- Role-Based Responses: Tailored responses based on user's medical role and specialty
- Medical Disclaimers: Appropriate warnings and disclaimers for medical information
- Export Functionality: Export chat sessions for medical records or educational purposes
Backend (FastAPI):
src/core/memory/memory.py: LRU shortβterm memory + sessionssrc/core/memory/history.py: builds context; writes memory/messages to Mongosrc/data/: Mongo helpers (modularized)connection.py: Mongo connection + collection namessession/operations.py: chat sessions (ensure/list/delete/etc.)message/operations.py: chat messages (save/list)patient/operations.py: patients (get/create/update/search)user/operations.py: accounts/doctors (create/search/list)medical/operations.py: medical records + memory summariesutils.py: generic helpers (indexing, backups)
src/api/routes/:chat,session,user(patients),system,static
Frontend (static):
static/index.html,static/css/styles.cssstatic/js/app.js(or modularized understatic/js/ui/*andstatic/js/chat/*β seeUI_SETUP.md)
π οΈ Quick Start
Prerequisites
- Python 3.11+
- pip
Setup
- Clone and install
git clone https://huggingface.co/spaces/MedAI-COS30018/MedicalDiagnosisSystem
cd MedAI
pip install -r requirements.txt
- Configure environment
# Create .env
echo "GEMINI_API_1=your_gemini_api_key_1" > .env
echo "NVIDIA_API_1=your_nvidia_api_key_1" >> .env
# MongoDB (required)
echo "MONGO_USER=your_mongodb_connection_string" >> .env
# Optional DB name (default: medicaldiagnosissystem)
# Optional DB name (default: medicaldiagnosissystem). Env var key: USER_DB
echo "USER_DB=medicaldiagnosissystem" >> .env
- Run
python -m src.main
Helpful: UI SETUP | SETUP GUIDE
π§ Config
GEMINI_API_1..5,NVIDIA_API_1..5MONGO_USER,MONGO_DBLOG_LEVEL,PORT- Memory: 3 shortβterm, 20 longβterm
π± Usage
- Select/create a doctor; set role/specialty.
- Search patient by name/ID; select a result.
- Start a new chat; ask your question.
- Manage sessions in the sidebar (rename/delete from menu).
- View patient profile and create/edit via modals/pages.
π Endpoints (selected)
POST /chatβ{ response, session_id, timestamp, medical_context? }POST /sessionsβ{ session_id }GET /patients/{patient_id}/sessionsGET /sessions/{session_id}/messagesDELETE /sessions/{session_id}β deletes session (cache + Mongo) and its messagesGET /patients/search?q=term&limit=8
π Data & Privacy
- MongoDB persistence keyed by
patient_idwithdoctor_idattribution - UI localStorage for UX (doctor list, preferences, selected patient)
- Avoid logging PHI; secure Mongo credentials
π§ͺ Dev
pip install -r requirements.txt
python -m src.main # run
pytest # tests
black . && flake8 # format + lint
Project Structure
MedAI/
βββ src/
β βββ api/
β β βββ routes/
β β βββ chat.py # Chat endpoint
β β βββ session.py # Session endpoints
β β βββ user.py # Patient APIs (get/create/update/search)
β β βββ system.py # Health/info
β β βββ static.py # Serve index
β βββ core/
β β βββ memory/
β β β βββ memory.py # LRU shortβterm memory + sessions
β β β βββ history.py # Context builder, persistence hooks
β β βββ state.py # App state (rotators, embeddings, memory)
β βββ data/
β β βββ __init__.py # Barrel exports for data layer
β β βββ connection.py # Mongo connection + collection names
β β βββ utils.py # Indexing, backups
β β βββ session/
β β β βββ operations.py # Sessions: ensure/list/delete
β β βββ message/
β β β βββ operations.py # Messages: save/list
β β βββ patient/
β β β βββ operations.py # Patients: get/create/update/search
β β βββ user/
β β β βββ operations.py # Accounts/Doctors
β β βββ medical/
β β βββ operations.py # Medical records + memory summaries
β βββ models/
β β βββ chat.py
β β βββ user.py
β βββ services/
β β βββ medical_response.py # Calls model(s)
β β βββ summariser.py # Title/QA summarisation
β βββ utils/
β β βββ embeddings.py
β β βββ logger.py
β β βββ rotator.py
β βββ main.py # FastAPI entrypoint
βββ static/
β βββ index.html
β βββ css/
β β βββ styles.css
β β βββ patient.css
β βββ js/
β β βββ app.js # Submodules under /ui/* and /chat/*
β β βββ patient.js
β βββ patient.html
βββ requirements.txt
βββ requirements-dev.txt
βββ Dockerfile
βββ docker-compose.yml
βββ LICENSE
βββ README.md
π§Ύ License & Disclaimer
- MIT License (see LICENSE)
- Educational information only; not a substitute for professional medical advice
- Team D1 - COS30018, Swinburne University of Technology