Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,56 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
img_norm = (img - img.min()) / (img.max() - img.min() + 1e-8)
|
| 35 |
-
display_img = (np.stack([img_norm]*3, axis=-1) * 255).astype(np.uint8)
|
| 36 |
-
else:
|
| 37 |
-
image = Image.open(file).convert("RGB")
|
| 38 |
-
display_img = np.array(image)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# التنبؤ
|
| 44 |
-
mean_probs, std_probs = mc_dropout_predictions(vision_model, input_tensor, n_samples=8)
|
| 45 |
-
|
| 46 |
-
# إنشاء تقرير مختصر
|
| 47 |
-
top_idx = np.argsort(mean_probs)[::-1][:5]
|
| 48 |
-
findings = []
|
| 49 |
-
for idx in top_idx:
|
| 50 |
-
p = mean_probs[idx]
|
| 51 |
-
u = std_probs[idx]
|
| 52 |
-
findings.append(f"Finding_{idx}: probability={p:.3f}, uncertainty={u:.3f}")
|
| 53 |
-
|
| 54 |
-
auto_report_en = "Model findings:\n" + "\n".join(findings)
|
| 55 |
-
auto_report_ar = translate_en_to_ar(auto_report_en, mt_tok, mt_model)
|
| 56 |
-
|
| 57 |
-
# إعداد JSON للإرسال لتطبيق Android
|
| 58 |
-
result = {
|
| 59 |
-
"filename": filename,
|
| 60 |
-
"vision_probs": mean_probs.tolist(),
|
| 61 |
-
"vision_uncertainty": std_probs.tolist(),
|
| 62 |
-
"auto_report_en": auto_report_en,
|
| 63 |
-
"auto_report_ar": auto_report_ar
|
| 64 |
-
}
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
demo = gr.Interface(
|
| 75 |
-
fn=analyze_image,
|
| 76 |
-
inputs=gr.Image(type="file", label="حمّل صورة الأشعة (JPG/PNG/DICOM)"),
|
| 77 |
-
outputs=[
|
| 78 |
-
gr.Image(label="Grad-CAM Overlay"),
|
| 79 |
-
gr.Textbox(label="نتائج JSON", lines=15)
|
| 80 |
-
],
|
| 81 |
-
title="MedAI — قارئ الأشعة الذكي",
|
| 82 |
-
description="يرجى رفع صورة أشعة لتحليلها وإنتاج تقرير تلقائي (تجريبي)."
|
| 83 |
-
)
|
| 84 |
|
| 85 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
from models import load_vision_model, get_image_transform, load_segmentation_model, DEVICE
|
| 6 |
+
from diagnosis_module import diagnose_symptoms
|
| 7 |
+
|
| 8 |
+
# تحميل الموديلات
|
| 9 |
+
vision_model = load_vision_model()
|
| 10 |
+
seg_model = load_segmentation_model()
|
| 11 |
+
transform = get_image_transform()
|
| 12 |
+
|
| 13 |
+
# تحليل الأشعة
|
| 14 |
+
def analyze_image(image):
|
| 15 |
+
if image is None:
|
| 16 |
+
return "يرجى رفع صورة الأشعة"
|
| 17 |
+
img = np.array(image.convert("RGB"))
|
| 18 |
+
tensor = transform(img).unsqueeze(0).to(DEVICE)
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
output = vision_model(tensor)
|
| 21 |
+
probs = torch.softmax(output, dim=1).cpu().numpy()[0]
|
| 22 |
+
top_classes = np.argsort(probs)[::-1][:5]
|
| 23 |
+
result = "\n".join([f"فئة {i}: احتمال {probs[i]:.3f}" for i in top_classes])
|
| 24 |
+
return result
|
| 25 |
+
|
| 26 |
+
# تحليل الأعراض
|
| 27 |
+
def analyze_symptoms(symptom_text):
|
| 28 |
+
if not symptom_text.strip():
|
| 29 |
+
return "يرجى كتابة الأعراض"
|
| 30 |
+
results = diagnose_symptoms(symptom_text)
|
| 31 |
+
text = "🔍 الأمراض المحتملة:\n\n"
|
| 32 |
+
for r in results:
|
| 33 |
+
text += f"- {r['disease']} (احتمال: {r['score']:.2f})\n📘 المصدر: {r['source']}\n\n"
|
| 34 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# واجهة Gradio
|
| 37 |
+
with gr.Blocks(title="MedAI Assistant") as app:
|
| 38 |
+
gr.Markdown("# 🧠 MedAI — مساعد التحليل الطبي الذكي\n### ⚠️ لأغراض بحثية فقط، ليست بديلاً عن الطبيب")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
with gr.Tab("تحليل الأشعة"):
|
| 41 |
+
image_input = gr.Image(type="pil", label="📤 ارفع صورة الأشعة")
|
| 42 |
+
image_output = gr.Textbox(label="🔍 نتائج التحليل")
|
| 43 |
+
analyze_button = gr.Button("تشغيل التحليل")
|
| 44 |
+
analyze_button.click(analyze_image, inputs=image_input, outputs=image_output)
|
| 45 |
|
| 46 |
+
with gr.Tab("تحليل الأعراض"):
|
| 47 |
+
symptom_input = gr.Textbox(label="✏️ اكتب الأعراض (مثال: حمى، سعال، ضيق تنفس)")
|
| 48 |
+
symptom_output = gr.Textbox(label="🩺 التشخيص المبدئي")
|
| 49 |
+
analyze_symptoms_btn = gr.Button("تشغيل التحليل")
|
| 50 |
+
analyze_symptoms_btn.click(analyze_symptoms, inputs=symptom_input, outputs=symptom_output)
|
| 51 |
|
| 52 |
+
app.launch()
|
| 53 |
|
| 54 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
demo.launch()
|