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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import (
|
| 4 |
+
AutoProcessor,
|
| 5 |
+
AutoModelForCausalLM,
|
| 6 |
+
pipeline,
|
| 7 |
+
BitsAndBytesConfig
|
| 8 |
+
)
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import numpy as np
|
| 11 |
+
import warnings
|
| 12 |
+
import gc
|
| 13 |
+
import os
|
| 14 |
+
from typing import Optional, Tuple
|
| 15 |
+
|
| 16 |
+
warnings.filterwarnings("ignore")
|
| 17 |
+
|
| 18 |
+
class OptimizedAIDoctor:
|
| 19 |
+
def __init__(self):
|
| 20 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
+
self.model_loaded = False
|
| 22 |
+
|
| 23 |
+
# Configuration for model loading
|
| 24 |
+
self.config = {
|
| 25 |
+
"vision_model": "microsoft/git-base-coco", # Lighter model for HF Spaces
|
| 26 |
+
"stt_model": "openai/whisper-tiny.en", # Smaller Whisper model
|
| 27 |
+
"max_memory": "6GB" if torch.cuda.is_available() else "2GB"
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
print(f"π Initializing AI Doctor on {self.device}")
|
| 31 |
+
print(f"πΎ Available memory config: {self.config['max_memory']}")
|
| 32 |
+
|
| 33 |
+
self.load_models()
|
| 34 |
+
|
| 35 |
+
def load_models(self):
|
| 36 |
+
"""Load models with optimizations for Hugging Face Spaces"""
|
| 37 |
+
try:
|
| 38 |
+
print("π¦ Loading Vision Model...")
|
| 39 |
+
|
| 40 |
+
# Quantization config for memory efficiency
|
| 41 |
+
if self.device == "cuda":
|
| 42 |
+
quantization_config = BitsAndBytesConfig(
|
| 43 |
+
load_in_4bit=True,
|
| 44 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 45 |
+
bnb_4bit_use_double_quant=True,
|
| 46 |
+
bnb_4bit_quant_type="nf4"
|
| 47 |
+
)
|
| 48 |
+
else:
|
| 49 |
+
quantization_config = None
|
| 50 |
+
|
| 51 |
+
# Load vision model
|
| 52 |
+
self.vision_processor = AutoProcessor.from_pretrained(
|
| 53 |
+
self.config["vision_model"],
|
| 54 |
+
trust_remote_code=True
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
self.vision_model = AutoModelForCausalLM.from_pretrained(
|
| 58 |
+
self.config["vision_model"],
|
| 59 |
+
quantization_config=quantization_config,
|
| 60 |
+
device_map="auto" if self.device == "cuda" else None,
|
| 61 |
+
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
| 62 |
+
trust_remote_code=True,
|
| 63 |
+
low_cpu_mem_usage=True
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
print("π€ Loading Speech-to-Text Model...")
|
| 67 |
+
# Load STT pipeline with smaller model
|
| 68 |
+
self.stt_pipeline = pipeline(
|
| 69 |
+
"automatic-speech-recognition",
|
| 70 |
+
model=self.config["stt_model"],
|
| 71 |
+
device=0 if self.device == "cuda" else -1,
|
| 72 |
+
return_timestamps=False,
|
| 73 |
+
chunk_length_s=10,
|
| 74 |
+
stride_length_s=2
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
self.model_loaded = True
|
| 78 |
+
print("β
All models loaded successfully!")
|
| 79 |
+
|
| 80 |
+
# Clear cache
|
| 81 |
+
if torch.cuda.is_available():
|
| 82 |
+
torch.cuda.empty_cache()
|
| 83 |
+
gc.collect()
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"β Error loading models: {e}")
|
| 87 |
+
print("π Trying fallback configuration...")
|
| 88 |
+
self.load_minimal_models()
|
| 89 |
+
|
| 90 |
+
def load_minimal_models(self):
|
| 91 |
+
"""Load minimal models as fallback"""
|
| 92 |
+
try:
|
| 93 |
+
# Use even lighter models
|
| 94 |
+
self.config["vision_model"] = "nlpconnect/vit-gpt2-image-captioning"
|
| 95 |
+
self.config["stt_model"] = "openai/whisper-tiny"
|
| 96 |
+
|
| 97 |
+
self.vision_processor = AutoProcessor.from_pretrained(self.config["vision_model"])
|
| 98 |
+
self.vision_model = AutoModelForCausalLM.from_pretrained(
|
| 99 |
+
self.config["vision_model"],
|
| 100 |
+
torch_dtype=torch.float32
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
self.stt_pipeline = pipeline(
|
| 104 |
+
"automatic-speech-recognition",
|
| 105 |
+
model=self.config["stt_model"],
|
| 106 |
+
device=-1 # Force CPU
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
self.model_loaded = True
|
| 110 |
+
print("β
Fallback models loaded!")
|
| 111 |
+
|
| 112 |
+
except Exception as e:
|
| 113 |
+
print(f"β Critical error: {e}")
|
| 114 |
+
self.model_loaded = False
|
| 115 |
+
|
| 116 |
+
def analyze_medical_image(self, image: Image.Image, question: str = "") -> str:
|
| 117 |
+
"""Analyze medical image with AI"""
|
| 118 |
+
if not self.model_loaded:
|
| 119 |
+
return "β Models not loaded. Please try again later."
|
| 120 |
+
|
| 121 |
+
if image is None:
|
| 122 |
+
return "πΈ Please upload an image for analysis."
|
| 123 |
+
|
| 124 |
+
try:
|
| 125 |
+
# Resize image for efficiency
|
| 126 |
+
image = image.convert("RGB")
|
| 127 |
+
if image.size[0] > 512 or image.size[1] > 512:
|
| 128 |
+
image.thumbnail((512, 512), Image.Resampling.LANCZOS)
|
| 129 |
+
|
| 130 |
+
# Create medical analysis prompt
|
| 131 |
+
if not question:
|
| 132 |
+
question = "Analyze this medical image and describe what you observe."
|
| 133 |
+
|
| 134 |
+
prompt = f"""Medical Image Analysis:
|
| 135 |
+
|
| 136 |
+
{question}
|
| 137 |
+
|
| 138 |
+
Please provide:
|
| 139 |
+
1. Visual observations
|
| 140 |
+
2. Possible medical conditions
|
| 141 |
+
3. General care recommendations
|
| 142 |
+
4. When to seek professional help
|
| 143 |
+
|
| 144 |
+
Response:"""
|
| 145 |
+
|
| 146 |
+
# Process inputs
|
| 147 |
+
inputs = self.vision_processor(
|
| 148 |
+
text=prompt,
|
| 149 |
+
images=image,
|
| 150 |
+
return_tensors="pt",
|
| 151 |
+
padding=True,
|
| 152 |
+
truncation=True
|
| 153 |
+
).to(self.device)
|
| 154 |
+
|
| 155 |
+
# Generate response with memory management
|
| 156 |
+
with torch.no_grad():
|
| 157 |
+
outputs = self.vision_model.generate(
|
| 158 |
+
**inputs,
|
| 159 |
+
max_new_tokens=300,
|
| 160 |
+
do_sample=True,
|
| 161 |
+
temperature=0.7,
|
| 162 |
+
top_p=0.9,
|
| 163 |
+
pad_token_id=self.vision_processor.tokenizer.eos_token_id
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
# Decode response
|
| 167 |
+
response = self.vision_processor.decode(outputs[0], skip_special_tokens=True)
|
| 168 |
+
|
| 169 |
+
# Clean response
|
| 170 |
+
if prompt in response:
|
| 171 |
+
response = response.replace(prompt, "").strip()
|
| 172 |
+
|
| 173 |
+
# Add medical disclaimer
|
| 174 |
+
medical_response = self.format_medical_response(response)
|
| 175 |
+
|
| 176 |
+
# Memory cleanup
|
| 177 |
+
del inputs, outputs
|
| 178 |
+
if torch.cuda.is_available():
|
| 179 |
+
torch.cuda.empty_cache()
|
| 180 |
+
|
| 181 |
+
return medical_response
|
| 182 |
+
|
| 183 |
+
except Exception as e:
|
| 184 |
+
return f"β Error analyzing image: {str(e)}\n\nPlease try with a different image or contact support."
|
| 185 |
+
|
| 186 |
+
def transcribe_audio(self, audio_path: Optional[str]) -> str:
|
| 187 |
+
"""Convert speech to text"""
|
| 188 |
+
if not self.model_loaded or audio_path is None:
|
| 189 |
+
return "No audio to transcribe."
|
| 190 |
+
|
| 191 |
+
try:
|
| 192 |
+
result = self.stt_pipeline(audio_path)
|
| 193 |
+
|
| 194 |
+
if isinstance(result, dict):
|
| 195 |
+
return result.get("text", "").strip()
|
| 196 |
+
elif isinstance(result, list) and len(result) > 0:
|
| 197 |
+
return result[0].get("text", "").strip()
|
| 198 |
+
else:
|
| 199 |
+
return str(result).strip()
|
| 200 |
+
|
| 201 |
+
except Exception as e:
|
| 202 |
+
return f"Error transcribing audio: {str(e)}"
|
| 203 |
+
|
| 204 |
+
def format_medical_response(self, response: str) -> str:
|
| 205 |
+
"""Format the AI response with proper medical disclaimers"""
|
| 206 |
+
|
| 207 |
+
# Clean and structure the response
|
| 208 |
+
formatted_response = f"""
|
| 209 |
+
π **AI Medical Image Analysis**
|
| 210 |
+
|
| 211 |
+
{response}
|
| 212 |
+
|
| 213 |
+
---
|
| 214 |
+
|
| 215 |
+
β οΈ **IMPORTANT MEDICAL DISCLAIMER**
|
| 216 |
+
|
| 217 |
+
This AI analysis is for informational purposes only and should NOT replace professional medical advice.
|
| 218 |
+
|
| 219 |
+
π₯ **Please consult a qualified healthcare provider for:**
|
| 220 |
+
- Proper medical diagnosis
|
| 221 |
+
- Treatment recommendations
|
| 222 |
+
- Emergency medical situations
|
| 223 |
+
- Any health concerns
|
| 224 |
+
|
| 225 |
+
π **Seek immediate medical attention if you experience:**
|
| 226 |
+
- Severe pain or discomfort
|
| 227 |
+
- Signs of infection (fever, swelling, pus)
|
| 228 |
+
- Rapid changes in condition
|
| 229 |
+
- Any emergency symptoms
|
| 230 |
+
|
| 231 |
+
This tool is designed to provide general health information and encourage appropriate medical care.
|
| 232 |
+
"""
|
| 233 |
+
|
| 234 |
+
return formatted_response.strip()
|
| 235 |
+
|
| 236 |
+
def process_medical_consultation(
|
| 237 |
+
self,
|
| 238 |
+
image: Optional[Image.Image],
|
| 239 |
+
audio: Optional[str],
|
| 240 |
+
text_question: str
|
| 241 |
+
) -> Tuple[str, str]:
|
| 242 |
+
"""Main function to handle medical consultation"""
|
| 243 |
+
|
| 244 |
+
if not self.model_loaded:
|
| 245 |
+
return "β AI models are not loaded. Please refresh and try again.", ""
|
| 246 |
+
|
| 247 |
+
# Handle input prioritization
|
| 248 |
+
question = text_question.strip() if text_question else ""
|
| 249 |
+
transcribed_audio = ""
|
| 250 |
+
|
| 251 |
+
# Process audio if provided
|
| 252 |
+
if audio is not None:
|
| 253 |
+
transcribed_audio = self.transcribe_audio(audio)
|
| 254 |
+
if transcribed_audio and len(transcribed_audio.strip()) > 0:
|
| 255 |
+
question = transcribed_audio
|
| 256 |
+
|
| 257 |
+
# Validate inputs
|
| 258 |
+
if image is None:
|
| 259 |
+
return "πΈ Please upload a medical image to analyze.", transcribed_audio
|
| 260 |
+
|
| 261 |
+
if not question:
|
| 262 |
+
question = "What medical condition or issue do you see in this image?"
|
| 263 |
+
|
| 264 |
+
# Perform medical analysis
|
| 265 |
+
try:
|
| 266 |
+
analysis = self.analyze_medical_image(image, question)
|
| 267 |
+
return analysis, f"Question processed: {question}"
|
| 268 |
+
|
| 269 |
+
except Exception as e:
|
| 270 |
+
error_response = f"""
|
| 271 |
+
β **Error Processing Request**
|
| 272 |
+
|
| 273 |
+
Unable to analyze the medical image due to: {str(e)}
|
| 274 |
+
|
| 275 |
+
π **Please try:**
|
| 276 |
+
- Uploading a clearer image
|
| 277 |
+
- Asking a simpler question
|
| 278 |
+
- Refreshing the page
|
| 279 |
+
|
| 280 |
+
If the problem persists, the AI service may be temporarily unavailable.
|
| 281 |
+
|
| 282 |
+
β οΈ **For urgent medical needs, please contact a healthcare provider immediately.**
|
| 283 |
+
"""
|
| 284 |
+
return error_response, transcribed_audio
|
| 285 |
+
|
| 286 |
+
# Initialize AI Doctor
|
| 287 |
+
print("π₯ Starting AI Doctor initialization...")
|
| 288 |
+
ai_doctor = OptimizedAIDoctor()
|
| 289 |
+
|
| 290 |
+
# Create enhanced Gradio interface
|
| 291 |
+
def create_medical_interface():
|
| 292 |
+
"""Create the main Gradio interface"""
|
| 293 |
+
|
| 294 |
+
# Custom CSS for medical theme
|
| 295 |
+
custom_css = """
|
| 296 |
+
.gradio-container {
|
| 297 |
+
max-width: 1400px !important;
|
| 298 |
+
margin: auto;
|
| 299 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 300 |
+
}
|
| 301 |
+
.medical-header {
|
| 302 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 303 |
+
color: white;
|
| 304 |
+
padding: 30px;
|
| 305 |
+
border-radius: 15px;
|
| 306 |
+
text-align: center;
|
| 307 |
+
margin-bottom: 30px;
|
| 308 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
|
| 309 |
+
}
|
| 310 |
+
.disclaimer-box {
|
| 311 |
+
background-color: #fff3cd;
|
| 312 |
+
border-left: 4px solid #ffc107;
|
| 313 |
+
border-radius: 5px;
|
| 314 |
+
padding: 20px;
|
| 315 |
+
margin: 20px 0;
|
| 316 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 317 |
+
}
|
| 318 |
+
.feature-box {
|
| 319 |
+
background: white;
|
| 320 |
+
border-radius: 10px;
|
| 321 |
+
padding: 20px;
|
| 322 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
|
| 323 |
+
margin: 10px 0;
|
| 324 |
+
}
|
| 325 |
+
.status-indicator {
|
| 326 |
+
display: inline-block;
|
| 327 |
+
width: 10px;
|
| 328 |
+
height: 10px;
|
| 329 |
+
border-radius: 50%;
|
| 330 |
+
margin-right: 8px;
|
| 331 |
+
}
|
| 332 |
+
.status-online { background-color: #28a745; }
|
| 333 |
+
.status-offline { background-color: #dc3545; }
|
| 334 |
+
"""
|
| 335 |
+
|
| 336 |
+
with gr.Blocks(
|
| 337 |
+
title="π₯ AI Doctor - Medical Assistant",
|
| 338 |
+
theme=gr.themes.Soft(
|
| 339 |
+
primary_hue="blue",
|
| 340 |
+
secondary_hue="indigo",
|
| 341 |
+
neutral_hue="slate"
|
| 342 |
+
),
|
| 343 |
+
css=custom_css
|
| 344 |
+
) as interface:
|
| 345 |
+
|
| 346 |
+
# Header
|
| 347 |
+
gr.HTML(f"""
|
| 348 |
+
<div class="medical-header">
|
| 349 |
+
<h1>π₯ AI Medical Assistant</h1>
|
| 350 |
+
<p style="font-size: 18px; margin: 10px 0;">
|
| 351 |
+
Advanced AI-powered medical image analysis with voice and text support
|
| 352 |
+
</p>
|
| 353 |
+
<p style="font-size: 14px; opacity: 0.9;">
|
| 354 |
+
<span class="status-indicator {'status-online' if ai_doctor.model_loaded else 'status-offline'}"></span>
|
| 355 |
+
AI Status: {'Online' if ai_doctor.model_loaded else 'Offline - Please refresh'}
|
| 356 |
+
</p>
|
| 357 |
+
</div>
|
| 358 |
+
""")
|
| 359 |
+
|
| 360 |
+
# Main disclaimer
|
| 361 |
+
gr.HTML("""
|
| 362 |
+
<div class="disclaimer-box">
|
| 363 |
+
<h3>β οΈ Critical Medical Disclaimer</h3>
|
| 364 |
+
<p><strong>This AI tool is for educational purposes only and cannot replace professional medical advice.</strong></p>
|
| 365 |
+
<ul>
|
| 366 |
+
<li>Always consult qualified healthcare professionals for medical concerns</li>
|
| 367 |
+
<li>Do not use for emergency medical situations - call emergency services</li>
|
| 368 |
+
<li>AI analysis may have limitations and should not be solely relied upon</li>
|
| 369 |
+
<li>Your privacy is important - images are processed temporarily and not stored</li>
|
| 370 |
+
</ul>
|
| 371 |
+
</div>
|
| 372 |
+
""")
|
| 373 |
+
|
| 374 |
+
# Main interface
|
| 375 |
+
with gr.Row(equal_height=True):
|
| 376 |
+
# Input column
|
| 377 |
+
with gr.Column(scale=1, variant="panel"):
|
| 378 |
+
gr.Markdown("## π Medical Consultation Input")
|
| 379 |
+
|
| 380 |
+
# Image upload
|
| 381 |
+
with gr.Group():
|
| 382 |
+
gr.Markdown("### πΈ Upload Medical Image")
|
| 383 |
+
image_input = gr.Image(
|
| 384 |
+
label="Medical Image (Required)",
|
| 385 |
+
type="pil",
|
| 386 |
+
height=300,
|
| 387 |
+
interactive=True
|
| 388 |
+
)
|
| 389 |
+
gr.Markdown("*Upload a clear image of the medical condition or area of concern*")
|
| 390 |
+
|
| 391 |
+
# Audio input
|
| 392 |
+
with gr.Group():
|
| 393 |
+
gr.Markdown("### π€ Voice Question (Optional)")
|
| 394 |
+
audio_input = gr.Audio(
|
| 395 |
+
label="Record your medical question",
|
| 396 |
+
source="microphone",
|
| 397 |
+
type="filepath",
|
| 398 |
+
interactive=True
|
| 399 |
+
)
|
| 400 |
+
gr.Markdown("*Click to record your question about the medical image*")
|
| 401 |
+
|
| 402 |
+
# Text input
|
| 403 |
+
with gr.Group():
|
| 404 |
+
gr.Markdown("### βοΈ Text Question (Optional)")
|
| 405 |
+
text_input = gr.Textbox(
|
| 406 |
+
label="Type your medical question",
|
| 407 |
+
placeholder="Example: What type of rash is this? How should I treat it?",
|
| 408 |
+
lines=3,
|
| 409 |
+
interactive=True
|
| 410 |
+
)
|
| 411 |
+
gr.Markdown("*Describe your symptoms or ask specific questions about the image*")
|
| 412 |
+
|
| 413 |
+
# Analysis button
|
| 414 |
+
analyze_button = gr.Button(
|
| 415 |
+
"π Analyze Medical Condition",
|
| 416 |
+
variant="primary",
|
| 417 |
+
size="lg",
|
| 418 |
+
scale=1
|
| 419 |
+
)
|
| 420 |
+
|
| 421 |
+
# Clear button
|
| 422 |
+
clear_button = gr.Button(
|
| 423 |
+
"ποΈ Clear All",
|
| 424 |
+
variant="secondary",
|
| 425 |
+
size="sm"
|
| 426 |
+
)
|
| 427 |
+
|
| 428 |
+
# Output column
|
| 429 |
+
with gr.Column(scale=1, variant="panel"):
|
| 430 |
+
gr.Markdown("## π AI Medical Analysis")
|
| 431 |
+
|
| 432 |
+
# Main analysis output
|
| 433 |
+
analysis_output = gr.Textbox(
|
| 434 |
+
label="Medical Analysis Report",
|
| 435 |
+
lines=20,
|
| 436 |
+
max_lines=25,
|
| 437 |
+
interactive=False,
|
| 438 |
+
placeholder="Upload an image and click 'Analyze Medical Condition' to get started..."
|
| 439 |
+
)
|
| 440 |
+
|
| 441 |
+
# Transcription output
|
| 442 |
+
transcription_output = gr.Textbox(
|
| 443 |
+
label="Voice Input Transcription",
|
| 444 |
+
lines=2,
|
| 445 |
+
interactive=False,
|
| 446 |
+
placeholder="Voice transcription will appear here..."
|
| 447 |
+
)
|
| 448 |
+
|
| 449 |
+
# Quick actions
|
| 450 |
+
with gr.Row():
|
| 451 |
+
gr.Button("π Copy Analysis", size="sm", variant="secondary")
|
| 452 |
+
gr.Button("π§ Email Report", size="sm", variant="secondary")
|
| 453 |
+
|
| 454 |
+
# Examples section
|
| 455 |
+
gr.Markdown("## π Example Medical Cases")
|
| 456 |
+
gr.Markdown("*Click on any example to see how the AI analyzes different medical conditions*")
|
| 457 |
+
|
| 458 |
+
with gr.Row():
|
| 459 |
+
with gr.Column():
|
| 460 |
+
gr.Examples(
|
| 461 |
+
examples=[
|
| 462 |
+
[None, None, "What does this skin condition look like?"],
|
| 463 |
+
[None, None, "Is this a normal mole or should I be concerned?"],
|
| 464 |
+
[None, None, "How should I care for this wound?"],
|
| 465 |
+
[None, None, "What type of rash is this?"],
|
| 466 |
+
[None, None, "Are these symptoms of an infection?"],
|
| 467 |
+
],
|
| 468 |
+
inputs=[image_input, audio_input, text_input],
|
| 469 |
+
label="Common Questions"
|
| 470 |
+
)
|
| 471 |
+
|
| 472 |
+
# Feature information
|
| 473 |
+
with gr.Row():
|
| 474 |
+
with gr.Column(scale=1):
|
| 475 |
+
gr.Markdown("""
|
| 476 |
+
### π― What Can AI Doctor Analyze?
|
| 477 |
+
|
| 478 |
+
**β
Skin Conditions**
|
| 479 |
+
- Acne and pimples
|
| 480 |
+
- Rashes and dermatitis
|
| 481 |
+
- Moles and skin spots
|
| 482 |
+
- Cuts and wounds
|
| 483 |
+
- Eczema and psoriasis
|
| 484 |
+
|
| 485 |
+
**β
General Health**
|
| 486 |
+
- Visible infections
|
| 487 |
+
- Swelling and inflammation
|
| 488 |
+
- Hair and scalp issues
|
| 489 |
+
- Eye conditions (external)
|
| 490 |
+
- Oral health concerns
|
| 491 |
+
""")
|
| 492 |
+
|
| 493 |
+
with gr.Column(scale=1):
|
| 494 |
+
gr.Markdown("""
|
| 495 |
+
### β οΈ Important Limitations
|
| 496 |
+
|
| 497 |
+
**β Cannot Diagnose**
|
| 498 |
+
- Internal medical conditions
|
| 499 |
+
- Serious diseases requiring tests
|
| 500 |
+
- Emergency medical situations
|
| 501 |
+
- Conditions needing lab work
|
| 502 |
+
|
| 503 |
+
**β Not Suitable For**
|
| 504 |
+
- Chest pain or heart issues
|
| 505 |
+
- Severe allergic reactions
|
| 506 |
+
- Mental health crises
|
| 507 |
+
- Medication dosing questions
|
| 508 |
+
- Pregnancy-related concerns
|
| 509 |
+
""")
|
| 510 |
+
|
| 511 |
+
# Technical information
|
| 512 |
+
with gr.Accordion("π οΈ Technical Details", open=False):
|
| 513 |
+
gr.Markdown(f"""
|
| 514 |
+
### AI Models Used
|
| 515 |
+
- **Vision Analysis**: {ai_doctor.config['vision_model']}
|
| 516 |
+
- **Speech Recognition**: {ai_doctor.config['stt_model']}
|
| 517 |
+
- **Processing Device**: {ai_doctor.device.upper()}
|
| 518 |
+
- **Memory Configuration**: {ai_doctor.config['max_memory']}
|
| 519 |
+
|
| 520 |
+
### Privacy & Security
|
| 521 |
+
- Images are processed in real-time and not permanently stored
|
| 522 |
+
- Voice recordings are transcribed locally and deleted
|
| 523 |
+
- No personal health information is saved
|
| 524 |
+
- All processing follows HIPAA-like privacy principles
|
| 525 |
+
|
| 526 |
+
### Open Source
|
| 527 |
+
This application is built entirely with open-source models and can be run locally or deployed on your own infrastructure.
|
| 528 |
+
""")
|
| 529 |
+
|
| 530 |
+
# Event handlers
|
| 531 |
+
def analyze_condition(image, audio, text):
|
| 532 |
+
if not ai_doctor.model_loaded:
|
| 533 |
+
return "β AI models are not available. Please refresh the page and try again.", ""
|
| 534 |
+
return ai_doctor.process_medical_consultation(image, audio, text)
|
| 535 |
+
|
| 536 |
+
def clear_all():
|
| 537 |
+
return None, None, "", "", ""
|
| 538 |
+
|
| 539 |
+
# Button events
|
| 540 |
+
analyze_button.click(
|
| 541 |
+
fn=analyze_condition,
|
| 542 |
+
inputs=[image_input, audio_input, text_input],
|
| 543 |
+
outputs=[analysis_output, transcription_output],
|
| 544 |
+
show_progress=True
|
| 545 |
+
)
|
| 546 |
+
|
| 547 |
+
clear_button.click(
|
| 548 |
+
fn=clear_all,
|
| 549 |
+
outputs=[image_input, audio_input, text_input, analysis_output, transcription_output]
|
| 550 |
+
)
|
| 551 |
+
|
| 552 |
+
# Footer
|
| 553 |
+
gr.HTML("""
|
| 554 |
+
<div style="text-align: center; margin-top: 40px; padding: 20px; background-color: #f8f9fa; border-radius: 10px;">
|
| 555 |
+
<p><strong>π₯ AI Medical Assistant</strong> - Powered by Open Source AI</p>
|
| 556 |
+
<p style="font-size: 12px; color: #666;">
|
| 557 |
+
Built with LLaVA, Whisper, and Gradio β’ Not for emergency use β’ Always consult healthcare professionals
|
| 558 |
+
</p>
|
| 559 |
+
</div>
|
| 560 |
+
""")
|
| 561 |
+
|
| 562 |
+
return interface
|
| 563 |
+
|
| 564 |
+
# Launch application
|
| 565 |
+
if __name__ == "__main__":
|
| 566 |
+
print("π Launching AI Doctor Medical Assistant...")
|
| 567 |
+
|
| 568 |
+
# Create and launch interface
|
| 569 |
+
demo = create_medical_interface()
|
| 570 |
+
|
| 571 |
+
# Launch configuration for Hugging Face Spaces
|
| 572 |
+
demo.launch(
|
| 573 |
+
server_name="0.0.0.0",
|
| 574 |
+
server_port=7860,
|
| 575 |
+
share=True,
|
| 576 |
+
show_error=True,
|
| 577 |
+
show_tips=True,
|
| 578 |
+
enable_queue=True,
|
| 579 |
+
max_threads=10
|
| 580 |
+
)
|