An intelligent healthcare application that explains medication dosages based on prescription information and provides medical assistance through a dual-mode chat interface.
- Prescription Image Processing: Extract text from prescription images using OCR
- Dosage Prediction: Machine learning model to predict appropriate dosages
- Explainable AI: Understand why a particular dosage was recommended
- User-friendly Interface: Simple web interface built with Streamlit
- Multiple Input Methods: Upload prescription images or enter details manually
- Dual-Mode Medical Assistant:
- Independent Mode: Ask general medical questions without requiring a prescription
- Enhanced Mode: Get personalized answers based on your prescription data
- Python 3.8+
- Tesseract OCR (for image processing)
- Google Gemini API key (optional, for enhanced text processing)
-
Clone the repository:
git clone <repository-url> cd drug-dosage-chatbot
-
Install Tesseract OCR:
- Windows: Download and install from UB Mannheim
- macOS:
brew install tesseract - Linux:
sudo apt-get install tesseract-ocr
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install the required packages:
pip install -r requirements.txt
-
(Optional) Set up Google Gemini API:
- Get an API key from Google AI Studio
- Create a
.envfile in the project root and add:GEMINI_API_KEY=your_api_key_here
The easiest way to run the application is using the launcher script:
python run_app.pyThis script will:
- Check if your Gemini API key is set
- Install required dependencies
- Start the Streamlit application
-
Set up Google Gemini API:
- Get an API key from Google AI Studio
- Set it as an environment variable:
# Windows (PowerShell) $env:GEMINI_API_KEY="your-api-key-here" # Windows (Command Prompt) set GEMINI_API_KEY=your-api-key-here # Linux/Mac export GEMINI_API_KEY=your-api-key-here
-
Generate Synthetic Data (Optional):
python data_generator.py
-
Train the Model (Optional):
python train_model.py
-
Run the Web Application:
streamlit run app.py
-
Upload Prescription:
- Click on the "Upload Prescription" tab
- Upload an image of your prescription
- Click "Analyze Prescription" to process the image
-
Or Enter Details Manually:
- Click on the "Enter Details Manually" tab
- Fill in the required information
- Click "Get Dosage Recommendation"
-
View Results:
- See the recommended dosage and frequency
- Expand the explanation to understand how the recommendation was made
- Review important safety information
-
Chat with Medical Assistant:
- Click on "Chat with Medical Assistant"
- Ask questions about your prescription if one is loaded
- Ask general medical questions even without a prescription
app.py: Streamlit web applicationenhanced_chatbot.py: Improved chatbot with independent Gemini interfaceindependent_gemini.py: Standalone Gemini interface for general medical querieschatbot.py: Original chatbot logic and prediction pipelinegemini_chat.py: Original Gemini chat implementationdata_generator.py: Script to generate synthetic prescription datatrain_model.py: Script to train the dosage prediction modelxai_module.py: Explainable AI functionality using SHAPxai_module_simple.py: Simplified XAI when SHAP is not availableocr_parser.py: OCR and text processing for prescription imagespages/chat.py: Streamlit chat interface pagerun_app.py: Launcher script with dependency checksmodels/: Directory for trained modelsdata/: Directory for datasets (not included in version control)
The application provides two modes of operation:
-
Independent Mode:
- Can answer general medical questions without requiring a prescription
- Uses Google's Gemini API to generate responses
- Works even when no prescription data is available
-
Enhanced Mode:
- Provides personalized answers based on prescription data
- Incorporates patient-specific information in responses
- Leverages the dosage prediction model for more accurate advice
The system is designed to handle missing components gracefully:
- If SHAP is not available, it falls back to a simplified explainer
- If no prescription data is available, it still provides general medical advice
- If OCR fails on a prescription image, it allows manual entry of data
To add support for additional medications:
- Update the
medicine_mappinginocr_parser.py - Add the medication to the
valid_diseasesdictionary inchatbot.py - Add appropriate safety notes in
xai_module.py - Retrain the model with updated data
To use a different machine learning model:
- Update the model pipeline in
train_model.py - Modify the prediction logic in
chatbot.py - Update the explanation code in
xai_module.pyif needed
- This is a prototype and not intended for clinical use
- The model is trained on synthetic data and may not be accurate for all cases
- Always consult with a healthcare professional for medical advice
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with Streamlit, scikit-learn, and Google's Gemini API
- Icons by Font Awesome
- Color scheme inspired by Material Design