Spaces:
Sleeping
Sleeping
Commit ·
ce63b32
1
Parent(s): ef4bd60
mostafa add notifction API8
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from PIL import Image, UnidentifiedImageError
|
|
| 3 |
import numpy as np
|
| 4 |
import requests
|
| 5 |
from io import BytesIO
|
|
|
|
| 6 |
|
| 7 |
from transformers import AutoFeatureExtractor, AutoModelForImageClassification, pipeline
|
| 8 |
|
|
@@ -23,7 +24,7 @@ def predict(image_url):
|
|
| 23 |
# التحقق من نوع المحتوى
|
| 24 |
content_type = response.headers.get('Content-Type')
|
| 25 |
if not content_type or not content_type.startswith('image'):
|
| 26 |
-
return "الرابط المقدم لا يشير إلى صورة صالحة."
|
| 27 |
|
| 28 |
# تحميل الصورة من الرابط
|
| 29 |
image = Image.open(BytesIO(response.content)).convert("RGB")
|
|
@@ -33,20 +34,22 @@ def predict(image_url):
|
|
| 33 |
max_item = max(yl, key=lambda x: x['score'])
|
| 34 |
nn = "{:.2f}".format(max_item['score']) # تنسيق الدقة ليكون مقروءًا بشكل أفضل
|
| 35 |
dd = max_item['label']
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
except requests.exceptions.RequestException as e:
|
| 39 |
-
return f"خطأ في تحميل الصورة من الرابط المقدم: {e}"
|
| 40 |
except UnidentifiedImageError:
|
| 41 |
-
return "لا يمكن تحديد نوع الصورة من الرابط المقدم."
|
| 42 |
except Exception as e:
|
| 43 |
-
return f"حدث خطأ أثناء معالجة الصورة: {e}"
|
| 44 |
|
| 45 |
# إنشاء واجهة Gradio باستخدام المكونات الجديدة
|
| 46 |
iface = gr.Interface(
|
| 47 |
fn=predict,
|
| 48 |
inputs=gr.Textbox(lines=2, placeholder="أدخل رابط الصورة هنا...", label="رابط الصورة"),
|
| 49 |
-
outputs="text",
|
| 50 |
title="نموذج ViT لتصنيف الصور",
|
| 51 |
description="أدخل رابط صورة للحصول على تصنيف باستخدام نموذج ViT المدرب."
|
| 52 |
)
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import requests
|
| 5 |
from io import BytesIO
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
from transformers import AutoFeatureExtractor, AutoModelForImageClassification, pipeline
|
| 9 |
|
|
|
|
| 24 |
# التحقق من نوع المحتوى
|
| 25 |
content_type = response.headers.get('Content-Type')
|
| 26 |
if not content_type or not content_type.startswith('image'):
|
| 27 |
+
return json.dumps({"error": "الرابط المقدم لا يشير إلى صورة صالحة."})
|
| 28 |
|
| 29 |
# تحميل الصورة من الرابط
|
| 30 |
image = Image.open(BytesIO(response.content)).convert("RGB")
|
|
|
|
| 34 |
max_item = max(yl, key=lambda x: x['score'])
|
| 35 |
nn = "{:.2f}".format(max_item['score']) # تنسيق الدقة ليكون مقروءًا بشكل أفضل
|
| 36 |
dd = max_item['label']
|
| 37 |
+
|
| 38 |
+
# إرجاع النتيجة بصيغة JSON
|
| 39 |
+
return json.dumps({"label": dd, "score": nn})
|
| 40 |
|
| 41 |
except requests.exceptions.RequestException as e:
|
| 42 |
+
return json.dumps({"error": f"خطأ في تحميل الصورة من الرابط المقدم: {e}"})
|
| 43 |
except UnidentifiedImageError:
|
| 44 |
+
return json.dumps({"error": "لا يمكن تحديد نوع الصورة من الرابط المقدم."})
|
| 45 |
except Exception as e:
|
| 46 |
+
return json.dumps({"error": f"حدث خطأ أثناء معالجة الصورة: {e}"})
|
| 47 |
|
| 48 |
# إنشاء واجهة Gradio باستخدام المكونات الجديدة
|
| 49 |
iface = gr.Interface(
|
| 50 |
fn=predict,
|
| 51 |
inputs=gr.Textbox(lines=2, placeholder="أدخل رابط الصورة هنا...", label="رابط الصورة"),
|
| 52 |
+
outputs="text", # مخرجات النص تُستخدم هنا لعرض نص الـ JSON
|
| 53 |
title="نموذج ViT لتصنيف الصور",
|
| 54 |
description="أدخل رابط صورة للحصول على تصنيف باستخدام نموذج ViT المدرب."
|
| 55 |
)
|