Woolv7007 commited on
Commit
9ebdafe
·
verified ·
1 Parent(s): 708e422

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -14,7 +14,7 @@ try:
14
  labels = response.json()
15
  if isinstance(labels, dict):
16
  labels = list(labels.values())
17
- except requests.exceptions.RequestException as e:
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
- result = pipe(text)[0]
27
- label_id = int(result['label'].replace("LABEL_", "")) # تحويل LABEL_3 إلى 3
28
- label = labels[label_id] if labels and label_id < len(labels) else result['label']
29
- confidence = round(result['score'], 3)
 
30
 
31
- json_output = {
32
- "prediction": label,
33
- "confidence": confidence
34
- }
35
 
36
- return label, json.dumps(json_output, indent=4, ensure_ascii=False)
 
 
 
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
- ).launch(share=True)
 
 
 
 
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()