This repository provides a FastAPI-based local service for querying medical documents (PDFs or images) using Google’s Gemini LLM via the LangChain GenAI integration. It ingests a user’s medical document, builds a FAISS vector store, and answers questions with concise bullet-point summaries.
medical-qa-api/
├── app/
│ ├── main.py # FastAPI endpoints and core logic
│ ├── ocr.py # PDF/image to text extraction
│ └── pipeline.py # Text splitting, embedding, and FAISS index management
├── data/ # Runtime session data (auto-created)
├── .env # Environment variables (e.g. GOOGLE_API_KEY)
├── requirements.txt # Python dependencies
└── README.md # This documentation
-
OCR & Text Extraction
- PDF → images via
pdf2image+ text viapytesseract - Direct image OCR via
pytesseract
- PDF → images via
-
Vector Store
- Chunks text with
CharacterTextSplitter(1000-token chunks, 200 overlap) - Embeds with
HuggingFaceEmbeddings(all‑MiniLM‑L6‑v2) - Indexes embeddings in FAISS, persisted per session
- Chunks text with
-
Retrieval‑Augmented QA
- Uses Google Gemini (
ChatGoogleGenerativeAI) for LLM - Custom prompt produces concise bullet‑point summaries
- Uses Google Gemini (
-
Session Isolation
- Each user session stored under
./data/{session_id} - Cleanup endpoint removes session data
- Each user session stored under
-
Python 3.10+
-
Virtual Environment (recommended)
python -m venv venv source venv/bin/activate -
Install dependencies
pip install -r requirements.txt
-
Tesseract OCR (for image and PDF OCR)
# macOS brew install tesseract # Ubuntu sudo apt-get install tesseract-ocr
-
Poppler (for PDF → image conversion)
# macOS brew install poppler # Ubuntu sudo apt-get install poppler-utils
-
Google Gemini API Key
-
Generate at Google AI Studio → API Keys
-
Store in
.env:GOOGLE_API_KEY=AIzaSy…<your_gemini_api_key>…
-
-
Clone repository
git clone [https://github.com/omar-abdel-aziz/med-qa.git] cd medical-qa-api -
Activate virtual environment
source venv/bin/activate -
Install dependencies
pip install -r requirements.txt
-
Create
.envwith yourGOOGLE_API_KEY -
Run server
uvicorn app.main:app --reload --host 127.0.0.1 --port 8000
All endpoints are prefixed by / on http://127.0.0.1:8000:
| Endpoint | Method | Description |
|---|---|---|
/upload |
POST | Upload a PDF/image. Returns session_id. |
/process/{session} |
POST | Process uploaded file (OCR, chunk, embed, index). |
/status/{session} |
GET | Check if processing complete. Returns { processed: bool }. |
/query/{session} |
POST | Ask a question. Returns bullet‑point summary in answer. |
/cleanup/{session} |
DELETE | Delete session data. |
POST /upload
Content-Type: multipart/form-data
Body:
file: <PDF or image>
Response:
{
"session_id": "<sid>"
}POST /process/{sid}
Response:
{
"status": "done"
}GET /status/{sid}
Response:
{
"processed": true
}POST /query/{sid}
Content-Type: application/json
Body:
{
"question": "Your medical question"
}
Response:
{
"answer": [
"- Bullet point 1",
"- Bullet point 2",
"…"
]
}DELETE /cleanup/{sid}
Response:
{
"deleted": true
}app/ocr.py: Extracts text via OCRapp/pipeline.py: Splits, embeds, and persists FAISS indexapp/main.py: Defines FastAPI routes and RAG workflow
Use Axios in React to interact:
import axios from "axios";
const api = axios.create({ baseURL: "http://127.0.0.1:8000" });
// Upload
const { data } = await api.post("/upload", formData);
const sid = data.session_id;
// Process
await api.post(`/process/${sid}`);
// Query
const res = await api.post(`/query/${sid}`, { question: "..." });
console.log(res.data.answer);- Local demo—no external storage or paid services.
- All session data under
./data/{session_id}is removed by/cleanup. - For production, tighten CORS in
app/main.py.