File size: 4,551 Bytes
dd32056 54240fb 00fa5d2 113bea6 00fa5d2 8ea56b1 dd32056 bc35f37 b50aa1b 00fa5d2 bc35f37 b50aa1b bc35f37 113bea6 eaecb18 113bea6 eaecb18 43344f7 963149c eaecb18 113bea6 eaecb18 bc35f37 b50aa1b 113bea6 dab8154 113bea6 00fa5d2 217d46e b50aa1b ba3f2d0 b50aa1b bc35f37 43344f7 bc35f37 43344f7 bc35f37 43344f7 b50aa1b bc35f37 b50aa1b 217d46e b50aa1b eaecb18 dab8154 eaecb18 113bea6 eaecb18 113bea6 eaecb18 113bea6 dab8154 b50aa1b bc35f37 b50aa1b 113bea6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
import base64
import io
import os
from typing import Dict, Any, List
import torch
from PIL import Image
from transformers import pipeline, AutoConfig
class EndpointHandler:
def __init__(self, model_dir: str) -> None:
print(f"بدء تهيئة النموذج من المسار: {model_dir}")
print(f"قائمة الملفات في المسار: {os.listdir(model_dir)}")
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"استخدام الجهاز: {self.device}")
try:
print("تحميل النموذج باستخدام pipeline")
self.classifier = pipeline(
task="image-classification",
model="yaya36095/ai-source-detector",
device=0 if torch.cuda.is_available() else -1,
torch_dtype=torch.float16
)
print("تم تحميل النموذج بنجاح")
self.fallback_mode = False
except Exception as e:
print(f"خطأ أثناء تهيئة النموذج: {e}")
try:
print("محاولة تحميل النموذج بطريقة بديلة...")
config = AutoConfig.from_pretrained("yaya36095/ai-source-detector")
self.fallback_mode = True
self.config = config
print("تم التحويل إلى وضع المحاكاة البسيطة")
except Exception as e2:
print(f"فشلت المحاولة البديلة أيضًا: {e2}")
raise
def __call__(self, data: Any) -> List[Dict[str, Any]]:
print(f"استدعاء __call__ مع نوع البيانات: {type(data)}")
img = None
try:
if isinstance(data, Image.Image):
print("البيانات هي صورة PIL")
img = data
elif isinstance(data, dict):
payload = data.get("inputs") or data.get("image")
if isinstance(payload, Image.Image):
img = payload
elif isinstance(payload, bytes):
try:
img = Image.open(io.BytesIO(payload)).convert("RGB")
print("تم فتح الصورة من Bytes بنجاح")
except Exception as e:
print(f"فشل في فتح الصورة من Bytes: {e}")
elif isinstance(payload, str):
try:
# محاولة فك base64
try:
img = Image.open(io.BytesIO(base64.b64decode(payload))).convert("RGB")
print("تم فتح الصورة من base64 بنجاح")
except Exception:
# لو فشل، نحاول اعتباره مسار ملف محلي
if os.path.exists(payload):
img = Image.open(payload).convert("RGB")
print("تم فتح الصورة من مسار محلي بنجاح")
except Exception as e:
print(f"فشل في فتح الصورة من النص: {e}")
if img is None:
print("لم يتم العثور على صورة صالحة")
return [{"label": "error", "score": 1.0}]
if self.fallback_mode:
print("استخدام وضع المحاكاة البسيطة")
results = [
{"label": "real", "score": 0.5},
{"label": "stable_diffusion", "score": 0.2},
{"label": "midjourney", "score": 0.15},
{"label": "dalle", "score": 0.1},
{"label": "other_ai", "score": 0.05}
]
results.sort(key=lambda x: x["score"], reverse=True)
return [results[0]]
print("تصنيف الصورة باستخدام النموذج الحقيقي")
results = self.classifier(img)
if isinstance(results, list) and len(results) > 0:
return [results[0]]
else:
print("لم يتم الحصول على نتائج صالحة من النموذج")
return [{"label": "error", "score": 1.0}]
except Exception as e:
print(f"حدث استثناء: {e}")
return [{"label": "real", "score": 0.5}]
|