Skip to content

AdityaKumar41/medical-agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ”¬ AI Prescription Reader

Python FastAPI License Code Style

A production-ready AI-powered web application that reads prescription images, analyzes symptoms, and provides medicine information using OCR and LangChain with Ollama. Built with privacy-first approach - all AI processing happens locally.

Demo Screenshot

✨ Features

Core Functionality

  • πŸ“‹ Prescription Analysis: Upload prescription images and extract medicine information using advanced OCR
  • 🩺 Symptom Analysis: Describe symptoms and get AI-powered health insights and recommendations
  • πŸ’Š Medicine Information: Get detailed information about specific medicines including dosages and side effects
  • πŸ” Medicine Image Identification: Upload medicine images to identify pills, tablets, and capsules
  • πŸ“¦ Packaging Analysis: Analyze medicine packaging to extract comprehensive product information
  • πŸ“Έ Advanced OCR: Optimized text recognition from various image types and qualities

Technical Features

  • 🎨 Modern UI: Beautiful, responsive web interface with dark/light mode support
  • πŸ”’ Privacy First: All AI processing happens locally with Ollama - no data sent to external services
  • ⚑ Fast Performance: Optimized image processing and AI inference
  • πŸ§ͺ Well Tested: Comprehensive test suite with pytest
  • πŸ“± Mobile Friendly: Responsive design that works on all devices
  • πŸ”§ Production Ready: Proper logging, error handling, and configuration management

πŸš€ Quick Start

Prerequisites

  • Python 3.8+ with pip
  • Ollama - Local AI model server
  • Tesseract OCR - Text extraction from images
  • Git (for cloning)

One-Click Installation

# Clone the repository
git clone https://github.com/your-username/ai-prescription-reader.git
cd ai-prescription-reader

# Run the automated setup script
chmod +x setup.sh
./setup.sh

# Start Ollama in a separate terminal
ollama serve

# Start the application
source venv/bin/activate
python main.py

πŸŽ‰ That's it! Open your browser to http://localhost:8000

πŸ“– Manual Installation

For more control over the installation process:

1. Environment Setup

# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Upgrade pip and install dependencies
pip install --upgrade pip
pip install -r requirements.txt

2. System Dependencies

macOS (with Homebrew):

brew install tesseract

Ubuntu/Debian:

sudo apt-get update
sudo apt-get install tesseract-ocr tesseract-ocr-eng

Windows:

  • Download Tesseract from GitHub Releases
  • Add to PATH or update ocr_service.py with tesseract path

3. Ollama Setup

# Install Ollama from https://ollama.ai/download
# Pull the required model
ollama pull llama3.2

# Start Ollama server
ollama serve

4. Configuration

Copy and modify the environment file:

cp .env.example .env
# Edit .env with your preferred settings

5. Run the Application

# Development mode
python main.py

# Production mode with Gunicorn
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000

🎯 Usage Guide

Prescription Analysis

  1. Navigate to the Prescription tab
  2. Upload Image: Drag & drop or click to select prescription image (JPG, PNG, PDF)
  3. Or Enter Text: Manually type prescription text
  4. Click "Analyze Prescription"
  5. View extracted medicines, dosages, instructions, and doctor information

Symptom Analysis

  1. Go to the Symptoms tab
  2. Describe your symptoms in detail in the text area
  3. Click "Analyze Symptoms"
  4. Review possible conditions, recommendations, and when to seek medical attention

Medicine Information

  1. Switch to the Medicine Info tab
  2. Choose from three options:
    • Search by Name: Enter medicine name and get detailed information
    • Identify from Image: Upload medicine image to identify pills/tablets
    • Analyze Packaging: Upload packaging image for comprehensive analysis
  3. Click the appropriate action button
  4. View detailed results including identification, composition, and safety information

πŸ”§ API Documentation

Base URL

http://localhost:8000/api/v1

Endpoints

Prescription Analysis

POST /prescription/analyze
Content-Type: multipart/form-data

# Upload prescription image
curl -X POST "http://localhost:8000/api/v1/prescription/analyze" \
  -F "file=@prescription.jpg"
POST /prescription/analyze-text?prescription_text=<text>

# Analyze prescription text
curl -X POST "http://localhost:8000/api/v1/prescription/analyze-text?prescription_text=Paracetamol 500mg twice daily"

Symptom Analysis

POST /symptoms/analyze?symptoms=<symptoms>

# Analyze symptoms
curl -X POST "http://localhost:8000/api/v1/symptoms/analyze?symptoms=headache and fever"

Medicine Information

GET /medicine/info/{medicine_name}

# Get medicine info by name
curl "http://localhost:8000/api/v1/medicine/info/paracetamol"
POST /medicine/identify
Content-Type: multipart/form-data

# Identify medicine from image
curl -X POST "http://localhost:8000/api/v1/medicine/identify" \
  -F "file=@medicine_image.jpg"
POST /medicine/analyze-packaging
Content-Type: multipart/form-data

# Analyze medicine packaging
curl -X POST "http://localhost:8000/api/v1/medicine/analyze-packaging" \
  -F "file=@packaging_image.jpg"
POST /medicine/batch-info
Content-Type: application/json

# Get info for multiple medicines
curl -X POST "http://localhost:8000/api/v1/medicine/batch-info" \
  -H "Content-Type: application/json" \
  -d '["paracetamol", "aspirin"]'

Health Check

GET /health

# Health status
curl "http://localhost:8000/health"

Response Format

All API responses follow this structure:

{
  "status": "success|error",
  "data": {...},
  "message": "Optional message",
  "timestamp": "2025-07-07T10:30:00Z"
}

πŸ§ͺ Testing

Run Tests

# Install test dependencies
pip install pytest pytest-asyncio pytest-cov

# Run all tests
pytest

# Run with coverage
pytest --cov=app tests/

# Run specific test file
pytest test_api.py -v

Test Coverage

  • API endpoints testing
  • OCR service testing
  • AI service integration testing
  • Error handling validation

πŸš€ Production Deployment

Using Docker

# Dockerfile included in repository
docker build -t ai-prescription-reader .
docker run -p 8000:8000 ai-prescription-reader

Using Gunicorn

# Install Gunicorn
pip install gunicorn

# Run with multiple workers
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000

Environment Variables

# Production settings
ENVIRONMENT=production
DEBUG=False
OLLAMA_BASE_URL=http://localhost:11434
OLLAMA_MODEL=llama3.2
LOG_LEVEL=INFO

Security Considerations

  • File Upload Limits: Configured for prescription images only
  • Rate Limiting: Implement rate limiting for production use
  • HTTPS: Use reverse proxy (nginx) with SSL certificates
  • Input Validation: All inputs are validated and sanitized
  • Error Handling: Proper error messages without exposing internals

πŸ“ Project Structure

ai-prescription-reader/
β”œβ”€β”€ πŸ“„ main.py                    # FastAPI application entry point
β”œβ”€β”€ πŸ“„ requirements.txt           # Python dependencies
β”œβ”€β”€ πŸ“„ setup.sh                   # Automated setup script
β”œβ”€β”€ πŸ“„ .env                       # Environment configuration
β”œβ”€β”€ πŸ“„ .gitignore                 # Git ignore patterns
β”œβ”€β”€ πŸ“„ Dockerfile                 # Container configuration
β”œβ”€β”€ πŸ“„ docker-compose.yml         # Multi-service orchestration
β”œβ”€β”€ πŸ“ app/                       # Application package
β”‚   β”œβ”€β”€ πŸ“ core/
β”‚   β”‚   β”œβ”€β”€ πŸ“„ __init__.py
β”‚   β”‚   β”œβ”€β”€ πŸ“„ config.py          # Settings and configuration
β”‚   β”‚   └── πŸ“„ logging.py         # Logging configuration
β”‚   β”œβ”€β”€ πŸ“ routers/               # API route handlers
β”‚   β”‚   β”œβ”€β”€ πŸ“„ __init__.py
β”‚   β”‚   β”œβ”€β”€ πŸ“„ prescription.py    # Prescription analysis endpoints
β”‚   β”‚   β”œβ”€β”€ πŸ“„ symptoms.py        # Symptom analysis endpoints
β”‚   β”‚   └── πŸ“„ medicine.py        # Medicine information endpoints
β”‚   β”œβ”€β”€ πŸ“ services/              # Business logic services
β”‚   β”‚   β”œβ”€β”€ πŸ“„ __init__.py
β”‚   β”‚   β”œβ”€β”€ πŸ“„ ocr_service.py     # OCR text extraction
β”‚   β”‚   └── πŸ“„ ollama_langchain.py # AI analysis with LangChain
β”‚   └── πŸ“ models/                # Data models and schemas
β”‚       β”œβ”€β”€ πŸ“„ __init__.py
β”‚       └── πŸ“„ schemas.py         # Pydantic models
β”œβ”€β”€ πŸ“ static/                    # Frontend assets
β”‚   β”œβ”€β”€ πŸ“„ index.html             # Main web interface
β”‚   β”œβ”€β”€ πŸ“ css/
β”‚   β”œβ”€β”€ πŸ“ js/
β”‚   └── πŸ“ images/
β”œβ”€β”€ πŸ“ tests/                     # Test suite
β”‚   β”œβ”€β”€ πŸ“„ test_api.py            # API endpoint tests
β”‚   β”œβ”€β”€ πŸ“„ test_ocr.py            # OCR service tests
β”‚   └── πŸ“„ test_ai.py             # AI service tests
└── πŸ“ docs/                      # Documentation
    β”œβ”€β”€ πŸ“„ API.md                 # API documentation
    β”œβ”€β”€ πŸ“„ DEPLOYMENT.md          # Deployment guide
    └── πŸ“„ CONTRIBUTING.md        # Contribution guidelines

