File size: 5,146 Bytes
064e698
a058e67
 
 
 
064e698
 
9c876ac
064e698
3e0555e
9c876ac
 
 
 
 
 
 
 
064e698
 
 
a058e67
064e698
9c876ac
 
 
 
064e698
9c876ac
4ba369d
9c876ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
---
title: Medical Assistant UI
emoji: 🩺
colorFrom: blue
colorTo: purple
sdk: docker
app_port: 8501
short_description: RAG-grounded medical Q&A chat over the MedQuAD dataset
tags:
- streamlit
- rag
- medical
- healthcare
- question-answering
- faiss
- sentence-transformers
models:
- sentence-transformers/all-MiniLM-L6-v2
pinned: false
---

# Medical Assistant

Ask a medical question and get an answer grounded in retrieved source
passages from the MedQuAD dataset, with the sources cited under every
response. The answer comes from retrieved text, not from the model's own
memory.

> **Not a substitute for professional medical advice, diagnosis, or treatment.**

This Space is the **frontend** of a two-Space system. Retrieval and
generation run in the
[`medical-assistant-ai-backend`](https://huggingface.co/spaces/whosouravsharma/medical-assistant-ai-backend)
Space.

## How it works

1. You type a question into the chat.
2. This Space sends it to the backend's `POST /ask` endpoint.
3. The backend embeds the question and retrieves the top 5 most similar
   passages from a FAISS index built over MedQuAD.
4. Those passages go into a prompt that tells the model to answer **only**
   from the retrieved context, to avoid diagnosis and prescriptions, and to
   say so when the context isn't enough to answer.
5. A hosted LLM generates the answer, and the backend returns it along with
   its sources.
6. This Space shows the answer with the cited sources underneath.

```
User ──▢ Streamlit UI (this Space) ──POST /ask──▢ FastAPI backend
                                                    β”‚
                                   embed (MiniLM) ───
                                   FAISS top-k ──────  (MedQuAD index)
                                   grounded prompt ──
                                   hosted LLM β”€β”€β”€β”€β”€β”€β”˜
         ◀──────────── answer + sources ────────────
```

## Components

| Component | Role |
|---|---|
| **This Space** (`Medical-Assistant-ChatBot`) | Streamlit chat UI. Its only dependencies are `streamlit` and `requests`, and it holds no model logic |
| [`medical-assistant-ai-backend`](https://huggingface.co/spaces/whosouravsharma/medical-assistant-ai-backend) | FastAPI service exposing `GET /health` and `POST /ask` |
| `whosouravsharma/medical-assistant-data` (private) | Prebuilt FAISS index and chunk metadata, pulled by the backend at startup |
| [MedQuAD](https://www.kaggle.com/datasets/pythonafroz/medquad-medical-question-answer-for-ai-research) | Source corpus of medical question–answer pairs |

## Models

No model in this project is trained or fine-tuned. The system is built from
off-the-shelf components:

| Stage | Model / method | Notes |
|---|---|---|
| Embedding | [`sentence-transformers/all-MiniLM-L6-v2`](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2) | Small (~90 MB), CPU-friendly, used unmodified. Loaded once per backend process |
| Retrieval | FAISS `IndexFlatIP` on L2-normalized vectors | Exact cosine-similarity search, top-k = 5 |
| Generation | `gpt-5.6-luna` via the OpenAI API | Hosted remotely. No generation model runs inside either Space |

## Data

The index is built offline from MedQuAD:

- Remove exact duplicates and records with no answer
- Join each question and answer into one text field
- Split the text into 500-word chunks with a 50-word overlap
- Store each chunk with its source, focus area, and original question so
  every answer can cite where it came from

## Intended use

**Use it for** general, informational questions about conditions, symptoms,
and treatments that MedQuAD covers.

**Don't use it for** diagnosis, treatment or medication decisions,
emergencies, or questions about a specific patient.

## API

The backend can also be called directly:

```bash
curl -X POST https://whosouravsharma-medical-assistant-ai-backend.hf.space/ask \
  -H "Authorization: Bearer $HF_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"question": "What are the symptoms of glaucoma?"}'
```

The response contains `query`, `answer`, and `sources`. Each source has its
rank, similarity score, source name, focus area, original question, and the
chunk text.

## Setup (for forks / redeploys)

Set these as Space secrets or variables:

| Name | Purpose |
|---|---|
| `BACKEND_URL` | Backend Space URL: `https://whosouravsharma-medical-assistant-ai-backend.hf.space` |
| `HF_TOKEN` | Sent as a bearer token to the backend. Authentication is token-based and there is no user login |

## Limitations

- Answers are limited to what MedQuAD covers. For questions outside it, the
  model is told to say the retrieved information is insufficient rather
  than guess.
- The grounding prompt reduces hallucination risk but doesn't remove it,
  and it hasn't been formally evaluated.
- Generation depends on a third-party hosted model. Its behaviour can only
  be shaped through the prompt.
- No latency, cost, or accuracy benchmarks have been published yet.
- The embedding model is general-purpose and hasn't been tuned for medical
  text.