A Deep Learning chatbot powered by Flan-T5 + LoRA fine-tuning and Retrieval-Augmented Generation (RAG) for answering drug-related questions.
- Fine-tuned Language Model: Flan-T5-Base with LoRA adapters trained on medical Q&A data
- RAG (Retrieval-Augmented Generation): FAISS-based vector search for accurate drug information
- Interactive UI: Built with Streamlit for easy interaction
- Source Attribution: Shows sources used for each answer
- Medical Database: Information from DailyMed and MedQuAD datasets
- Python 3.8 or higher
- CUDA-capable GPU (optional, but recommended for faster inference)
- Git (for cloning the repository)
git clone <repository-url>
cd deep-learning-final-projectOn Windows:
python -m venv venv
venv\Scripts\activateOn macOS/Linux:
python -m venv venv
source venv/bin/activatepip install -r requirements.txtNote: If you have a CUDA-enabled GPU and want to use GPU acceleration:
- Install PyTorch with CUDA support from pytorch.org
- Replace
faiss-cpuwithfaiss-gpuin requirements.txt
deep-learning-final-project/
βββ app/
β βββ streamlit_app.py # Main Streamlit application
β βββ __init__.py
βββ notebooks/
β βββ faiss_rag_builder.py # RAG retriever implementation
β βββ finetuning_model.ipynb # Model fine-tuning notebook
β βββ ...
βββ data/
β βββ faiss/ # FAISS index and metadata
β βββ finetuning/ # Training data
β βββ rag/ # RAG knowledge base
βββ models/
β βββ drug_qna_lora/
β βββ final/ # Fine-tuned model checkpoint
βββ requirements.txt # Python dependencies
βββ README.md # This file
Make sure you have:
- β
Trained model files in
models/drug_qna_lora/final/ - β
FAISS index files in
data/faiss/drug_knowledge.indexmetadata.pklconfig.json
If these files are missing, you'll need to:
- Run the fine-tuning notebook:
notebooks/finetuning_model.ipynb - Build the RAG index:
notebooks/faiss_rag_builder.ipynb
streamlit run app/streamlit_app.pyThe application will automatically open in your default web browser at http://localhost:8501
streamlit run app/streamlit_app.py --server.port 8080- Ask Questions: Type your drug-related questions in the chat input
- View Answers: The chatbot will provide answers based on the trained model
- Check Sources: Expand the "Sources Used" section to see the retrieved documents
- Try Examples: Click the example questions in the sidebar
- Toggle RAG: Enable/disable RAG in the settings (sidebar)
- Clear Chat: Use the "Clear Chat" button to start a new conversation
- "What is the dosage of Amoxicillin?"
- "What are the warnings and precautions of Atorvastatin?"
- "How does Albuterol work?"
- "What are the side effects of Ibuprofen?"
- "When should I not take Amoxicillin?"
Edit the paths in app/streamlit_app.py:
MODEL_DIR = ROOT / "models" / "drug_qna_lora" / "final"
FAISS_DIR = ROOT / "data" / "faiss"Modify the generation parameters in the generate_answer() function:
outputs = model.generate(
**inputs,
max_length=512, # Maximum output length
num_beams=4, # Beam search width
repetition_penalty=1.2, # Avoid repetition
no_repeat_ngram_size=3, # N-gram blocking
early_stopping=True,
)Error: "β Model not loaded"
Solution:
- Ensure you have trained the model first
- Check that the model files exist in
models/drug_qna_lora/final/ - Run the fine-tuning notebook to create the model
Error: "
Solution:
- Check that FAISS index files exist in
data/faiss/ - Run the
faiss_rag_builder.ipynbnotebook to build the index - Verify the
faiss-cpupackage is installed
Solution:
- Reduce batch size or max_length in generation
- Use CPU instead of GPU (set
device_map="cpu") - Close other memory-intensive applications
Solution:
- Use GPU if available (install CUDA-enabled PyTorch)
- Reduce
num_beamsparameter - Cache the model loading (already implemented with
@st.cache_resource)
To train the model from scratch:
- Prepare your dataset in
data/finetuning/ - Open and run
notebooks/finetuning_model.ipynb - The trained model will be saved to
models/drug_qna_lora/final/
To rebuild the FAISS index:
- Ensure your knowledge base is in
data/rag/knowledge_base.json - Open and run
notebooks/faiss_rag_builder.ipynb - The index will be saved to
data/faiss/
This chatbot is for educational purposes only.
- Do NOT use this as a substitute for professional medical advice
- Always consult with qualified healthcare professionals for medical decisions
- The information provided may not be complete or up-to-date
- This is a student project for a Deep Learning course
- Course: Deep Learning Final Project
- Technology Stack:
- Flan-T5-Base (Google)
- LoRA (Low-Rank Adaptation)
- FAISS (Facebook AI Similarity Search)
- Sentence Transformers
- Streamlit
- Data Sources:
- DailyMed (drug information)
- MedQuAD (medical Q&A dataset)
This project is for educational purposes. Please check the licenses of the underlying models and datasets: