Skip to content

Repository files navigation

Medical Diagnostics AI Agent

Sharan G S

GitHub

An advanced AI-powered system for analyzing complex medical cases using specialized LLM-based agents. This project uses free, open-source APIs (Ollama and Hugging Face) to provide intelligent medical case analysis, symptom evaluation, and diagnostic suggestions.

⚠️ Medical Disclaimer ⚠️

This system is for educational and informational purposes only. It is NOT a substitute for professional medical advice, diagnosis, or treatment. Always seek the advice of qualified healthcare providers. If you have a medical emergency, call emergency services immediately.

✨ Features

  • 🏥 Comprehensive Case Analysis - Multi-step reasoning for complex medical cases
  • 🔍 Symptom Extraction - Automatic categorization and analysis of symptoms
  • 💊 Differential Diagnosis - AI-generated diagnostic suggestions with confidence levels
  • 💬 Interactive Chat - Conversational interface for medical questions
  • 📚 PubMed Integration - Access to peer-reviewed medical literature and research citations
  • 🌐 Multi-Language Support - Interface and responses in Tamil and English
  • 📄 PDF Export - Download analysis reports as professional PDF documents
  • 🎤 Voice Input Support - Dictate medical cases using speech-to-text
  • 🖼️ Medical Imaging Analysis - Analyze medical images (X-rays, CT scans) for quality and structure
  • 🧠 Enhanced RAG System - Vector-based medical knowledge retrieval with semantic search
  • 🌐 Web Interface - Modern, responsive web UI
  • 🖥️ CLI Interface - Rich command-line interface for power users
  • 📚 Knowledge Base - Medical knowledge integration with disease-symptom mappings
  • 🔄 Automatic Fallback - Seamless switching between Ollama and Hugging Face APIs

🚀 Quick Start

Prerequisites

  • Python 3.8 or higher
  • Ollama (recommended) or Hugging Face API key

Installation

  1. Clone or download the project

    cd /Users/sharan/Downloads/AI-Agent-for-Medical-Diagnostics
  2. Create a virtual environment

    python3 -m venv venv
    source venv/bin/activate  # On macOS/Linux
  3. Install dependencies

    pip install -r requirements.txt
  4. Set up Ollama (Primary LLM - Recommended)

    # Install Ollama
    brew install ollama  # macOS
    
    # Start Ollama service
    ollama serve
    
    # In a new terminal, pull a model
    ollama pull llama3.2
    # or
    ollama pull mistral
  5. Configure environment variables

    cp .env.example .env
    # Edit .env and add your Hugging Face API key (optional, for fallback)

Running the Application

Web Interface (Recommended)

python app.py

Then open your browser to: http://localhost:5000

CLI Interface

# Interactive mode
python cli.py interactive

# Chat mode
python cli.py chat

# Analyze from file
python cli.py analyze-file case.txt

# Check system status
python cli.py status

📖 Usage Examples

Web Interface

  1. Navigate to http://localhost:5000
  2. Enter a medical case description in the "Case Analysis" section
  3. Click "Analyze Case" to get comprehensive analysis
  4. Use the "Interactive Consultation" chat for follow-up questions

CLI Interface

# Start interactive session
python cli.py interactive

# Example case input:
Patient: 45-year-old male
Symptoms: Persistent cough for 3 weeks, fever (101°F), night sweats, fatigue
Duration: 3 weeks
Medical History: No significant past medical history

🏗️ Project Structure

AI-Agent-for-Medical-Diagnostics/
├── agents/
│   ├── medical_agent.py      # Main medical AI agent
│   ├── llm_provider.py        # LLM abstraction layer
│   └── prompts.py             # Specialized medical prompts
├── config/
│   └── settings.py            # Configuration management
├── knowledge/
│   ├── medical_kb.py          # Medical knowledge base
│   └── sample_data.json       # Disease and symptom data
├── static/
│   ├── css/style.css          # Modern UI styling
│   └── js/app.js              # Frontend JavaScript
├── templates/
│   └── index.html             # Web interface
├── utils/
│   ├── validators.py          # Input validation
│   └── logger.py              # Logging utilities
├── app.py                     # Flask web application
├── cli.py                     # Command-line interface
├── requirements.txt           # Python dependencies
└── README.md                  # This file

🔧 Configuration

Environment Variables

Edit .env file:

# Ollama (Primary)
OLLAMA_BASE_URL=http://localhost:11434
OLLAMA_MODEL=llama3.2

# Hugging Face (Fallback)
HUGGINGFACE_API_KEY=your_key_here
HUGGINGFACE_MODEL=microsoft/BioGPT

# Flask
FLASK_PORT=5000
FLASK_DEBUG=True

Getting a Free Hugging Face API Key

  1. Go to https://huggingface.co/join
  2. Create a free account
  3. Navigate to Settings → Access Tokens
  4. Create a new token
  5. Add it to your .env file

🎯 Key Components

Medical Agent

The core AI agent (agents/medical_agent.py) provides:

  • Case analysis with multi-step reasoning
  • Symptom extraction and categorization
  • Differential diagnosis generation
  • Treatment suggestions
  • Interactive chat capabilities

LLM Provider

Abstraction layer (agents/llm_provider.py) supporting:

  • Ollama - Local, unlimited, free inference
  • Hugging Face - Cloud-based fallback
  • Automatic failover between providers

Knowledge Base

Medical knowledge system (knowledge/medical_kb.py) with:

  • Disease-symptom mappings
  • Red flag detection
  • Symptom categorization

🧪 Testing

Run the application with sample cases from examples/sample_cases.md:

# Web interface
python app.py

# CLI
python cli.py interactive

🛠️ Troubleshooting

Ollama Connection Issues

# Check if Ollama is running
curl http://localhost:11434/api/tags

# Start Ollama
ollama serve

# Pull a model if not already done
ollama pull llama3.2

Hugging Face API Issues

  • Verify your API key in .env
  • Check rate limits (free tier has limits)
  • Ensure model name is correct

Port Already in Use

# Change port in .env
FLASK_PORT=5001

📝 Sample Cases

See examples/sample_cases.md for example medical cases to test with.

🤝 Contributing

This is an educational project. Feel free to:

  • Report issues
  • Suggest improvements
  • Add more medical knowledge to the knowledge base
  • Improve prompts for better analysis

📄 License

This project is for educational purposes. Please ensure compliance with medical regulations in your jurisdiction.

🔗 Resources


Remember: This tool is for educational purposes only. Always consult with qualified healthcare professionals for medical advice.


Made with love 💚 from Sharan G S

About

Medical Diagnostics AI Agent project, The system uses Ollama (local, completely free) and Hugging Face's free tier as LLM backends.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages