Spaces:
Configuration error
Configuration error
Upload 4 files
Browse files- README.md +8 -11
- app.py +14 -0
- rag_pipeline.py +27 -0
- requirements.txt +5 -0
README.md
CHANGED
@@ -1,12 +1,9 @@
|
|
1 |
-
|
2 |
-
title: Medical Qa Assistant
|
3 |
-
emoji: 📚
|
4 |
-
colorFrom: yellow
|
5 |
-
colorTo: pink
|
6 |
-
sdk: streamlit
|
7 |
-
sdk_version: 1.44.1
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
---
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 🧠 Medical QA Assistant
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
A Retrieval-Augmented Generation (RAG) app that answers medical questions using research paper snippets from the `pubmed_qa` dataset.
|
4 |
+
|
5 |
+
- Built with: Streamlit, Hugging Face Transformers, FAISS
|
6 |
+
- Embedding Model: BioBERT via `sentence-transformers`
|
7 |
+
- Generator Model: BART
|
8 |
+
|
9 |
+
Great for medical students or MedBot-style experimentation.
|
app.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from rag_pipeline import load_rag_pipeline, ask_question
|
3 |
+
|
4 |
+
st.set_page_config(page_title="Medical QA Assistant 💊", layout="centered")
|
5 |
+
st.title("🧠 Medical QA Assistant")
|
6 |
+
st.markdown("Ask medical questions and get answers based on PubMed research.")
|
7 |
+
|
8 |
+
query = st.text_input("Enter your medical question:")
|
9 |
+
if query:
|
10 |
+
with st.spinner("Searching..."):
|
11 |
+
pipe = load_rag_pipeline()
|
12 |
+
result = ask_question(pipe, query)
|
13 |
+
st.markdown("### 💡 Answer")
|
14 |
+
st.write(result)
|
rag_pipeline.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
from sentence_transformers import SentenceTransformer
|
3 |
+
import faiss
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Dummy knowledge base - replace with real embeddings in production
|
7 |
+
knowledge_base = [
|
8 |
+
{"text": "Aspirin is used to reduce fever and relieve mild to moderate pain.", "embedding": None},
|
9 |
+
{"text": "Hypertension is a condition in which the blood pressure in the arteries is elevated.", "embedding": None},
|
10 |
+
{"text": "Diabetes is a chronic condition that affects how the body processes blood sugar.", "embedding": None},
|
11 |
+
]
|
12 |
+
|
13 |
+
def load_rag_pipeline():
|
14 |
+
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
15 |
+
for entry in knowledge_base:
|
16 |
+
entry["embedding"] = embedder.encode(entry["text"])
|
17 |
+
index = faiss.IndexFlatL2(len(knowledge_base[0]["embedding"]))
|
18 |
+
index.add(np.array([entry["embedding"] for entry in knowledge_base]))
|
19 |
+
return {"embedder": embedder, "index": index, "texts": [entry["text"] for entry in knowledge_base]}
|
20 |
+
|
21 |
+
def ask_question(pipe, query):
|
22 |
+
query_vec = pipe["embedder"].encode(query)
|
23 |
+
D, I = pipe["index"].search(np.array([query_vec]), k=1)
|
24 |
+
context = pipe["texts"][I[0][0]]
|
25 |
+
generator = pipeline("text2text-generation", model="facebook/bart-large")
|
26 |
+
answer = generator(f"question: {query} context: {context}", max_length=100, do_sample=False)
|
27 |
+
return answer[0]['generated_text']
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
torch
|
3 |
+
transformers
|
4 |
+
sentence-transformers
|
5 |
+
faiss-cpu
|