AsmaaElnagger's picture
update_word_of_prediction_to_disease_stage
5f6a808
import streamlit as st
import torch
from transformers import AutoImageProcessor, AutoModelForImageClassification
from PIL import Image
import requests
from io import BytesIO
from pdf2image import convert_from_bytes
# إعداد اسم النموذج
model_path = r"AsmaaElnagger/Diabetic_RetinoPathy_detection"
# تحميل النموذج والمعالج
@st.cache_resource
def load_model():
processor = AutoImageProcessor.from_pretrained(model_path)
model = AutoModelForImageClassification.from_pretrained(model_path)
model.eval()
return model, processor
# دالة التنبؤ بتصنيف الصورة
def predict(image, model, processor):
inputs = processor(images=image, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
predicted_class = predictions.argmax().item()
predicted_label = model.config.id2label[predicted_class]
return predicted_label
# ✅ واجهة Streamlit
st.set_page_config(page_title="Diabetic Retinopathy Detection", page_icon="👁️")
# 🔹 إضافة الهيدر (Header)
st.title("🔍 Diabetic Retinopathy Detection")
st.markdown(
"""
### 🏥 مشروع لاكتشاف **اعتلال الشبكية السكري**
- يعتمد هذا التطبيق على نموذج ذكاء اصطناعي مدرب للكشف عن اعتلال الشبكية السكري من خلال الصور الطبية.
- يستخدم النموذج **DINOv2** من Hugging Face مع مكتبة Transformers.
- قم بتحميل صورة **أو** أدخل رابط صورة للحصول على التشخيص التلقائي.
"""
)
# 📌 تحميل النموذج والمعالج
model, processor = load_model()
# 🔹 اختيار طريقة إدخال الصورة
st.sidebar.header("📂 اختر طريقة الإدخال")
option = st.sidebar.radio("", ("📤 رفع صورة أو ملف PDF", "🌐 إدخال رابط صورة"))
image = None
if option == "📤 رفع صورة أو ملف PDF":
uploaded_file = st.file_uploader("📎 اختر صورة أو ملف PDF...", type=["jpg", "png", "jpeg", "pdf"])
if uploaded_file:
if uploaded_file.type == "application/pdf":
images = convert_from_bytes(uploaded_file.read())
if images:
image = images[0] # استخدام الصفحة الأولى من ملف PDF
st.image(image, caption="📄 صورة مستخرجة من PDF", use_column_width=True)
else:
image = Image.open(uploaded_file)
st.image(image, caption="🖼️ الصورة المرفوعة", use_column_width=True)
elif option == "🌐 إدخال رابط صورة":
url = st.text_input("🔗 أدخل رابط الصورة هنا")
if url:
try:
response = requests.get(url)
image = Image.open(BytesIO(response.content))
st.image(image, caption="🖼️ الصورة المستوردة", use_column_width=True)
except Exception as e:
st.error("⚠️ خطأ في تحميل الصورة من الرابط!")
# 🔹 التنبؤ بتصنيف الصورة
if image:
st.subheader("🧠 النموذج يعمل على تحليل الصورة...")
prediction = predict(image, model, processor)
st.success(f"✅ **مرحلة المرض: {prediction}**")
# ✅ إضافة فوتر (Footer)
st.markdown(
"""
---
**👩‍💻 تم تطويره بواسطة [Asmaa Elnagger](https://huggingface.co/AsmaaElnagger)**
🔗 يعتمد على [DINOv2](https://huggingface.co/docs/transformers/model_doc/dinov2) من Hugging Face
📅 تم التحديث في: 2025
"""
)