Update handler.py
Browse files- handler.py +12 -35
handler.py
CHANGED
@@ -11,39 +11,30 @@ class EndpointHandler:
|
|
11 |
print(f"بدء تهيئة النموذج من المسار: {model_dir}")
|
12 |
print(f"قائمة الملفات في المسار: {os.listdir(model_dir)}")
|
13 |
|
14 |
-
# تحديد الجهاز المستخدم (CPU في معظم بيئات Edge Function)
|
15 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
16 |
print(f"استخدام الجهاز: {self.device}")
|
17 |
|
18 |
try:
|
19 |
-
# استخدام pipeline مباشرة من Hugging Face مع تحديد خيارات تحسين الذاكرة
|
20 |
print("تحميل النموذج باستخدام pipeline")
|
21 |
-
|
22 |
-
# تحميل النموذج مباشرة من Hugging Face مع تفعيل خيارات تحسين الذاكرة
|
23 |
self.classifier = pipeline(
|
24 |
task="image-classification",
|
25 |
model="yaya36095/ai-source-detector",
|
26 |
-
device=
|
27 |
-
torch_dtype=torch.float16
|
28 |
-
low_cpu_mem_usage=True # تقليل استخدام ذاكرة CPU
|
29 |
)
|
30 |
-
|
31 |
print("تم تحميل النموذج بنجاح")
|
32 |
-
|
|
|
33 |
except Exception as e:
|
34 |
print(f"خطأ أثناء تهيئة النموذج: {e}")
|
35 |
-
# محاولة بديلة باستخدام تكوين مخصص إذا فشلت الطريقة الأولى
|
36 |
try:
|
37 |
print("محاولة تحميل النموذج بطريقة بديلة...")
|
38 |
-
|
39 |
-
# تحميل التكوين فقط (ملف صغير) بدلاً من النموذج الكامل
|
40 |
config = AutoConfig.from_pretrained("yaya36095/ai-source-detector")
|
41 |
-
|
42 |
-
# إنشاء وظيفة محاكاة بسيطة للتصنيف
|
43 |
self.fallback_mode = True
|
44 |
self.config = config
|
45 |
print("تم التحويل إلى وضع المحاكاة البسيطة")
|
46 |
-
|
47 |
except Exception as e2:
|
48 |
print(f"فشلت المحاولة البديلة أيضًا: {e2}")
|
49 |
raise
|
@@ -61,7 +52,6 @@ class EndpointHandler:
|
|
61 |
|
62 |
img = None
|
63 |
try:
|
64 |
-
# استخراج الصورة من البيانات المدخلة
|
65 |
if isinstance(data, Image.Image):
|
66 |
img = data
|
67 |
elif isinstance(data, dict):
|
@@ -75,13 +65,8 @@ class EndpointHandler:
|
|
75 |
print("لم يتم العثور على صورة صالحة")
|
76 |
return [{"label": "error", "score": 1.0}]
|
77 |
|
78 |
-
|
79 |
-
if hasattr(self, 'fallback_mode') and self.fallback_mode:
|
80 |
print("استخدام وضع المحاكاة البسيطة")
|
81 |
-
# تحليل بسيط للصورة واستخدام قيم افتراضية
|
82 |
-
# يمكن تحسين هذا الجزء بإضافة تحليل بسيط للصورة
|
83 |
-
|
84 |
-
# استخدام قيم افتراضية متوازنة
|
85 |
results = [
|
86 |
{"label": "real", "score": 0.5},
|
87 |
{"label": "stable_diffusion", "score": 0.2},
|
@@ -89,26 +74,18 @@ class EndpointHandler:
|
|
89 |
{"label": "dalle", "score": 0.1},
|
90 |
{"label": "other_ai", "score": 0.05}
|
91 |
]
|
92 |
-
|
93 |
-
# ترتيب النتائج تنازليًا حسب النتيجة
|
94 |
results.sort(key=lambda x: x["score"], reverse=True)
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
# استخدام النموذج الكامل إذا كان متاحًا
|
100 |
-
print("تصنيف الصورة باستخدام النموذج")
|
101 |
results = self.classifier(img)
|
102 |
-
|
103 |
if isinstance(results, list) and len(results) > 0:
|
104 |
-
|
105 |
-
print(f"أفضل نتيجة: {best}")
|
106 |
-
return [best]
|
107 |
else:
|
108 |
print("لم يتم الحصول على نتائج صالحة من النموذج")
|
109 |
return [{"label": "error", "score": 1.0}]
|
110 |
|
111 |
except Exception as e:
|
112 |
print(f"حدث استثناء: {e}")
|
113 |
-
# في حالة حدوث خطأ، نعود بنتيجة محايدة بدلاً من خطأ
|
114 |
return [{"label": "real", "score": 0.5}]
|
|
|
11 |
print(f"بدء تهيئة النموذج من المسار: {model_dir}")
|
12 |
print(f"قائمة الملفات في المسار: {os.listdir(model_dir)}")
|
13 |
|
|
|
14 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
15 |
print(f"استخدام الجهاز: {self.device}")
|
16 |
|
17 |
try:
|
|
|
18 |
print("تحميل النموذج باستخدام pipeline")
|
19 |
+
|
|
|
20 |
self.classifier = pipeline(
|
21 |
task="image-classification",
|
22 |
model="yaya36095/ai-source-detector",
|
23 |
+
device=0 if torch.cuda.is_available() else -1,
|
24 |
+
torch_dtype=torch.float16 # يُفضل فقط لو الجهاز يدعم
|
|
|
25 |
)
|
26 |
+
|
27 |
print("تم تحميل النموذج بنجاح")
|
28 |
+
self.fallback_mode = False
|
29 |
+
|
30 |
except Exception as e:
|
31 |
print(f"خطأ أثناء تهيئة النموذج: {e}")
|
|
|
32 |
try:
|
33 |
print("محاولة تحميل النموذج بطريقة بديلة...")
|
|
|
|
|
34 |
config = AutoConfig.from_pretrained("yaya36095/ai-source-detector")
|
|
|
|
|
35 |
self.fallback_mode = True
|
36 |
self.config = config
|
37 |
print("تم التحويل إلى وضع المحاكاة البسيطة")
|
|
|
38 |
except Exception as e2:
|
39 |
print(f"فشلت المحاولة البديلة أيضًا: {e2}")
|
40 |
raise
|
|
|
52 |
|
53 |
img = None
|
54 |
try:
|
|
|
55 |
if isinstance(data, Image.Image):
|
56 |
img = data
|
57 |
elif isinstance(data, dict):
|
|
|
65 |
print("لم يتم العثور على صورة صالحة")
|
66 |
return [{"label": "error", "score": 1.0}]
|
67 |
|
68 |
+
if self.fallback_mode:
|
|
|
69 |
print("استخدام وضع المحاكاة البسيطة")
|
|
|
|
|
|
|
|
|
70 |
results = [
|
71 |
{"label": "real", "score": 0.5},
|
72 |
{"label": "stable_diffusion", "score": 0.2},
|
|
|
74 |
{"label": "dalle", "score": 0.1},
|
75 |
{"label": "other_ai", "score": 0.05}
|
76 |
]
|
|
|
|
|
77 |
results.sort(key=lambda x: x["score"], reverse=True)
|
78 |
+
return [results[0]]
|
79 |
+
|
80 |
+
print("تصنيف الصورة باستخدام النموذج الحقيقي")
|
|
|
|
|
|
|
81 |
results = self.classifier(img)
|
82 |
+
|
83 |
if isinstance(results, list) and len(results) > 0:
|
84 |
+
return [results[0]]
|
|
|
|
|
85 |
else:
|
86 |
print("لم يتم الحصول على نتائج صالحة من النموذج")
|
87 |
return [{"label": "error", "score": 1.0}]
|
88 |
|
89 |
except Exception as e:
|
90 |
print(f"حدث استثناء: {e}")
|
|
|
91 |
return [{"label": "real", "score": 0.5}]
|