Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
from pubmed_module import search_pubmed, summarize_text
|
| 8 |
+
|
| 9 |
+
# تحميل الموديلات
|
| 10 |
+
vision_model = load_vision_model()
|
| 11 |
+
seg_model = load_segmentation_model()
|
| 12 |
+
transform = get_image_transform()
|
| 13 |
+
|
| 14 |
+
# تحليل الأشعة
|
| 15 |
+
def analyze_image(image):
|
| 16 |
+
if image is None:
|
| 17 |
+
return "يرجى رفع صورة الأشعة"
|
| 18 |
+
img = np.array(image.convert("RGB"))
|
| 19 |
+
tensor = transform(img).unsqueeze(0).to(DEVICE)
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
output = vision_model(tensor)
|
| 22 |
+
probs = torch.softmax(output, dim=1).cpu().numpy()[0]
|
| 23 |
+
top_classes = np.argsort(probs)[::-1][:5]
|
| 24 |
+
result = "\n".join([f"فئة {i}: احتمال {probs[i]:.3f}" for i in top_classes])
|
| 25 |
+
return result
|
| 26 |
+
|
| 27 |
+
# تحليل الأعراض + البحث العلمي
|
| 28 |
+
def analyze_symptoms_with_pubmed(symptom_text):
|
| 29 |
+
if not symptom_text.strip():
|
| 30 |
+
return "يرجى كتابة الأعراض"
|
| 31 |
+
|
| 32 |
+
results = diagnose_symptoms(symptom_text)
|
| 33 |
+
|
| 34 |
+
text = "🔍 الأمراض المحتملة:\n\n"
|
| 35 |
+
for r in results:
|
| 36 |
+
text += f"- {r['disease']} (احتمال: {r['score']:.2f})\n📘 المصدر: {r['source']}\n"
|
| 37 |
+
if r.get("brief"):
|
| 38 |
+
text += f"📝 ملخص: {r['brief']}\n"
|
| 39 |
+
|
| 40 |
+
# البحث في PubMed لكل مرض محتمل
|
| 41 |
+
articles = search_pubmed(r['disease'])
|
| 42 |
+
if articles:
|
| 43 |
+
text += "📰 أحدث الأبحاث:\n"
|
| 44 |
+
for art in articles:
|
| 45 |
+
summary = summarize_text(art['abstract']) if art['abstract'] else "لا يوجد ملخص"
|
| 46 |
+
text += f"• {art['title']}\n ملخص: {summary}\n رابط: {art['link']}\n\n"
|
| 47 |
+
|
| 48 |
+
return text
|
| 49 |
+
|
| 50 |
+
# واجهة Gradio
|
| 51 |
+
with gr.Blocks(title="MedAI Assistant") as app:
|
| 52 |
+
gr.Markdown("# 🧠 MedAI — مساعد التحليل الطبي الذكي\n### ⚠️ لأغراض بحثية فقط، ليست بديلاً عن الطبيب")
|
| 53 |
+
|
| 54 |
+
with gr.Tab("تحليل الأشعة"):
|
| 55 |
+
image_input = gr.Image(type="pil", label="📤 ارفع صورة الأشعة")
|
| 56 |
+
image_output = gr.Textbox(label="🔍 نتائج التحليل")
|
| 57 |
+
analyze_button = gr.Button("تشغيل التحليل")
|
| 58 |
+
analyze_button.click(analyze_image, inputs=image_input, outputs=image_output)
|
| 59 |
+
|
| 60 |
+
with gr.Tab("تحليل الأعراض"):
|
| 61 |
+
symptom_input = gr.Textbox(label="✏️ اكتب الأعراض (مثال: حمى، سعال، ضيق تنفس)")
|
| 62 |
+
symptom_output = gr.Textbox(label="🩺 التشخيص + أبحاث PubMed")
|
| 63 |
+
analyze_symptoms_btn = gr.Button("تشغيل التحليل")
|
| 64 |
+
analyze_symptoms_btn.click(analyze_symptoms_with_pubmed, inputs=symptom_input, outputs=symptom_output)
|
| 65 |
+
|
| 66 |
+
app.launch()
|