yaya36095 commited on
Commit
43344f7
·
verified ·
1 Parent(s): eaecb18

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +27 -13
handler.py CHANGED
@@ -21,7 +21,7 @@ class EndpointHandler:
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("تم تحميل النموذج بنجاح")
@@ -39,27 +39,41 @@ class EndpointHandler:
39
  print(f"فشلت المحاولة البديلة أيضًا: {e2}")
40
  raise
41
 
42
- def _decode_b64(self, b: bytes) -> Image.Image:
43
- try:
44
- print(f"فك ترميز base64. حجم البيانات: {len(b)} بايت")
45
- return Image.open(io.BytesIO(base64.b64decode(b))).convert("RGB")
46
- except Exception as e:
47
- print(f"خطأ في فك الترميز: {e}")
48
- raise
49
-
50
  def __call__(self, data: Any) -> List[Dict[str, Any]]:
51
  print(f"استدعاء __call__ مع نوع البيانات: {type(data)}")
52
 
53
  img = None
54
  try:
55
  if isinstance(data, Image.Image):
 
56
  img = data
 
57
  elif isinstance(data, dict):
58
  payload = data.get("inputs") or data.get("image")
59
- if isinstance(payload, str):
60
- payload = payload.encode()
61
- if isinstance(payload, bytes):
62
- img = self._decode_b64(payload)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  if img is None:
65
  print("لم يتم العثور على صورة صالحة")
 
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("تم تحميل النموذج بنجاح")
 
39
  print(f"فشلت المحاولة البديلة أيضًا: {e2}")
40
  raise
41
 
 
 
 
 
 
 
 
 
42
  def __call__(self, data: Any) -> List[Dict[str, Any]]:
43
  print(f"استدعاء __call__ مع نوع البيانات: {type(data)}")
44
 
45
  img = None
46
  try:
47
  if isinstance(data, Image.Image):
48
+ print("البيانات هي صورة PIL")
49
  img = data
50
+
51
  elif isinstance(data, dict):
52
  payload = data.get("inputs") or data.get("image")
53
+
54
+ if isinstance(payload, Image.Image):
55
+ img = payload
56
+
57
+ elif isinstance(payload, bytes):
58
+ try:
59
+ img = Image.open(io.BytesIO(payload)).convert("RGB")
60
+ print("تم فتح الصورة من Bytes بنجاح")
61
+ except Exception as e:
62
+ print(f"فشل في فتح الصورة من Bytes: {e}")
63
+
64
+ elif isinstance(payload, str):
65
+ try:
66
+ # محاولة فك base64
67
+ try:
68
+ img = Image.open(io.BytesIO(base64.b64decode(payload))).convert("RGB")
69
+ print("تم فتح الصورة من base64 بنجاح")
70
+ except Exception:
71
+ # لو فشل، نحاول اعتباره مسار ملف محلي
72
+ if os.path.exists(payload):
73
+ img = Image.open(payload).convert("RGB")
74
+ print("تم فتح الصورة من مسار محلي بنجاح")
75
+ except Exception as e:
76
+ print(f"فشل في فتح الصورة من النص: {e}")
77
 
78
  if img is None:
79
  print("لم يتم العثور على صورة صالحة")