Update handler.py
Browse files- handler.py +5 -6
handler.py
CHANGED
|
@@ -4,23 +4,22 @@ class EndpointHandler:
|
|
| 4 |
def __init__(self, path=""):
|
| 5 |
self.model = AutoModelForSequenceClassification.from_pretrained(
|
| 6 |
path,
|
| 7 |
-
ignore_mismatched_sizes=True #
|
| 8 |
)
|
| 9 |
self.tokenizer = AutoTokenizer.from_pretrained(path)
|
| 10 |
self.pipeline = pipeline("text-classification", model=self.model, tokenizer=self.tokenizer)
|
| 11 |
|
| 12 |
def __call__(self, data):
|
| 13 |
try:
|
| 14 |
-
# نحاول نجيب النص المدخل سواء من مفتاح inputs أو من القيمة نفسها
|
| 15 |
inputs = data.get("inputs", "") if isinstance(data, dict) else data
|
| 16 |
|
| 17 |
-
# تأكد إن المدخل نص مش قائمة أو فارغ
|
| 18 |
if not isinstance(inputs, str) or not inputs.strip():
|
| 19 |
-
return {"error": "Input must be a non-empty string."}
|
| 20 |
|
| 21 |
-
# شغل النموذج
|
| 22 |
result = self.pipeline(inputs)
|
|
|
|
|
|
|
| 23 |
return result
|
| 24 |
|
| 25 |
except Exception as e:
|
| 26 |
-
return {"error": str(e)}
|
|
|
|
| 4 |
def __init__(self, path=""):
|
| 5 |
self.model = AutoModelForSequenceClassification.from_pretrained(
|
| 6 |
path,
|
| 7 |
+
ignore_mismatched_sizes=True # مهم جدًا
|
| 8 |
)
|
| 9 |
self.tokenizer = AutoTokenizer.from_pretrained(path)
|
| 10 |
self.pipeline = pipeline("text-classification", model=self.model, tokenizer=self.tokenizer)
|
| 11 |
|
| 12 |
def __call__(self, data):
|
| 13 |
try:
|
|
|
|
| 14 |
inputs = data.get("inputs", "") if isinstance(data, dict) else data
|
| 15 |
|
|
|
|
| 16 |
if not isinstance(inputs, str) or not inputs.strip():
|
| 17 |
+
return [{"error": "Input must be a non-empty string."}] # نرجعها داخل List
|
| 18 |
|
|
|
|
| 19 |
result = self.pipeline(inputs)
|
| 20 |
+
|
| 21 |
+
# نتاكد إنه list
|
| 22 |
return result
|
| 23 |
|
| 24 |
except Exception as e:
|
| 25 |
+
return [{"error": str(e)}] # نرجع الخطأ داخل List
|