Skip to content

Repository files navigation

Medical Assistant Chatbot

A smart, AI-powered medical assistant that helps you understand prescriptions, answer health questions, and provide general medical guidance. Built with Flask and Google's Gemini AI.

Features

  • 💬 Interactive Chat Interface - Ask medical questions and get informed responses
  • 📄 Prescription Analysis - Upload prescription images and get detailed explanations of medications
  • 🔍 OCR Technology - Automatically extract text from prescription images
  • 💾 Chat History - All conversations are saved and can be reviewed
  • 🎨 Modern UI - Clean, responsive design that works on all devices
  • 🔒 Privacy Focused - All data stored locally in SQLite database

What This Bot Can Do

  1. Answer Medical Questions - Get information about symptoms, conditions, and treatments
  2. Explain Prescriptions - Upload prescription images and understand what medications you've been prescribed
  3. Medication Information - Learn about drug uses, side effects, and precautions
  4. Health Advice - Receive general wellness tips and guidance
  5. Symptom Assessment - Understand when to seek professional help

Important: This chatbot provides general information only and is not a substitute for professional medical advice, diagnosis, or treatment. Always consult qualified healthcare providers for medical decisions.

Prerequisites

Before you begin, make sure you have:

  • Python 3.8 or higher
  • A Google API key (for Gemini AI)
  • Tesseract OCR installed (for prescription text extraction)

Installation Guide

Step 1: Install Python

If you don't have Python installed:

Windows:

  • Download from python.org
  • Run installer and check "Add Python to PATH"

Mac/Linux:

# Mac (using Homebrew)
brew install python3

# Ubuntu/Debian
sudo apt-get install python3 python3-pip

Step 2: Install Tesseract OCR

Tesseract is required for reading text from prescription images.

Windows:

  1. Download installer from GitHub
  2. Run the installer (use default installation path)
  3. Add to PATH: C:\Program Files\Tesseract-OCR

Mac:

brew install tesseract

Linux:

sudo apt-get install tesseract-ocr

