| 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 |
|
|
| |
| st.set_page_config(page_title="Diabetic Retinopathy Detection", page_icon="👁️") |
|
|
| |
| 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] |
| 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}**") |
|
|
| |
| st.markdown( |
| """ |
| --- |
| **👩💻 تم تطويره بواسطة [Asmaa Elnagger](https://huggingface.co/AsmaaElnagger)** |
| 🔗 يعتمد على [DINOv2](https://huggingface.co/docs/transformers/model_doc/dinov2) من Hugging Face |
| 📅 تم التحديث في: 2025 |
| """ |
| ) |
|
|