Update handler.py
Browse files- handler.py +25 -37
handler.py
CHANGED
@@ -1,37 +1,25 @@
|
|
1 |
-
import
|
2 |
-
from
|
3 |
-
from
|
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 |
-
def __call__(self, data):
|
29 |
-
image = Image.open(data["inputs"]).convert("RGB")
|
30 |
-
image = self.transform(image).unsqueeze(0)
|
31 |
-
with torch.no_grad():
|
32 |
-
outputs = self.model(image)
|
33 |
-
probs = torch.nn.functional.softmax(outputs[0], dim=0)
|
34 |
-
return {
|
35 |
-
"real": round(probs[0].item(), 4),
|
36 |
-
"fake": round(probs[1].item(), 4)
|
37 |
-
}
|
|
|
1 |
+
from typing import Dict, Any
|
2 |
+
from PIL import Image
|
3 |
+
from io import BytesIO
|
4 |
+
import base64
|
5 |
+
|
6 |
+
from inference import load_model, predict
|
7 |
+
|
8 |
+
# تحميل النموذج عند بدء السيرفر
|
9 |
+
model = load_model()
|
10 |
+
|
11 |
+
# الدالة اللي بيتم استدعاؤها عند رفع صورة
|
12 |
+
def predict_image(inputs: Dict[str, Any]) -> Dict[str, float]:
|
13 |
+
# لو كانت الصورة مرسلة كـ base64
|
14 |
+
if "image" in inputs:
|
15 |
+
image_data = inputs["image"]
|
16 |
+
if isinstance(image_data, str):
|
17 |
+
image = Image.open(BytesIO(base64.b64decode(image_data))).convert("RGB")
|
18 |
+
else:
|
19 |
+
image = Image.open(image_data).convert("RGB")
|
20 |
+
else:
|
21 |
+
raise ValueError("Missing 'image' key in input")
|
22 |
+
|
23 |
+
# التنبؤ
|
24 |
+
result = predict(model, image)
|
25 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|