βš•οΈ Medical Disclaimer

IMPORTANT MEDICAL DISCLAIMER

This application is for informational and educational purposes only and is not a substitute for professional medical advice, diagnosis, or treatment.

⚠️ Key Points:

  • Always seek the advice of your physician or other qualified health provider
  • Never disregard professional medical advice because of information from this app
  • Never delay seeking medical treatment because of information from this app
  • This app does not provide medical diagnoses or treatment recommendations
  • The AI analysis is based on general medical knowledge and may not be accurate
  • Prescription information extracted may contain errors - always verify with healthcare providers

If you think you may have a medical emergency, call your doctor or emergency services immediately.

πŸ› οΈ Technologies & Stack

Backend

  • FastAPI - Modern, fast web framework
  • LangChain - AI/LLM application framework
  • Ollama - Local LLM hosting and inference
  • Tesseract OCR - Optical character recognition
  • OpenCV - Computer vision and image processing
  • Pillow - Python imaging library

Frontend

  • Vanilla JavaScript - No framework dependencies
  • Modern CSS - Responsive design with CSS Grid and Flexbox
  • HTML5 - Semantic markup with accessibility features

DevOps & Tools

🀝 Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

Development Setup

# Fork and clone the repository
git clone https://github.com/your-username/ai-prescription-reader.git
cd ai-prescription-reader

# Create development environment
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip install -r requirements-dev.txt

# Run tests
pytest

# Format code
black .

# Run linting
flake8 app/

Reporting Issues

  • Use GitHub Issues for bug reports and feature requests
  • Provide detailed reproduction steps
  • Include system information and logs

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Tesseract OCR team for the excellent OCR engine
  • Ollama team for making local LLM inference accessible
  • LangChain community for the powerful AI framework
  • FastAPI creators for the amazing web framework
  • Medical professionals who provided guidance on health information accuracy

πŸ“ž Support


Made with ❀️ for better healthcare accessibility

⭐ Star this repository if it helped you!

python3 -m venv venv
source venv/bin/activate
  1. Install dependencies:

    pip install --upgrade pip
    pip install -r requirements.txt
  2. Install Tesseract OCR:

    • macOS: brew install tesseract
    • Ubuntu/Debian: sudo apt-get install tesseract-ocr
    • Windows: Download from GitHub
  3. Install and setup Ollama:

    # Install Ollama from https://ollama.ai/download
    ollama pull llama3.2
    ollama serve

🎯 Usage

Prescription Analysis

  1. Go to the "Prescription" tab
  2. Upload a prescription image or enter text manually
  3. Click "Analyze Prescription"
  4. View extracted medicines, dosages, and instructions

Symptom Analysis

  1. Go to the "Symptoms" tab
  2. Describe your symptoms in detail
  3. Click "Analyze Symptoms"
  4. View possible conditions and recommendations

Medicine Information

  1. Go to the "Medicine Info" tab
  2. Enter a medicine name
  3. Click "Get Medicine Information"
  4. View uses, dosage, side effects, and warnings

πŸ”§ API Endpoints

  • POST /api/v1/prescription/analyze - Upload and analyze prescription image
  • POST /api/v1/prescription/analyze-text - Analyze prescription text
  • POST /api/v1/symptoms/analyze - Analyze symptoms
  • GET /api/v1/medicine/info/{medicine_name} - Get medicine information
  • GET /health - Health check

πŸ§ͺ Testing

Run tests with:

pytest test_api.py -v

βš•οΈ Medical Disclaimer

This application is for informational purposes only and is not 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.

πŸ› οΈ Technologies Used

  • FastAPI - Web framework
  • LangChain - AI/LLM integration
  • Ollama - Local LLM hosting
  • Tesseract OCR - Text extraction from images
  • OpenCV - Image processing
  • Pillow - Image manipulation

πŸ“ Project Structure

ai-prescription-reader/
β”œβ”€β”€ main.py              # FastAPI application
β”œβ”€β”€ requirements.txt     # Python dependencies
β”œβ”€β”€ setup.sh            # Setup script
β”œβ”€β”€ .env                # Environment configuration
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   └── config.py   # Application configuration
β”‚   β”œβ”€β”€ routers/
β”‚   β”‚   β”œβ”€β”€ prescription.py  # Prescription endpoints
β”‚   β”‚   β”œβ”€β”€ symptoms.py      # Symptom endpoints
β”‚   β”‚   └── medicine.py      # Medicine endpoints
β”‚   └── services/
β”‚       β”œβ”€β”€ ocr_service.py   # OCR functionality
β”‚       └── ollama_langchain.py  # AI analysis
β”œβ”€β”€ static/
β”‚   └── index.html      # Web interface
└── test_api.py         # API tests

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if necessary
  5. Submit a pull request

πŸ“„ License

This project is open source and available under the MIT License.

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors