Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,7 +14,7 @@ try:
|
|
14 |
labels = response.json()
|
15 |
if isinstance(labels, dict):
|
16 |
labels = list(labels.values())
|
17 |
-
except
|
18 |
print("فشل تحميل labels.json:", e)
|
19 |
labels = None
|
20 |
|
@@ -23,20 +23,24 @@ pipe = pipeline("text-classification", model=model_name)
|
|
23 |
|
24 |
# دالة التنبؤ
|
25 |
def predict(text):
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
|
|
|
|
|
|
|
37 |
|
38 |
-
# Gradio
|
39 |
-
gr.Interface(
|
40 |
fn=predict,
|
41 |
inputs=gr.Textbox(lines=3, placeholder="اكتب جملة باللهجة المصرية..."),
|
42 |
outputs=[
|
@@ -45,4 +49,7 @@ gr.Interface(
|
|
45 |
],
|
46 |
title="تصنيف النصوص باللهجة المصرية",
|
47 |
description="نموذج يصنف النصوص إلى: حياد، إساءة، عنصرية، تمييز ديني، إعلانات، إلخ."
|
48 |
-
)
|
|
|
|
|
|
|
|
14 |
labels = response.json()
|
15 |
if isinstance(labels, dict):
|
16 |
labels = list(labels.values())
|
17 |
+
except Exception as e:
|
18 |
print("فشل تحميل labels.json:", e)
|
19 |
labels = None
|
20 |
|
|
|
23 |
|
24 |
# دالة التنبؤ
|
25 |
def predict(text):
|
26 |
+
try:
|
27 |
+
result = pipe(text)[0]
|
28 |
+
label_id = int(result['label'].replace("LABEL_", "")) # تحويل LABEL_3 إلى 3
|
29 |
+
label = labels[label_id] if labels and label_id < len(labels) else result['label']
|
30 |
+
confidence = round(result['score'], 3)
|
31 |
|
32 |
+
json_output = {
|
33 |
+
"prediction": label,
|
34 |
+
"confidence": confidence
|
35 |
+
}
|
36 |
|
37 |
+
return label, json.dumps(json_output, indent=4, ensure_ascii=False)
|
38 |
+
|
39 |
+
except Exception as e:
|
40 |
+
return "خطأ في المعالجة", json.dumps({"error": str(e)}, indent=4, ensure_ascii=False)
|
41 |
|
42 |
+
# Gradio Interface
|
43 |
+
demo = gr.Interface(
|
44 |
fn=predict,
|
45 |
inputs=gr.Textbox(lines=3, placeholder="اكتب جملة باللهجة المصرية..."),
|
46 |
outputs=[
|
|
|
49 |
],
|
50 |
title="تصنيف النصوص باللهجة المصرية",
|
51 |
description="نموذج يصنف النصوص إلى: حياد، إساءة، عنصرية، تمييز ديني، إعلانات، إلخ."
|
52 |
+
)
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
demo.launch()
|