Add HF Spaces README config
Browse files
README.md
CHANGED
|
@@ -1,178 +1,14 @@
|
|
| 1 |
-
# 🏥 MedRAG — Medical Knowledge RAG System
|
| 2 |
-
|
| 3 |
-
A full-stack, production-ready Retrieval-Augmented Generation system for medical knowledge, with a ChatGPT-style interface, per-user chat history, JWT auth, and an admin dashboard.
|
| 4 |
-
|
| 5 |
-
## Tech Stack
|
| 6 |
-
|
| 7 |
-
| Layer | Technology |
|
| 8 |
-
|---|---|
|
| 9 |
-
| **LLM** | Llama 4 Scout 17B via **Groq** |
|
| 10 |
-
| **Embeddings** | `NeuML/pubmedbert-base-embeddings` |
|
| 11 |
-
| **Reranker** | `cross-encoder/ms-marco-MiniLM-L-6-v2` |
|
| 12 |
-
| **Vector Store** | **Qdrant** (local path storage) |
|
| 13 |
-
| **Backend** | **FastAPI** + SQLAlchemy async |
|
| 14 |
-
| **Auth** | **JWT** (access + refresh tokens) |
|
| 15 |
-
| **Database** | **PostgreSQL** (users, docs, chat sessions, and message history) |
|
| 16 |
-
| **Frontend** | React + Tailwind + Zustand + Framer Motion |
|
| 17 |
-
|
| 18 |
-
---
|
| 19 |
-
|
| 20 |
-
## Project Structure
|
| 21 |
-
|
| 22 |
-
```
|
| 23 |
-
medical-rag/
|
| 24 |
-
├── app/
|
| 25 |
-
│ ├── main.py # FastAPI app, lifespan, routers
|
| 26 |
-
│ ├── core/
|
| 27 |
-
│ │ ├── config.py # Pydantic settings
|
| 28 |
-
│ │ └── security.py # JWT helpers
|
| 29 |
-
│ ├── db/
|
| 30 |
-
│ │ ├── database.py # Main database (engine, sessions, and core schema)
|
| 31 |
-
│ │ └── chat_db.py # Chat history models (conversations + messages)
|
| 32 |
-
│ ├── schemas/schemas.py # All Pydantic models
|
| 33 |
-
│ ├── services/
|
| 34 |
-
│ │ ├── embedding_service.py # PubMedBERT async encoder
|
| 35 |
-
│ │ ├── reranker_service.py # Cross-encoder reranker
|
| 36 |
-
│ │ ├── qdrant_service.py # Qdrant CRUD
|
| 37 |
-
│ │ ├── llm_service.py # Groq → Llama 4 Scout
|
| 38 |
-
│ │ ├── ingestion_service.py # PDF/DOCX/TXT → chunk → embed → store
|
| 39 |
-
│ │ └── rag_pipeline.py # Retrieve → Rerank → Generate → Persist
|
| 40 |
-
│ └── api/routes/
|
| 41 |
-
│ ├── auth.py # /auth/register, /login, /refresh, /me
|
| 42 |
-
│ ├── conversations.py # /conversations CRUD
|
| 43 |
-
│ ├── query.py # /query (RAG endpoint)
|
| 44 |
-
│ ├── documents.py # /documents upload/list/delete
|
| 45 |
-
│ └── health_admin.py # /health, /admin/*
|
| 46 |
-
├── frontend/
|
| 47 |
-
│ └── src/
|
| 48 |
-
│ ├── api/client.js # Axios + auto-refresh interceptor
|
| 49 |
-
│ ├── store/index.js # Zustand: auth + chat state
|
| 50 |
-
│ ├── pages/
|
| 51 |
-
│ │ ├── AuthPage.jsx # Login + Register
|
| 52 |
-
│ │ ├── ChatPage.jsx # Main ChatGPT-style chat
|
| 53 |
-
│ │ └── AdminPage.jsx # Admin dashboard
|
| 54 |
-
│ └── components/
|
| 55 |
-
│ ├── layout/Sidebar.jsx # Conversation list, search, grouped by date
|
| 56 |
-
│ └── chat/
|
| 57 |
-
│ ├── MessageBubble.jsx # Markdown, sources, timing
|
| 58 |
-
│ ├── ChatInput.jsx # Textarea + PDF upload + button
|
| 59 |
-
│ ├── UploadModal.jsx # Drag & drop upload dialog
|
| 60 |
-
│ └── WelcomeScreen.jsx # Suggestion cards
|
| 61 |
-
└── requirements.txt
|
| 62 |
-
```
|
| 63 |
-
|
| 64 |
-
---
|
| 65 |
-
|
| 66 |
-
## Quickstart
|
| 67 |
-
|
| 68 |
-
### 1. Clone & configure
|
| 69 |
-
|
| 70 |
-
```bash
|
| 71 |
-
cp .env.example .env
|
| 72 |
-
# Edit .env — fill in GROQ_API_KEY, SECRET_KEY, and DATABASE_URL (PostgreSQL connection string)
|
| 73 |
-
```
|
| 74 |
-
|
| 75 |
-
### 2. Backend (local dev)
|
| 76 |
-
|
| 77 |
-
```bash
|
| 78 |
-
python -m venv .venv
|
| 79 |
-
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
| 80 |
-
pip install -r requirements.txt
|
| 81 |
-
|
| 82 |
-
# Start server
|
| 83 |
-
uvicorn app.main:app --reload --port 8000
|
| 84 |
-
```
|
| 85 |
-
|
| 86 |
-
API docs: http://localhost:8000/docs
|
| 87 |
-
|
| 88 |
-
### 3. Frontend (local dev)
|
| 89 |
-
|
| 90 |
-
```bash
|
| 91 |
-
cd frontend
|
| 92 |
-
npm install
|
| 93 |
-
npm run dev
|
| 94 |
-
# → http://localhost:5173
|
| 95 |
-
```
|
| 96 |
-
|
| 97 |
---
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
▼
|
| 105 |
-
[PubMedBERT Embed] — medical-domain embedding
|
| 106 |
-
│
|
| 107 |
-
▼
|
| 108 |
-
[Qdrant Search] — top-20 cosine similarity candidates
|
| 109 |
-
│
|
| 110 |
-
▼
|
| 111 |
-
[Cross-Encoder] — ms-marco-MiniLM-L-6-v2 reranks to top-5
|
| 112 |
-
│
|
| 113 |
-
▼
|
| 114 |
-
[Llama 4 Scout] — Groq inference, medical system prompt
|
| 115 |
-
│
|
| 116 |
-
▼
|
| 117 |
-
Answer + Sources — saved to PostgreSQL database
|
| 118 |
-
```
|
| 119 |
-
|
| 120 |
-
---
|
| 121 |
-
|
| 122 |
-
## API Reference
|
| 123 |
-
|
| 124 |
-
| Method | Endpoint | Auth | Description |
|
| 125 |
-
|---|---|---|---|
|
| 126 |
-
| POST | `/api/v1/auth/register` | None | Create account |
|
| 127 |
-
| POST | `/api/v1/auth/login` | None | Get JWT tokens |
|
| 128 |
-
| POST | `/api/v1/auth/refresh` | None | Refresh access token |
|
| 129 |
-
| GET | `/api/v1/auth/me` | User | Current user info |
|
| 130 |
-
| GET | `/api/v1/conversations` | User | List user's conversations |
|
| 131 |
-
| POST | `/api/v1/conversations` | User | Create new conversation |
|
| 132 |
-
| GET | `/api/v1/conversations/{id}/messages` | User | Get chat history |
|
| 133 |
-
| PATCH | `/api/v1/conversations/{id}` | User | Rename conversation |
|
| 134 |
-
| DELETE | `/api/v1/conversations/{id}` | User | Delete conversation |
|
| 135 |
-
| POST | `/api/v1/query` | User | RAG query (returns answer + sources) |
|
| 136 |
-
| GET | `/api/v1/documents` | User | List all documents |
|
| 137 |
-
| POST | `/api/v1/documents/upload` | Admin | Upload + ingest document |
|
| 138 |
-
| DELETE | `/api/v1/documents/{id}` | Admin | Delete document + vectors |
|
| 139 |
-
| GET | `/api/v1/admin/stats` | Admin | System statistics |
|
| 140 |
-
| GET | `/api/v1/admin/users` | Admin | All users |
|
| 141 |
-
| PATCH | `/api/v1/admin/users/{id}/role` | Admin | Change user role |
|
| 142 |
-
| PATCH | `/api/v1/admin/users/{id}/toggle` | Admin | Enable/disable user |
|
| 143 |
-
|
| 144 |
---
|
| 145 |
|
| 146 |
-
#
|
| 147 |
-
|
| 148 |
-
All tables (users, documents, conversations, and messages) live in the same PostgreSQL database. Strict user data isolation is enforced by queries filtering on the `user_id` column.
|
| 149 |
-
|
| 150 |
-
Conversations are auto-titled from the first message and grouped in the UI by Today / Last 7 days / Older.
|
| 151 |
-
|
| 152 |
-
---
|
| 153 |
-
|
| 154 |
-
## Environment Variables
|
| 155 |
-
|
| 156 |
-
| Variable | Description |
|
| 157 |
-
|---|---|
|
| 158 |
-
| `SECRET_KEY` | JWT signing secret (use a long random string) |
|
| 159 |
-
| `GROQ_API_KEY` | Your Groq API key |
|
| 160 |
-
| `LLM_MODEL` | `meta-llama/llama-4-scout-17b-16e-instruct` |
|
| 161 |
-
| `QDRANT_PATH` | `data/qdrant` |
|
| 162 |
-
| `QDRANT_COLLECTION_NAME` | `medical-knowledge-rag` |
|
| 163 |
-
| `QDRANT_NAMESPACE` | `medical-docs` |
|
| 164 |
-
| `EMBEDDING_MODEL` | `NeuML/pubmedbert-base-embeddings` |
|
| 165 |
-
| `RERANKER_MODEL` | `cross-encoder/ms-marco-MiniLM-L-6-v2` |
|
| 166 |
-
| `RAG_RETRIEVAL_TOP_K` | Candidates from Qdrant (default: 20) |
|
| 167 |
-
| `RERANKER_TOP_K` | Final chunks after rerank (default: 5) |
|
| 168 |
-
| `RAG_CHUNK_SIZE` | Tokens per chunk (default: 512) |
|
| 169 |
-
| `RAG_CHUNK_OVERLAP` | Overlap tokens (default: 64) |
|
| 170 |
-
|
| 171 |
-
---
|
| 172 |
|
| 173 |
-
|
| 174 |
|
| 175 |
-
|
| 176 |
-
- **No documents = no answers** — ingest PDFs/DOCX first before querying
|
| 177 |
-
- The first model load (PubMedBERT + cross-encoder) takes ~30–60 seconds; subsequent requests are fast
|
| 178 |
-
- For production: optionally move Qdrant to a managed/cloud deployment (the app is already fully migrated to use PostgreSQL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: MedRAG
|
| 3 |
+
emoji: 🩺
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
+
# MedRAG — Medical Knowledge RAG Assistant
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
An AI-powered medical question answering system using Retrieval-Augmented Generation (RAG).
|
| 13 |
|
| 14 |
+
Built with FastAPI, PubMedBERT embeddings, Qdrant vector database, and Groq LLM.
|
|
|
|
|
|
|
|
|