Step 3: Get Google API Key

  1. Go to Google AI Studio
  2. Sign in with your Google account
  3. Click "Create API Key"
  4. Copy the key (you'll need it soon)

Step 4: Clone or Download This Project

# If you have git
git clone <repository-url>
cd Medical-Chatbot

# Or download and extract the ZIP file, then navigate to the folder

Step 5: Install Python Dependencies

Open terminal/command prompt in the project folder:

# Install required packages
pip install -r requirements.txt

If you encounter issues, try:

pip install --upgrade pip
pip install -r requirements.txt

Step 6: Configure Environment Variables

  1. Copy the example environment file:
# Windows
copy .env.example .env

# Mac/Linux
cp .env.example .env
  1. Open .env file in a text editor and add your Google API key:
GOOGLE_API_KEY=your_actual_api_key_here
GENAI_MODEL=gemini-pro
DB_PATH=chat_history.db
PORT=5000

Configuring Gemini / Google API Key (Windows PowerShell)

You can set your GOOGLE_API_KEY in a .env file (recommended) or in your PowerShell session.

  • Using the .env file (recommended):

    1. Copy .env.example to .env:
      copy .env.example .env
    2. Open .env in a text editor and paste your actual API key:
      GOOGLE_API_KEY=your_actual_api_key_here
      
  • Setting for the current PowerShell session (temporary):

     $env:GOOGLE_API_KEY = 'your_actual_api_key_here'
  • Setting permanently for your user (persists across sessions):

     [Environment]::SetEnvironmentVariable('GOOGLE_API_KEY', 'your_actual_api_key_here', 'User')

After setting the key, restart your terminal or IDE so environment variables are picked up.

Quick test

  1. With the environment configured, run the app:
python app.py

If GOOGLE_API_KEY is missing the app will print a clear error and exit. If configured correctly, the server should start and be available at http://localhost:5000.

Running the Application

Start the Server

python app.py

You should see output like:

* Running on http://0.0.0.0:5000
* Running on http://127.0.0.1:5000

Access the Application

Open your web browser and go to:

http://localhost:5000

How to Use

1. Asking Medical Questions

Simply type your question in the chat box:

  • "What causes headaches?"
  • "How to treat a common cold?"
  • "What are symptoms of diabetes?"

2. Uploading Prescriptions

  1. Click the file input under "Upload Prescription Image"
  2. Select a clear photo of your prescription (JPG, PNG, etc.)
  3. Click "Analyze" button
  4. Wait for the AI to extract and explain the medications

Tips for Best Results:

  • Take photos in good lighting
  • Ensure text is clear and readable
  • Avoid shadows or glare
  • Keep the image straight and in focus

3. Clearing Chat History

Click the "Clear History" button in the top-right corner to delete all conversations.

Project Structure

Medical-Chatbot/
├── app.py                    # Main Flask application
├── db.py                     # Database operations
├── medical_assistant.py      # AI response generation
├── prescription_handler.py   # OCR and prescription processing
├── llm.py                    # Legacy compatibility layer
├── requirements.txt          # Python dependencies
├── .env                      # Environment configuration
├── .env.example             # Example environment file
├── chat_history.db          # SQLite database (created automatically)
├── templates/
│   └── index.html           # Frontend interface
└── uploads/                 # Uploaded prescription images

Troubleshooting

"No module named X"

pip install -r requirements.txt --force-reinstall

"GOOGLE_API_KEY not found"

  • Check that .env file exists
  • Verify API key is correct
  • Make sure no extra spaces in .env file

Tesseract Not Found Error

# Edit prescription_handler.py and uncomment this line (Windows):
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

Port Already in Use

Change the port in .env:

PORT=8000

Prescription Upload Not Working

  • Verify Tesseract is installed: tesseract --version
  • Check image file is a supported format
  • Ensure image text is clear and readable

AI Responses Are Slow

  • This is normal - AI processing takes a few seconds
  • Check your internet connection
  • Verify Google API key has sufficient quota

Development

Adding New Features

The codebase is modular:

  • medical_assistant.py - Modify AI prompts and response logic
  • prescription_handler.py - Enhance OCR or parsing algorithms
  • templates/index.html - Update UI and styling
  • app.py - Add new API endpoints

Running in Debug Mode

Debug mode is enabled by default. To disable:

# In app.py, change:
app.run(debug=False, host='0.0.0.0', port=port)

Security Notes

  • Never commit .env file to version control
  • Keep your Google API key private
  • Uploaded files are stored in uploads/ folder
  • Database file contains all chat history
  • Consider implementing user authentication for production use

API Endpoints

  • GET / - Main chat interface
  • POST /api/chat - Send text message
  • POST /api/upload-prescription - Upload prescription image
  • POST /api/clear-history - Clear chat history

Dependencies

  • Flask - Web framework
  • langchain - LLM integration
  • langchain-google-genai - Google Gemini AI
  • pytesseract - OCR for image text extraction
  • Pillow - Image processing
  • python-dotenv - Environment variable management

Limitations

  • Prescription OCR accuracy depends on image quality
  • AI responses are general information only
  • Requires active internet connection
  • Google API has rate limits (check your quota)

Future Enhancements

Potential improvements:

  • User authentication system
  • Export chat history to PDF
  • Multi-language support
  • Voice input/output
  • Medication reminder system
  • Integration with pharmacy databases

License

This project is for educational purposes. Always consult healthcare professionals for medical advice.

Support

If you encounter issues:

  1. Check this README's troubleshooting section
  2. Verify all prerequisites are installed
  3. Review error messages in terminal
  4. Check that API key is valid and has quota

Disclaimer

This medical chatbot is an informational tool and should not be used as a substitute for professional medical advice, diagnosis, or treatment. Always seek the advice of your physician or other qualified health provider with any questions you may have regarding a medical condition.


Made with ❤️ for better health awareness

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages