An AI-powered medical assistant that answers health questions using Retrieval-Augmented Generation (RAG), with a full chat history sidebar, conversation memory, and a modern UI.
Features • Tech Stack • Installation • API Endpoints • Deployment
| Chat Interface | Chat History Sidebar |
|---|---|
![]() |
![]() |
- 🤖 AI Medical Q&A — Ask any medical question and get specific, actionable answers
- 💬 Chat History Sidebar — Browse and resume previous conversations (like Claude / ChatGPT)
- ➕ New Chat Button — Start fresh conversations anytime; previous chats are auto-saved
- 🧠 Conversation Memory — Bot remembers context within each session for natural follow-ups
- 🔍 RAG Pipeline — Retrieves relevant medical knowledge from Pinecone before generating answers
- 📝 Markdown Rendering — Responses display with bold text, numbered lists, and headers
- 🗄️ Persistent History — All chats stored in local SQLite database across restarts
- 📱 Responsive UI — Collapsible sidebar works on desktop and mobile
- 🚀 Cloud Inference — No local GPU needed; uses HuggingFace Inference API
| Layer | Technology |
|---|---|
| Backend | Python 3.10+, Flask 3.0 |
| Containerization | Docker (HuggingFace Spaces ready, port 7860) |
| LLM | Llama 3.1 8B Instruct via HuggingFace Inference API |
| RAG Framework | LangChain 0.2 |
| Vector Database | Pinecone (index: medical) |
| Embeddings | sentence-transformers/all-MiniLM-L6-v2 |
| Chat Storage | SQLite (local, zero-config) |
| Frontend | HTML5, CSS3, JavaScript, jQuery, marked.js, DOMPurify |
GENAI-Smart-Medical-Chatbot/
├── Dockerfile # Docker image — used for HuggingFace Spaces deployment
├── app.py # Flask app — routes, RAG chain, SQLite logic
├── store_index.py # Script to embed documents and populate Pinecone
├── requirements.txt # Python dependencies
├── setup.py # Package setup
├── .env # Environment variables (not committed)
├── chat_history.db # SQLite database (auto-created at runtime)
│
├── data/
│ └── blood_sugar_guide.txt # Curated medical knowledge (blood sugar management)
│
├── src/
│ ├── helper.py # PDF loading and text splitting utilities
│ └── prompt.py # Legacy prompt template
│
├── model/
│ └── instructions.txt # Model download instructions
│
├── templates/
│ └── chat.html # Full frontend (sidebar + chat UI)
│
├── static/ # Static assets (CSS, images)
└── research/ # Notebooks and experiments
- Python 3.9+
- A HuggingFace account (free) with an API token
- A Pinecone account (free tier) with an API key
git clone https://github.com/agrawal-2005/GENAI-Smart-Medical-Chatbot.git
cd GENAI-Smart-Medical-Chatbotpython -m venv venv
# macOS / Linux
source venv/bin/activate
# Windows
venv\Scripts\activatepip install -r requirements.txtcp .env.example .env # or create manuallyAdd the following to .env:
HUGGINGFACEHUB_API_TOKEN=your_huggingface_token_here
PINECONE_API_KEY=your_pinecone_api_key_herePlace any medical PDF files in the data/ folder, then run:
python store_index.pyThe curated
data/blood_sugar_guide.txtis already indexed. Only re-run this when adding new documents.
python app.pyOpen your browser and visit:
http://localhost:7860
docker build -t medical-chatbot .
docker run -p 7860:7860 \
-e HUGGINGFACEHUB_API_TOKEN=your_token \
-e PINECONE_API_KEY=your_key \
medical-chatbot| Variable | Description | Where to get it |
|---|---|---|
HUGGINGFACEHUB_API_TOKEN |
HuggingFace API token for Llama 3.1 inference | huggingface.co/settings/tokens |
PINECONE_API_KEY |
Pinecone vector database API key | app.pinecone.io |
⚠️ Never commit your.envfile. It is listed in.gitignore.
| Method | Endpoint | Description |
|---|---|---|
GET |
/ |
Main chat interface |
POST |
/get |
Send a message; returns AI response and conversation_id |
GET |
/api/conversations |
List all saved conversations |
POST |
/api/conversations |
Create a new blank conversation |
GET |
/api/conversations/<id> |
Get a conversation with its full message history |
DELETE |
/api/conversations/<id> |
Delete a conversation and all its messages |
curl -X POST http://localhost:7860/get \
-F "msg=What is my target blood sugar range?" \
-F "conversation_id=optional-uuid-here"{
"response": "For most adults with diabetes, target blood sugar levels are:\n- **Before meals:** 80–130 mg/dL\n- **After meals (2hr):** < 180 mg/dL",
"conversation_id": "abc123-..."
}User Message
│
▼
┌─────────────────────┐
│ Pinecone Retriever │ ← Finds top-3 relevant medical document chunks
└─────────────────────┘
│ context
▼
┌─────────────────────┐
│ Prompt Builder │ ← Injects context + last 3 conversation turns
└─────────────────────┘
│ formatted prompt
▼
┌─────────────────────┐
│ Llama 3.1 8B via │ ← HuggingFace Inference API (no local GPU needed)
│ HF Router API │
└─────────────────────┘
│ answer
▼
┌─────────────────────┐
│ SQLite + Response │ ← Saves to DB, renders markdown, returns to UI
└─────────────────────┘
This app requires no local model download — inference runs entirely in the cloud via the HuggingFace API.
The project includes a Dockerfile configured for HF Spaces (port 7860).
- Go to huggingface.co/new-space
- Choose Docker as the Space SDK
- Link your GitHub repo or clone the Space and push manually:
git remote add space https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME git push space main
- Add secrets in Settings → Variables and Secrets:
Secret Value HUGGINGFACEHUB_API_TOKENYour HF token PINECONE_API_KEYYour Pinecone key - The Space will build the Docker image and go live automatically at:
https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
⚠️ SQLite chat history will reset on each Space restart. For persistent history, replace SQLite with a hosted database (PostgreSQL, MongoDB Atlas, etc.)
docker build -t medical-chatbot .
docker run -p 7860:7860 \
-e HUGGINGFACEHUB_API_TOKEN=your_token \
-e PINECONE_API_KEY=your_key \
medical-chatbot- Connect your GitHub repo
- Set start command:
python app.py - Add
HUGGINGFACEHUB_API_TOKENandPINECONE_API_KEYas environment variables - Set port to
7860
Contributions are welcome!
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature-name - Commit your changes:
git commit -m "Add your feature" - Push to the branch:
git push origin feature/your-feature-name - Open a Pull Request
Please make sure your code runs without errors before submitting.
This project is licensed under the MIT License — see the LICENSE file for details.
Prashant Agrawal
⭐ If you find this project useful, please give it a star! ⭐

