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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import torch
|
4 |
+
import warnings
|
5 |
+
import numpy as np
|
6 |
+
import gradio as gr
|
7 |
+
from transformers import (
|
8 |
+
AutoTokenizer,
|
9 |
+
AutoModelForCausalLM,
|
10 |
+
BitsAndBytesConfig
|
11 |
+
)
|
12 |
+
from sentence_transformers import SentenceTransformer
|
13 |
+
from typing import List, Dict, Optional
|
14 |
+
import time
|
15 |
+
from datetime import datetime
|
16 |
+
|
17 |
+
# Suppress warnings
|
18 |
+
warnings.filterwarnings('ignore')
|
19 |
+
|
20 |
+
class BioGPTMedicalChatbot:
|
21 |
+
def __init__(self):
|
22 |
+
"""Initialize BioGPT chatbot for Gradio deployment"""
|
23 |
+
print("π₯ Initializing BioGPT Medical Chatbot...")
|
24 |
+
|
25 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
26 |
+
self.use_8bit = torch.cuda.is_available()
|
27 |
+
|
28 |
+
print(f"π₯οΈ Using device: {self.device}")
|
29 |
+
|
30 |
+
# Setup components
|
31 |
+
self.setup_embeddings()
|
32 |
+
self.setup_biogpt()
|
33 |
+
|
34 |
+
# Knowledge base and conversation tracking
|
35 |
+
self.knowledge_chunks = []
|
36 |
+
self.conversation_history = []
|
37 |
+
|
38 |
+
# Load default medical knowledge
|
39 |
+
self.load_default_medical_knowledge()
|
40 |
+
|
41 |
+
print("β
BioGPT Medical Chatbot ready!")
|
42 |
+
|
43 |
+
def setup_embeddings(self):
|
44 |
+
"""Setup medical embeddings"""
|
45 |
+
try:
|
46 |
+
print("π§ Loading embeddings...")
|
47 |
+
self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
48 |
+
self.use_embeddings = True
|
49 |
+
print("β
Embeddings loaded successfully")
|
50 |
+
except Exception as e:
|
51 |
+
print(f"β οΈ Embeddings failed: {e}")
|
52 |
+
self.embedding_model = None
|
53 |
+
self.use_embeddings = False
|
54 |
+
|
55 |
+
def setup_biogpt(self):
|
56 |
+
"""Setup BioGPT model with fallback"""
|
57 |
+
print("π§ Loading BioGPT model...")
|
58 |
+
|
59 |
+
# Try BioGPT first, then fallback to smaller model
|
60 |
+
models_to_try = [
|
61 |
+
"microsoft/BioGPT-Large",
|
62 |
+
"microsoft/BioGPT",
|
63 |
+
"microsoft/DialoGPT-medium"
|
64 |
+
]
|
65 |
+
|
66 |
+
for model_name in models_to_try:
|
67 |
+
try:
|
68 |
+
print(f" Trying {model_name}...")
|
69 |
+
|
70 |
+
# Setup quantization for memory efficiency
|
71 |
+
quantization_config = None
|
72 |
+
if self.use_8bit and "BioGPT" in model_name:
|
73 |
+
quantization_config = BitsAndBytesConfig(
|
74 |
+
load_in_8bit=True,
|
75 |
+
llm_int8_threshold=6.0,
|
76 |
+
)
|
77 |
+
|
78 |
+
# Load tokenizer
|
79 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
80 |
+
if self.tokenizer.pad_token is None:
|
81 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
82 |
+
|
83 |
+
# Load model
|
84 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
85 |
+
model_name,
|
86 |
+
quantization_config=quantization_config,
|
87 |
+
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
88 |
+
device_map="auto" if self.device == "cuda" and quantization_config else None,
|
89 |
+
trust_remote_code=True
|
90 |
+
)
|
91 |
+
|
92 |
+
if self.device == "cuda" and quantization_config is None:
|
93 |
+
self.model = self.model.to(self.device)
|
94 |
+
|
95 |
+
print(f"β
Successfully loaded {model_name}!")
|
96 |
+
self.model_name = model_name
|
97 |
+
return
|
98 |
+
|
99 |
+
except Exception as e:
|
100 |
+
print(f"β Failed to load {model_name}: {e}")
|
101 |
+
continue
|
102 |
+
|
103 |
+
# If all models fail
|
104 |
+
print("β All models failed to load")
|
105 |
+
self.model = None
|
106 |
+
self.tokenizer = None
|
107 |
+
self.model_name = "None"
|
108 |
+
|
109 |
+
def load_default_medical_knowledge(self):
|
110 |
+
"""Load comprehensive medical knowledge base"""
|
111 |
+
default_knowledge = [
|
112 |
+
{
|
113 |
+
'id': 0,
|
114 |
+
'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.",
|
115 |
+
'medical_focus': 'pediatric_symptoms'
|
116 |
+
},
|
117 |
+
{
|
118 |
+
'id': 1,
|
119 |
+
'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.",
|
120 |
+
'medical_focus': 'pediatric_symptoms'
|
121 |
+
},
|
122 |
+
{
|
123 |
+
'id': 2,
|
124 |
+
'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.",
|
125 |
+
'medical_focus': 'pediatric_symptoms'
|
126 |
+
},
|
127 |
+
{
|
128 |
+
'id': 3,
|
129 |
+
'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.",
|
130 |
+
'medical_focus': 'emergency'
|
131 |
+
},
|
132 |
+
{
|
133 |
+
'id': 4,
|
134 |
+
'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.",
|
135 |
+
'medical_focus': 'prevention'
|
136 |
+
},
|
137 |
+
{
|
138 |
+
'id': 5,
|
139 |
+
'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.",
|
140 |
+
'medical_focus': 'pediatric_symptoms'
|
141 |
+
},
|
142 |
+
{
|
143 |
+
'id': 6,
|
144 |
+
'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.",
|
145 |
+
'medical_focus': 'pediatric_symptoms'
|
146 |
+
},
|
147 |
+
{
|
148 |
+
'id': 7,
|
149 |
+
'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.",
|
150 |
+
'medical_focus': 'pediatric_symptoms'
|
151 |
+
},
|
152 |
+
{
|
153 |
+
'id': 8,
|
154 |
+
'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.",
|
155 |
+
'medical_focus': 'general_medical'
|
156 |
+
},
|
157 |
+
{
|
158 |
+
'id': 9,
|
159 |
+
'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.",
|
160 |
+
'medical_focus': 'prevention'
|
161 |
+
},
|
162 |
+
{
|
163 |
+
'id': 10,
|
164 |
+
'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.",
|
165 |
+
'medical_focus': 'pediatric_symptoms'
|
166 |
+
},
|
167 |
+
{
|
168 |
+
'id': 11,
|
169 |
+
'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.",
|
170 |
+
'medical_focus': 'pediatric_symptoms'
|
171 |
+
},
|
172 |
+
{
|
173 |
+
'id': 12,
|
174 |
+
'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.",
|
175 |
+
'medical_focus': 'emergency'
|
176 |
+
},
|
177 |
+
{
|
178 |
+
'id': 13,
|
179 |
+
'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.",
|
180 |
+
'medical_focus': 'emergency'
|
181 |
+
},
|
182 |
+
{
|
183 |
+
'id': 14,
|
184 |
+
'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.",
|
185 |
+
'medical_focus': 'pediatric_symptoms'
|
186 |
+
}
|
187 |
+
]
|
188 |
+
|
189 |
+
self.knowledge_chunks = default_knowledge
|
190 |
+
print(f"π Loaded {len(default_knowledge)} comprehensive medical knowledge chunks")
|
191 |
+
|
192 |
+
def retrieve_medical_context(self, query: str, n_results: int = 3) -> List[str]:
|
193 |
+
"""Retrieve relevant medical context using improved keyword search"""
|
194 |
+
if not self.knowledge_chunks:
|
195 |
+
return []
|
196 |
+
|
197 |
+
query_words = set(query.lower().split())
|
198 |
+
chunk_scores = []
|
199 |
+
|
200 |
+
# Enhanced medical keyword mapping
|
201 |
+
medical_keywords = {
|
202 |
+
'fever': ['fever', 'temperature', 'hot', 'warm', 'burning'],
|
203 |
+
'cough': ['cough', 'coughing', 'respiratory', 'breathing'],
|
204 |
+
'stomach': ['stomach', 'abdominal', 'belly', 'tummy', 'pain', 'ache'],
|
205 |
+
'rash': ['rash', 'skin', 'red', 'spots', 'bumps', 'itchy'],
|
206 |
+
'vomiting': ['vomit', 'vomiting', 'throw up', 'sick', 'nausea'],
|
207 |
+
'diarrhea': ['diarrhea', 'loose', 'stool', 'bowel', 'poop'],
|
208 |
+
'dehydration': ['dehydration', 'dehydrated', 'fluids', 'water', 'thirsty'],
|
209 |
+
'breathing': ['breathing', 'breath', 'respiratory', 'lungs', 'airways'],
|
210 |
+
'emergency': ['emergency', 'urgent', 'serious', 'severe', 'hospital', 'doctor'],
|
211 |
+
'cold': ['cold', 'runny nose', 'congestion', 'sneezing'],
|
212 |
+
'sleep': ['sleep', 'sleeping', 'bedtime', 'insomnia', 'tired'],
|
213 |
+
'nutrition': ['nutrition', 'eating', 'food', 'diet', 'feeding'],
|
214 |
+
'vaccination': ['vaccine', 'vaccination', 'immunization', 'shot'],
|
215 |
+
'injury': ['injury', 'hurt', 'cut', 'burn', 'bruise', 'accident'],
|
216 |
+
'ear': ['ear', 'hearing', 'earache', 'infection']
|
217 |
+
}
|
218 |
+
|
219 |
+
# Expand query with related medical terms
|
220 |
+
expanded_query_words = set(query_words)
|
221 |
+
for medical_term, synonyms in medical_keywords.items():
|
222 |
+
if any(word in query_lower for word in synonyms):
|
223 |
+
expanded_query_words.update(synonyms)
|
224 |
+
|
225 |
+
for chunk_info in self.knowledge_chunks:
|
226 |
+
chunk_text = chunk_info['text'].lower()
|
227 |
+
|
228 |
+
# Calculate relevance score with expanded terms
|
229 |
+
word_overlap = sum(1 for word in expanded_query_words if word in chunk_text)
|
230 |
+
base_score = word_overlap / len(expanded_query_words) if expanded_query_words else 0
|
231 |
+
|
232 |
+
# Strong boost for medical focus alignment
|
233 |
+
medical_boost = 0
|
234 |
+
medical_focus = chunk_info.get('medical_focus', '')
|
235 |
+
|
236 |
+
if medical_focus == 'pediatric_symptoms':
|
237 |
+
medical_boost = 0.5
|
238 |
+
elif medical_focus == 'emergency':
|
239 |
+
medical_boost = 0.4
|
240 |
+
elif medical_focus in ['treatments', 'diagnosis']:
|
241 |
+
medical_boost = 0.3
|
242 |
+
elif medical_focus == 'prevention':
|
243 |
+
medical_boost = 0.2
|
244 |
+
|
245 |
+
final_score = base_score + medical_boost
|
246 |
+
|
247 |
+
if final_score > 0:
|
248 |
+
chunk_scores.append((final_score, chunk_info['text']))
|
249 |
+
|
250 |
+
# Return top matches - ensure we always have at least some context
|
251 |
+
chunk_scores.sort(reverse=True)
|
252 |
+
results = [chunk for _, chunk in chunk_scores[:n_results]]
|
253 |
+
|
254 |
+
# If no good matches, return some default medical chunks
|
255 |
+
if not results:
|
256 |
+
results = [chunk['text'] for chunk in self.knowledge_chunks[:2]]
|
257 |
+
|
258 |
+
return results
|
259 |
+
|
260 |
+
def generate_biogpt_response(self, context: str, query: str) -> str:
|
261 |
+
"""Generate medical response using BioGPT"""
|
262 |
+
if not self.model or not self.tokenizer:
|
263 |
+
return "Medical model not available. Please try again later."
|
264 |
+
|
265 |
+
try:
|
266 |
+
# Create medical prompt
|
267 |
+
prompt = f"""Medical Context: {context[:800]}
|
268 |
+
|
269 |
+
Question: {query}
|
270 |
+
|
271 |
+
Medical Answer:"""
|
272 |
+
|
273 |
+
# Tokenize input
|
274 |
+
inputs = self.tokenizer(
|
275 |
+
prompt,
|
276 |
+
return_tensors="pt",
|
277 |
+
truncation=True,
|
278 |
+
max_length=1024
|
279 |
+
)
|
280 |
+
|
281 |
+
# Move inputs to device
|
282 |
+
if self.device == "cuda":
|
283 |
+
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
284 |
+
|
285 |
+
# Generate response
|
286 |
+
with torch.no_grad():
|
287 |
+
outputs = self.model.generate(
|
288 |
+
**inputs,
|
289 |
+
max_new_tokens=150,
|
290 |
+
do_sample=True,
|
291 |
+
temperature=0.7,
|
292 |
+
top_p=0.9,
|
293 |
+
pad_token_id=self.tokenizer.eos_token_id,
|
294 |
+
repetition_penalty=1.1
|
295 |
+
)
|
296 |
+
|
297 |
+
# Decode response
|
298 |
+
full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
299 |
+
|
300 |
+
# Extract generated part
|
301 |
+
if "Medical Answer:" in full_response:
|
302 |
+
generated_response = full_response.split("Medical Answer:")[-1].strip()
|
303 |
+
else:
|
304 |
+
generated_response = full_response[len(prompt):].strip()
|
305 |
+
|
306 |
+
return self.clean_medical_response(generated_response)
|
307 |
+
|
308 |
+
except Exception as e:
|
309 |
+
print(f"β οΈ BioGPT generation failed: {e}")
|
310 |
+
return self.fallback_response(context, query)
|
311 |
+
|
312 |
+
def clean_medical_response(self, response: str) -> str:
|
313 |
+
"""Clean medical response"""
|
314 |
+
sentences = re.split(r'[.!?]+', response)
|
315 |
+
clean_sentences = []
|
316 |
+
|
317 |
+
for sentence in sentences:
|
318 |
+
sentence = sentence.strip()
|
319 |
+
if len(sentence) > 10:
|
320 |
+
clean_sentences.append(sentence)
|
321 |
+
if len(clean_sentences) >= 3:
|
322 |
+
break
|
323 |
+
|
324 |
+
if clean_sentences:
|
325 |
+
cleaned = '. '.join(clean_sentences) + '.'
|
326 |
+
else:
|
327 |
+
cleaned = response[:200] + '...' if len(response) > 200 else response
|
328 |
+
|
329 |
+
return cleaned
|
330 |
+
|
331 |
+
def fallback_response(self, context: str, query: str) -> str:
|
332 |
+
"""Fallback response when model fails"""
|
333 |
+
sentences = [s.strip() for s in context.split('.') if len(s.strip()) > 20]
|
334 |
+
|
335 |
+
if sentences:
|
336 |
+
response = sentences[0] + '.'
|
337 |
+
if len(sentences) > 1:
|
338 |
+
response += ' ' + sentences[1] + '.'
|
339 |
+
else:
|
340 |
+
response = context[:300] + '...'
|
341 |
+
|
342 |
+
return response
|
343 |
+
|
344 |
+
def handle_conversational_interactions(self, query: str) -> Optional[str]:
|
345 |
+
"""Handle conversational interactions"""
|
346 |
+
query_lower = query.lower().strip()
|
347 |
+
|
348 |
+
# Greetings
|
349 |
+
greeting_patterns = [
|
350 |
+
r'^\s*(hello|hi|hey)\s*,
|
351 |
+
r'^\s*(good morning|good afternoon|good evening)\s*,
|
352 |
+
r'^\s*(how are you)\s*
|
353 |
+
]
|
354 |
+
|
355 |
+
for pattern in greeting_patterns:
|
356 |
+
if re.match(pattern, query_lower):
|
357 |
+
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?"
|
358 |
+
|
359 |
+
# Thanks
|
360 |
+
if any(word in query_lower for word in ['thank you', 'thanks', 'helpful']):
|
361 |
+
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!"
|
362 |
+
|
363 |
+
# About/Help
|
364 |
+
if any(word in query_lower for word in ['what are you', 'who are you', 'help', 'what can you do']):
|
365 |
+
return """π€ **About BioGPT Medical Assistant**
|
366 |
+
|
367 |
+
I'm an AI medical assistant powered by BioGPT, specialized in pediatric medicine. I can help with:
|
368 |
+
|
369 |
+
π©Ί **Medical Information:**
|
370 |
+
β’ Pediatric symptoms and conditions
|
371 |
+
β’ Treatment guidance
|
372 |
+
β’ When to seek medical care
|
373 |
+
β’ Prevention and wellness
|
374 |
+
|
375 |
+
β οΈ **Important:** I provide educational information only. Always consult healthcare professionals for medical decisions."""
|
376 |
+
|
377 |
+
return None
|
378 |
+
|
379 |
+
def chat_interface(self, message: str, history: List[List[str]]) -> str:
|
380 |
+
"""Main chat interface for Gradio - FIXED to prevent greeting mode stuck"""
|
381 |
+
if not message.strip():
|
382 |
+
return "Hello! I'm BioGPT, your medical AI assistant. How can I help you with pediatric medical questions today?"
|
383 |
+
|
384 |
+
print(f"π Processing query: '{message}'") # Debug logging
|
385 |
+
|
386 |
+
# Handle ONLY very specific conversational interactions
|
387 |
+
conversational_response = self.handle_conversational_interactions(message)
|
388 |
+
if conversational_response:
|
389 |
+
print(" Handled as conversational") # Debug
|
390 |
+
return conversational_response
|
391 |
+
|
392 |
+
print(" Processing as medical query") # Debug
|
393 |
+
|
394 |
+
# ALWAYS try to process as medical query if not a strict greeting
|
395 |
+
context = self.retrieve_medical_context(message)
|
396 |
+
|
397 |
+
if not context:
|
398 |
+
# Even if no context found, provide a helpful medical response
|
399 |
+
return f"""π©Ί **Medical Query:** {message}
|
400 |
+
|
401 |
+
β οΈ I don't have specific information about this topic in my current medical database. However, I recommend:
|
402 |
+
|
403 |
+
1. **Consult Healthcare Provider**: For personalized medical advice
|
404 |
+
2. **Emergency Signs**: If symptoms are severe, seek immediate care
|
405 |
+
3. **Reliable Sources**: Check with pediatricians for children's health concerns
|
406 |
+
|
407 |
+
**For urgent medical concerns, contact your healthcare provider or emergency services.**
|
408 |
+
|
409 |
+
π‘ **Try asking about**: fever, cough, rash, dehydration, or other common pediatric symptoms."""
|
410 |
+
|
411 |
+
# Generate medical response
|
412 |
+
main_context = '\n\n'.join(context)
|
413 |
+
response = self.generate_biogpt_response(main_context, message)
|
414 |
+
|
415 |
+
# Always format as medical response
|
416 |
+
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."
|
417 |
+
|
418 |
+
return final_response
|
419 |
+
|
420 |
+
# Test function to verify chatbot functionality
|
421 |
+
def test_chatbot_responses():
|
422 |
+
"""Test the chatbot with various queries to ensure it's working properly"""
|
423 |
+
print("\nπ§ͺ Testing BioGPT Chatbot Responses...")
|
424 |
+
print("=" * 50)
|
425 |
+
|
426 |
+
test_queries = [
|
427 |
+
"hello", # Should be conversational
|
428 |
+
"what causes fever", # Should be medical
|
429 |
+
"my child has a cough", # Should be medical
|
430 |
+
"help", # Should be conversational
|
431 |
+
"breathing problems in babies", # Should be medical
|
432 |
+
"thank you" # Should be conversational
|
433 |
+
]
|
434 |
+
|
435 |
+
for query in test_queries:
|
436 |
+
print(f"\nπ Query: '{query}'")
|
437 |
+
response = chatbot.chat_interface(query, [])
|
438 |
+
print(f"π€ Response type: {'CONVERSATIONAL' if any(word in response for word in ['Hello!', 'welcome!', 'About BioGPT']) else 'MEDICAL'}")
|
439 |
+
print(f"π Response: {response[:100]}...")
|
440 |
+
print("-" * 30)
|
441 |
+
|
442 |
+
# Initialize the chatbot globally
|
443 |
+
print("π Initializing BioGPT Medical Chatbot for Gradio...")
|
444 |
+
chatbot = BioGPTMedicalChatbot()
|
445 |
+
|
446 |
+
# Run tests
|
447 |
+
test_chatbot_responses()
|
448 |
+
|
449 |
+
def create_gradio_interface():
|
450 |
+
"""Create Gradio chat interface"""
|
451 |
+
|
452 |
+
# Custom CSS for medical theme
|
453 |
+
css = """
|
454 |
+
.gradio-container {
|
455 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
456 |
+
}
|
457 |
+
.chat-message {
|
458 |
+
background-color: #f8f9fa;
|
459 |
+
border-radius: 10px;
|
460 |
+
padding: 10px;
|
461 |
+
margin: 5px;
|
462 |
+
}
|
463 |
+
"""
|
464 |
+
|
465 |
+
with gr.Blocks(
|
466 |
+
css=css,
|
467 |
+
title="BioGPT Medical Assistant",
|
468 |
+
theme=gr.themes.Soft()
|
469 |
+
) as demo:
|
470 |
+
|
471 |
+
# Header
|
472 |
+
gr.HTML("""
|
473 |
+
<div style="text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea, #764ba2); color: white; border-radius: 10px; margin-bottom: 20px;">
|
474 |
+
<h1>π₯ BioGPT Medical Assistant</h1>
|
475 |
+
<p>Professional AI Medical Chatbot powered by BioGPT</p>
|
476 |
+
<p><strong>Specialized in Pediatric Medicine & Children's Health</strong></p>
|
477 |
+
</div>
|
478 |
+
""")
|
479 |
+
|
480 |
+
# Important disclaimer
|
481 |
+
gr.HTML("""
|
482 |
+
<div style="background-color: #fff3cd; border: 1px solid #ffeaa7; border-radius: 8px; padding: 15px; margin-bottom: 20px;">
|
483 |
+
<h3 style="color: #856404; margin-top: 0;">β οΈ Medical Disclaimer</h3>
|
484 |
+
<p style="color: #856404; margin-bottom: 0;">
|
485 |
+
This AI provides educational medical information only and is NOT a substitute for professional medical advice,
|
486 |
+
diagnosis, or treatment. Always consult qualified healthcare providers for medical decisions.
|
487 |
+
<strong>In case of medical emergency, call emergency services immediately.</strong>
|
488 |
+
</p>
|
489 |
+
</div>
|
490 |
+
""")
|
491 |
+
|
492 |
+
# Chat interface
|
493 |
+
chatbot_interface = gr.ChatInterface(
|
494 |
+
fn=chatbot.chat_interface,
|
495 |
+
title="π¬ Chat with BioGPT",
|
496 |
+
description="Ask me about pediatric health, symptoms, treatments, and medical guidance.",
|
497 |
+
examples=[
|
498 |
+
"What causes fever in children?",
|
499 |
+
"How should I treat my child's cough?",
|
500 |
+
"When should I be concerned about my baby's breathing?",
|
501 |
+
"What are the signs of dehydration in infants?",
|
502 |
+
"When should I call the doctor for my child's symptoms?",
|
503 |
+
"How can I prevent common childhood illnesses?"
|
504 |
+
],
|
505 |
+
retry_btn=None,
|
506 |
+
undo_btn=None,
|
507 |
+
clear_btn="ποΈ Clear Chat",
|
508 |
+
submit_btn="π©Ί Ask BioGPT",
|
509 |
+
chatbot=gr.Chatbot(
|
510 |
+
height=500,
|
511 |
+
placeholder="<div style='text-align: center; color: #666;'>Start a conversation with BioGPT Medical Assistant</div>",
|
512 |
+
show_copy_button=True,
|
513 |
+
bubble_full_width=False
|
514 |
+
)
|
515 |
+
)
|
516 |
+
|
517 |
+
# Information tabs
|
518 |
+
with gr.Tabs():
|
519 |
+
with gr.Tab("βΉοΈ About"):
|
520 |
+
gr.Markdown("""
|
521 |
+
## About BioGPT Medical Assistant
|
522 |
+
|
523 |
+
This AI assistant is powered by **BioGPT**, a specialized medical language model trained on extensive medical literature.
|
524 |
+
|
525 |
+
### π― Capabilities:
|
526 |
+
- **Pediatric Medicine**: Specialized in children's health
|
527 |
+
- **Symptom Analysis**: Understanding medical symptoms
|
528 |
+
- **Treatment Guidance**: Evidence-based treatment information
|
529 |
+
- **Medical Education**: Explaining medical concepts
|
530 |
+
- **Emergency Guidance**: When to seek immediate care
|
531 |
+
|
532 |
+
### π§ Technical Features:
|
533 |
+
- **Model**: Microsoft BioGPT (Medical AI)
|
534 |
+
- **Specialization**: Medical and biomedical text
|
535 |
+
- **Knowledge**: Based on medical literature and research
|
536 |
+
- **Optimization**: Memory-efficient deployment
|
537 |
+
|
538 |
+
### π± How to Use:
|
539 |
+
1. Type your medical question in the chat
|
540 |
+
2. Be specific about symptoms or concerns
|
541 |
+
3. Ask about pediatric health topics
|
542 |
+
4. Request guidance on when to seek care
|
543 |
+
""")
|
544 |
+
|
545 |
+
with gr.Tab("π©Ί Medical Topics"):
|
546 |
+
gr.Markdown("""
|
547 |
+
## Supported Medical Topics
|
548 |
+
|
549 |
+
### πΆ Pediatric Specialties:
|
550 |
+
- **Common Symptoms**: Fever, cough, rash, vomiting, diarrhea
|
551 |
+
- **Respiratory**: Breathing issues, asthma, colds
|
552 |
+
- **Digestive**: Stomach problems, feeding issues
|
553 |
+
- **Skin**: Rashes, eczema, allergic reactions
|
554 |
+
- **Development**: Growth and developmental concerns
|
555 |
+
|
556 |
+
### π¨ Emergency Guidance:
|
557 |
+
- When to call emergency services
|
558 |
+
- Signs requiring immediate medical attention
|
559 |
+
- First aid basics for children
|
560 |
+
|
561 |
+
### π Treatment Information:
|
562 |
+
- Evidence-based treatment options
|
563 |
+
- Home care remedies
|
564 |
+
- Safety considerations
|
565 |
+
- Recovery expectations
|
566 |
+
|
567 |
+
### π‘οΈ Prevention:
|
568 |
+
- Vaccination information
|
569 |
+
- Disease prevention
|
570 |
+
- Healthy habits for children
|
571 |
+
- Safety measures
|
572 |
+
""")
|
573 |
+
|
574 |
+
with gr.Tab("β οΈ Safety & Limitations"):
|
575 |
+
gr.Markdown("""
|
576 |
+
## Important Safety Information
|
577 |
+
|
578 |
+
### π¨ Emergency Situations - Call Emergency Services:
|
579 |
+
- Difficulty breathing or choking
|
580 |
+
- Severe allergic reactions
|
581 |
+
- Unconsciousness or unresponsiveness
|
582 |
+
- Severe injuries or accidents
|
583 |
+
- Persistent high fever (>104Β°F/40Β°C)
|
584 |
+
|
585 |
+
### π₯ When to Consult Healthcare Providers:
|
586 |
+
- For diagnosis of medical conditions
|
587 |
+
- Before starting any treatments
|
588 |
+
- For prescription medications
|
589 |
+
- When symptoms worsen or persist
|
590 |
+
- For personalized medical advice
|
591 |
+
|
592 |
+
### π€ AI Limitations:
|
593 |
+
- Cannot diagnose medical conditions
|
594 |
+
- Cannot prescribe medications
|
595 |
+
- Cannot replace professional medical judgment
|
596 |
+
- May not have latest medical developments
|
597 |
+
- Should not be used for emergency situations
|
598 |
+
|
599 |
+
### π Additional Resources:
|
600 |
+
- **Emergency**: Your local emergency number
|
601 |
+
- **Poison Control**: Contact local poison control
|
602 |
+
- **Pediatrician**: Your child's healthcare provider
|
603 |
+
- **Nurse Hotline**: 24/7 nurse consultations (many insurance plans)
|
604 |
+
""")
|
605 |
+
|
606 |
+
# Footer
|
607 |
+
gr.HTML("""
|
608 |
+
<div style="text-align: center; padding: 20px; margin-top: 30px; border-top: 1px solid #ddd; color: #666;">
|
609 |
+
<p>π€ <strong>BioGPT Medical Assistant</strong> | Powered by Microsoft BioGPT</p>
|
610 |
+
<p>For educational purposes only β’ Always consult healthcare professionals</p>
|
611 |
+
</div>
|
612 |
+
""")
|
613 |
+
|
614 |
+
return demo
|
615 |
+
|
616 |
+
# Create and launch the interface
|
617 |
+
demo = create_gradio_interface()
|
618 |
+
|
619 |
+
if __name__ == "__main__":
|
620 |
+
# Launch the app
|
621 |
+
demo.launch(
|
622 |
+
server_name="0.0.0.0",
|
623 |
+
server_port=7860,
|
624 |
+
share=False
|
625 |
+
)
|