BioGPT-chatbot / app.py
Blaiseboy's picture
Upload app.py
1e7b118 verified
raw
history blame
30.2 kB
import os
import re
import torch
import warnings
import numpy as np
import gradio as gr
from transformers import (
AutoTokenizer,
AutoModelForCausalLM,
BitsAndBytesConfig
)
from sentence_transformers import SentenceTransformer
from typing import List, Dict, Optional
import time
from datetime import datetime
# Suppress warnings
warnings.filterwarnings('ignore')
class BioGPTMedicalChatbot:
def __init__(self):
"""Initialize BioGPT chatbot for Gradio deployment"""
print("🏥 Initializing BioGPT Medical Chatbot...")
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.use_8bit = torch.cuda.is_available()
print(f"🖥️ Using device: {self.device}")
# Setup components
self.setup_embeddings()
self.setup_biogpt()
# Knowledge base and conversation tracking
self.knowledge_chunks = []
self.conversation_history = []
# Load default medical knowledge
self.load_default_medical_knowledge()
print("✅ BioGPT Medical Chatbot ready!")
def setup_embeddings(self):
"""Setup medical embeddings"""
try:
print("🔧 Loading embeddings...")
self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
self.use_embeddings = True
print("✅ Embeddings loaded successfully")
except Exception as e:
print(f"⚠️ Embeddings failed: {e}")
self.embedding_model = None
self.use_embeddings = False
def setup_biogpt(self):
"""Setup BioGPT model with fallback"""
print("🧠 Loading BioGPT model...")
# Try BioGPT first, then fallback to smaller model
models_to_try = [
"microsoft/BioGPT-Large",
"microsoft/BioGPT",
"microsoft/DialoGPT-medium"
]
for model_name in models_to_try:
try:
print(f" Trying {model_name}...")
# Setup quantization for memory efficiency
quantization_config = None
if self.use_8bit and "BioGPT" in model_name:
quantization_config = BitsAndBytesConfig(
load_in_8bit=True,
llm_int8_threshold=6.0,
)
# Load tokenizer
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
if self.tokenizer.pad_token is None:
self.tokenizer.pad_token = self.tokenizer.eos_token
# Load model
self.model = AutoModelForCausalLM.from_pretrained(
model_name,
quantization_config=quantization_config,
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
device_map="auto" if self.device == "cuda" and quantization_config else None,
trust_remote_code=True
)
if self.device == "cuda" and quantization_config is None:
self.model = self.model.to(self.device)
print(f"✅ Successfully loaded {model_name}!")
self.model_name = model_name
return
except Exception as e:
print(f"❌ Failed to load {model_name}: {e}")
continue
# If all models fail
print("❌ All models failed to load")
self.model = None
self.tokenizer = None
self.model_name = "None"
def load_default_medical_knowledge(self):
"""Load comprehensive medical knowledge base"""
default_knowledge = [
{
'id': 0,
'text': "Fever in children is commonly caused by viral infections (most common), bacterial infections, immunizations, teething in infants, or overdressing. Normal body temperature ranges from 97°F to 100.4°F (36.1°C to 38°C). A fever is generally considered when oral temperature exceeds 100.4°F (38°C). Most fevers are not dangerous and help the body fight infection. Treatment includes rest, fluids, and fever reducers like acetaminophen or ibuprofen for comfort.",
'medical_focus': 'pediatric_symptoms'
},
{
'id': 1,
'text': "Dehydration in infants and children can occur rapidly, especially during illness with vomiting or diarrhea. Warning signs include: dry mouth and tongue, decreased urination, lethargy or irritability, sunken eyes, and in infants under 12 months, sunken fontanelle (soft spot). Mild dehydration can be treated with oral rehydration solutions or clear fluids. Severe dehydration requires immediate medical attention.",
'medical_focus': 'pediatric_symptoms'
},
{
'id': 2,
'text': "Common cold symptoms in children include runny or stuffy nose, cough, low-grade fever, sneezing, and general fussiness. Most colds are viral and resolve within 7-10 days without specific treatment. Treatment focuses on comfort measures: rest, adequate fluids, humidified air, and saline nasal drops for congestion. Antibiotics are not effective against viral colds.",
'medical_focus': 'pediatric_symptoms'
},
{
'id': 3,
'text': "Emergency warning signs in children requiring immediate medical attention include: severe difficulty breathing, persistent high fever over 104°F (40°C), signs of severe dehydration, persistent vomiting preventing fluid intake, severe headache with neck stiffness, altered consciousness or extreme lethargy, severe abdominal pain, or any concerning change in behavior. When in doubt, seek medical care.",
'medical_focus': 'emergency'
},
{
'id': 4,
'text': "Childhood vaccination schedules protect against serious diseases including measles, mumps, rubella, polio, hepatitis B, Haemophilus influenzae, pneumococcal disease, and others. Vaccines are rigorously tested for safety and effectiveness. Side effects are typically mild, such as low-grade fever or soreness at injection site. Following recommended vaccination schedules protects individual children and the community.",
'medical_focus': 'prevention'
},
{
'id': 5,
'text': "Persistent cough in children can be caused by viral upper respiratory infections, asthma, allergies, bacterial infections, or irritants. Most coughs from colds resolve within 2-3 weeks. Seek medical evaluation for coughs lasting more than 3 weeks, coughs with blood, difficulty breathing, or coughs severely interfering with sleep. Treatment depends on the underlying cause.",
'medical_focus': 'pediatric_symptoms'
},
{
'id': 6,
'text': "Skin rashes in children have many causes including viral infections (roseola, fifth disease), bacterial infections, eczema, allergic reactions, heat rash, or contact dermatitis. Most viral rashes are harmless and resolve on their own. Seek medical attention for rashes with high fever, rapidly spreading rashes, blistering, or signs of infection like pus or red streaking.",
'medical_focus': 'pediatric_symptoms'
},
{
'id': 7,
'text': "Stomach pain and abdominal pain in children can result from constipation, gas, viral gastroenteritis, food intolerance, appendicitis, or emotional stress. Most stomach aches are mild and resolve quickly with rest and clear fluids. Warning signs requiring immediate medical attention include severe pain, persistent vomiting, fever with abdominal pain, or pain preventing normal activities.",
'medical_focus': 'pediatric_symptoms'
},
{
'id': 8,
'text': "Sleep problems in children may include difficulty falling asleep, frequent night wakings, early morning awakening, or nightmares. Good sleep hygiene includes consistent bedtime routines, appropriate sleep environment (cool, dark, quiet), limiting screen time before bed, adequate physical activity during the day, and avoiding caffeine. Most sleep issues improve with consistent routines.",
'medical_focus': 'general_medical'
},
{
'id': 9,
'text': "Nutrition for children should include a variety of foods from all food groups: fruits, vegetables, whole grains, lean protein sources, and dairy or dairy alternatives. Limit processed foods, sugary drinks, and excessive snacks. Breastfeeding is recommended for infants for the first 6 months, with introduction of solid foods around 6 months while continuing breastfeeding.",
'medical_focus': 'prevention'
},
{
'id': 10,
'text': "Vomiting in children can be caused by viral gastroenteritis (stomach flu), food poisoning, motion sickness, or other illnesses. Most episodes are brief and resolve within 24-48 hours. Focus on preventing dehydration with small, frequent sips of clear fluids. Seek medical care for persistent vomiting, signs of dehydration, blood in vomit, or severe abdominal pain.",
'medical_focus': 'pediatric_symptoms'
},
{
'id': 11,
'text': "Diarrhea in children is often caused by viral infections, bacterial infections, food intolerance, or medications. Most cases resolve within a few days. Prevention of dehydration is key - offer clear fluids and oral rehydration solutions. Seek medical attention for bloody stools, signs of dehydration, persistent high fever, or diarrhea lasting more than a week.",
'medical_focus': 'pediatric_symptoms'
},
{
'id': 12,
'text': "Breathing difficulties in children can range from mild congestion to serious respiratory distress. Signs of serious breathing problems include rapid breathing, retractions (pulling in around ribs), wheezing, blue lips or fingernails, or extreme difficulty speaking. Mild congestion can be helped with humidified air and saline drops. Severe breathing difficulties require immediate medical attention.",
'medical_focus': 'emergency'
},
{
'id': 13,
'text': "Common childhood injuries include cuts, scrapes, bruises, and minor burns. Basic first aid includes cleaning wounds with soap and water, applying pressure to stop bleeding, and covering with clean bandages. Seek medical care for deep cuts requiring stitches, burns larger than a quarter, head injuries with loss of consciousness, or any injury causing severe pain or inability to move normally.",
'medical_focus': 'emergency'
},
{
'id': 14,
'text': "Ear infections are common in children and can cause ear pain, fever, irritability, and sometimes drainage from the ear. Many ear infections resolve on their own, but some require antibiotic treatment. Pain can be managed with appropriate pain relievers. Seek medical evaluation for severe ear pain, high fever, or symptoms lasting more than 2-3 days.",
'medical_focus': 'pediatric_symptoms'
}
]
self.knowledge_chunks = default_knowledge
print(f"📚 Loaded {len(default_knowledge)} comprehensive medical knowledge chunks")
def retrieve_medical_context(self, query: str, n_results: int = 3) -> List[str]:
"""Retrieve relevant medical context using improved keyword search"""
if not self.knowledge_chunks:
return []
query_words = set(query.lower().split())
chunk_scores = []
# Enhanced medical keyword mapping
medical_keywords = {
'fever': ['fever', 'temperature', 'hot', 'warm', 'burning'],
'cough': ['cough', 'coughing', 'respiratory', 'breathing'],
'stomach': ['stomach', 'abdominal', 'belly', 'tummy', 'pain', 'ache'],
'rash': ['rash', 'skin', 'red', 'spots', 'bumps', 'itchy'],
'vomiting': ['vomit', 'vomiting', 'throw up', 'sick', 'nausea'],
'diarrhea': ['diarrhea', 'loose', 'stool', 'bowel', 'poop'],
'dehydration': ['dehydration', 'dehydrated', 'fluids', 'water', 'thirsty'],
'breathing': ['breathing', 'breath', 'respiratory', 'lungs', 'airways'],
'emergency': ['emergency', 'urgent', 'serious', 'severe', 'hospital', 'doctor'],
'cold': ['cold', 'runny nose', 'congestion', 'sneezing'],
'sleep': ['sleep', 'sleeping', 'bedtime', 'insomnia', 'tired'],
'nutrition': ['nutrition', 'eating', 'food', 'diet', 'feeding'],
'vaccination': ['vaccine', 'vaccination', 'immunization', 'shot'],
'injury': ['injury', 'hurt', 'cut', 'burn', 'bruise', 'accident'],
'ear': ['ear', 'hearing', 'earache', 'infection']
}
# Expand query with related medical terms
expanded_query_words = set(query_words)
for medical_term, synonyms in medical_keywords.items():
if any(word in query_lower for word in synonyms):
expanded_query_words.update(synonyms)
for chunk_info in self.knowledge_chunks:
chunk_text = chunk_info['text'].lower()
# Calculate relevance score with expanded terms
word_overlap = sum(1 for word in expanded_query_words if word in chunk_text)
base_score = word_overlap / len(expanded_query_words) if expanded_query_words else 0
# Strong boost for medical focus alignment
medical_boost = 0
medical_focus = chunk_info.get('medical_focus', '')
if medical_focus == 'pediatric_symptoms':
medical_boost = 0.5
elif medical_focus == 'emergency':
medical_boost = 0.4
elif medical_focus in ['treatments', 'diagnosis']:
medical_boost = 0.3
elif medical_focus == 'prevention':
medical_boost = 0.2
final_score = base_score + medical_boost
if final_score > 0:
chunk_scores.append((final_score, chunk_info['text']))
# Return top matches - ensure we always have at least some context
chunk_scores.sort(reverse=True)
results = [chunk for _, chunk in chunk_scores[:n_results]]
# If no good matches, return some default medical chunks
if not results:
results = [chunk['text'] for chunk in self.knowledge_chunks[:2]]
return results
def generate_biogpt_response(self, context: str, query: str) -> str:
"""Generate medical response using BioGPT"""
if not self.model or not self.tokenizer:
return "Medical model not available. Please try again later."
try:
# Create medical prompt
prompt = f"""Medical Context: {context[:800]}
Question: {query}
Medical Answer:"""
# Tokenize input
inputs = self.tokenizer(
prompt,
return_tensors="pt",
truncation=True,
max_length=1024
)
# Move inputs to device
if self.device == "cuda":
inputs = {k: v.to(self.device) for k, v in inputs.items()}
# Generate response
with torch.no_grad():
outputs = self.model.generate(
**inputs,
max_new_tokens=150,
do_sample=True,
temperature=0.7,
top_p=0.9,
pad_token_id=self.tokenizer.eos_token_id,
repetition_penalty=1.1
)
# Decode response
full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract generated part
if "Medical Answer:" in full_response:
generated_response = full_response.split("Medical Answer:")[-1].strip()
else:
generated_response = full_response[len(prompt):].strip()
return self.clean_medical_response(generated_response)
except Exception as e:
print(f"⚠️ BioGPT generation failed: {e}")
return self.fallback_response(context, query)
def clean_medical_response(self, response: str) -> str:
"""Clean medical response"""
sentences = re.split(r'[.!?]+', response)
clean_sentences = []
for sentence in sentences:
sentence = sentence.strip()
if len(sentence) > 10:
clean_sentences.append(sentence)
if len(clean_sentences) >= 3:
break
if clean_sentences:
cleaned = '. '.join(clean_sentences) + '.'
else:
cleaned = response[:200] + '...' if len(response) > 200 else response
return cleaned
def fallback_response(self, context: str, query: str) -> str:
"""Fallback response when model fails"""
sentences = [s.strip() for s in context.split('.') if len(s.strip()) > 20]
if sentences:
response = sentences[0] + '.'
if len(sentences) > 1:
response += ' ' + sentences[1] + '.'
else:
response = context[:300] + '...'
return response
def handle_conversational_interactions(self, query: str) -> Optional[str]:
"""Handle conversational interactions"""
query_lower = query.lower().strip()
# Greetings
greeting_patterns = [
r'^\s*(hello|hi|hey)\s*,
r'^\s*(good morning|good afternoon|good evening)\s*,
r'^\s*(how are you)\s*
]
for pattern in greeting_patterns:
if re.match(pattern, query_lower):
return "👋 Hello! I'm BioGPT, your AI medical assistant specialized in pediatric medicine. I provide evidence-based medical information. What health concern can I help you with today?"
# Thanks
if any(word in query_lower for word in ['thank you', 'thanks', 'helpful']):
return "🙏 You're welcome! I'm glad I could provide helpful medical information. Remember to always consult healthcare providers for personalized advice. Feel free to ask more questions!"
# About/Help
if any(word in query_lower for word in ['what are you', 'who are you', 'help', 'what can you do']):
return """🤖 **About BioGPT Medical Assistant**
I'm an AI medical assistant powered by BioGPT, specialized in pediatric medicine. I can help with:
🩺 **Medical Information:**
• Pediatric symptoms and conditions
• Treatment guidance
• When to seek medical care
• Prevention and wellness
⚠️ **Important:** I provide educational information only. Always consult healthcare professionals for medical decisions."""
return None
def chat_interface(self, message: str, history: List[List[str]]) -> str:
"""Main chat interface for Gradio - FIXED to prevent greeting mode stuck"""
if not message.strip():
return "Hello! I'm BioGPT, your medical AI assistant. How can I help you with pediatric medical questions today?"
print(f"🔍 Processing query: '{message}'") # Debug logging
# Handle ONLY very specific conversational interactions
conversational_response = self.handle_conversational_interactions(message)
if conversational_response:
print(" Handled as conversational") # Debug
return conversational_response
print(" Processing as medical query") # Debug
# ALWAYS try to process as medical query if not a strict greeting
context = self.retrieve_medical_context(message)
if not context:
# Even if no context found, provide a helpful medical response
return f"""🩺 **Medical Query:** {message}
⚠️ I don't have specific information about this topic in my current medical database. However, I recommend:
1. **Consult Healthcare Provider**: For personalized medical advice
2. **Emergency Signs**: If symptoms are severe, seek immediate care
3. **Reliable Sources**: Check with pediatricians for children's health concerns
**For urgent medical concerns, contact your healthcare provider or emergency services.**
💡 **Try asking about**: fever, cough, rash, dehydration, or other common pediatric symptoms."""
# Generate medical response
main_context = '\n\n'.join(context)
response = self.generate_biogpt_response(main_context, message)
# Always format as medical response
final_response = f"🩺 **Medical Information:** {response}\n\n⚠️ **Important:** This information is for educational purposes only. Always consult qualified healthcare professionals for medical diagnosis, treatment, and personalized advice."
return final_response
# Test function to verify chatbot functionality
def test_chatbot_responses():
"""Test the chatbot with various queries to ensure it's working properly"""
print("\n🧪 Testing BioGPT Chatbot Responses...")
print("=" * 50)
test_queries = [
"hello", # Should be conversational
"what causes fever", # Should be medical
"my child has a cough", # Should be medical
"help", # Should be conversational
"breathing problems in babies", # Should be medical
"thank you" # Should be conversational
]
for query in test_queries:
print(f"\n🔍 Query: '{query}'")
response = chatbot.chat_interface(query, [])
print(f"🤖 Response type: {'CONVERSATIONAL' if any(word in response for word in ['Hello!', 'welcome!', 'About BioGPT']) else 'MEDICAL'}")
print(f"📝 Response: {response[:100]}...")
print("-" * 30)
# Initialize the chatbot globally
print("🚀 Initializing BioGPT Medical Chatbot for Gradio...")
chatbot = BioGPTMedicalChatbot()
# Run tests
test_chatbot_responses()
def create_gradio_interface():
"""Create Gradio chat interface"""
# Custom CSS for medical theme
css = """
.gradio-container {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.chat-message {
background-color: #f8f9fa;
border-radius: 10px;
padding: 10px;
margin: 5px;
}
"""
with gr.Blocks(
css=css,
title="BioGPT Medical Assistant",
theme=gr.themes.Soft()
) as demo:
# Header
gr.HTML("""
<div style="text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea, #764ba2); color: white; border-radius: 10px; margin-bottom: 20px;">
<h1>🏥 BioGPT Medical Assistant</h1>
<p>Professional AI Medical Chatbot powered by BioGPT</p>
<p><strong>Specialized in Pediatric Medicine & Children's Health</strong></p>
</div>
""")
# Important disclaimer
gr.HTML("""
<div style="background-color: #fff3cd; border: 1px solid #ffeaa7; border-radius: 8px; padding: 15px; margin-bottom: 20px;">
<h3 style="color: #856404; margin-top: 0;">⚠️ Medical Disclaimer</h3>
<p style="color: #856404; margin-bottom: 0;">
This AI provides educational medical information only and is NOT a substitute for professional medical advice,
diagnosis, or treatment. Always consult qualified healthcare providers for medical decisions.
<strong>In case of medical emergency, call emergency services immediately.</strong>
</p>
</div>
""")
# Chat interface
chatbot_interface = gr.ChatInterface(
fn=chatbot.chat_interface,
title="💬 Chat with BioGPT",
description="Ask me about pediatric health, symptoms, treatments, and medical guidance.",
examples=[
"What causes fever in children?",
"How should I treat my child's cough?",
"When should I be concerned about my baby's breathing?",
"What are the signs of dehydration in infants?",
"When should I call the doctor for my child's symptoms?",
"How can I prevent common childhood illnesses?"
],
retry_btn=None,
undo_btn=None,
clear_btn="🗑️ Clear Chat",
submit_btn="🩺 Ask BioGPT",
chatbot=gr.Chatbot(
height=500,
placeholder="<div style='text-align: center; color: #666;'>Start a conversation with BioGPT Medical Assistant</div>",
show_copy_button=True,
bubble_full_width=False
)
)
# Information tabs
with gr.Tabs():
with gr.Tab("ℹ️ About"):
gr.Markdown("""
## About BioGPT Medical Assistant
This AI assistant is powered by **BioGPT**, a specialized medical language model trained on extensive medical literature.
### 🎯 Capabilities:
- **Pediatric Medicine**: Specialized in children's health
- **Symptom Analysis**: Understanding medical symptoms
- **Treatment Guidance**: Evidence-based treatment information
- **Medical Education**: Explaining medical concepts
- **Emergency Guidance**: When to seek immediate care
### 🔧 Technical Features:
- **Model**: Microsoft BioGPT (Medical AI)
- **Specialization**: Medical and biomedical text
- **Knowledge**: Based on medical literature and research
- **Optimization**: Memory-efficient deployment
### 📱 How to Use:
1. Type your medical question in the chat
2. Be specific about symptoms or concerns
3. Ask about pediatric health topics
4. Request guidance on when to seek care
""")
with gr.Tab("🩺 Medical Topics"):
gr.Markdown("""
## Supported Medical Topics
### 👶 Pediatric Specialties:
- **Common Symptoms**: Fever, cough, rash, vomiting, diarrhea
- **Respiratory**: Breathing issues, asthma, colds
- **Digestive**: Stomach problems, feeding issues
- **Skin**: Rashes, eczema, allergic reactions
- **Development**: Growth and developmental concerns
### 🚨 Emergency Guidance:
- When to call emergency services
- Signs requiring immediate medical attention
- First aid basics for children
### 💊 Treatment Information:
- Evidence-based treatment options
- Home care remedies
- Safety considerations
- Recovery expectations
### 🛡️ Prevention:
- Vaccination information
- Disease prevention
- Healthy habits for children
- Safety measures
""")
with gr.Tab("⚠️ Safety & Limitations"):
gr.Markdown("""
## Important Safety Information
### 🚨 Emergency Situations - Call Emergency Services:
- Difficulty breathing or choking
- Severe allergic reactions
- Unconsciousness or unresponsiveness
- Severe injuries or accidents
- Persistent high fever (>104°F/40°C)
### 🏥 When to Consult Healthcare Providers:
- For diagnosis of medical conditions
- Before starting any treatments
- For prescription medications
- When symptoms worsen or persist
- For personalized medical advice
### 🤖 AI Limitations:
- Cannot diagnose medical conditions
- Cannot prescribe medications
- Cannot replace professional medical judgment
- May not have latest medical developments
- Should not be used for emergency situations
### 📞 Additional Resources:
- **Emergency**: Your local emergency number
- **Poison Control**: Contact local poison control
- **Pediatrician**: Your child's healthcare provider
- **Nurse Hotline**: 24/7 nurse consultations (many insurance plans)
""")
# Footer
gr.HTML("""
<div style="text-align: center; padding: 20px; margin-top: 30px; border-top: 1px solid #ddd; color: #666;">
<p>🤖 <strong>BioGPT Medical Assistant</strong> | Powered by Microsoft BioGPT</p>
<p>For educational purposes only • Always consult healthcare professionals</p>
</div>
""")
return demo
# Create and launch the interface
demo = create_gradio_interface()
if __name__ == "__main__":
# Launch the app
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
)