Woolv7007 commited on
Commit
7eb94fc
·
verified ·
1 Parent(s): 7769be8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -24
app.py CHANGED
@@ -3,45 +3,52 @@ from transformers import pipeline
3
  import requests
4
  import json
5
 
6
- # اسم النموذج
7
  model_name = "Woolv7007/egyptian-text-classification"
8
 
9
- # تحميل labels.json
10
  labels_url = f"https://huggingface.co/{model_name}/resolve/main/labels.json"
11
  try:
12
  response = requests.get(labels_url)
13
  response.raise_for_status()
14
  labels = response.json()
 
 
15
  if isinstance(labels, dict):
16
  labels = list(labels.values())
17
- print("✅ Labels loaded:", labels)
 
 
18
  except requests.exceptions.RequestException as e:
19
- print(" فشل تحميل labels.json:", e)
20
  labels = None
21
 
22
- # تحميل النموذج
23
  pipe = pipeline("text-classification", model=model_name)
24
- print("Model pipeline loaded.")
25
 
26
- # دالة التنبؤ
27
  def predict(text):
28
- print("🎯 Received:", text)
 
29
  try:
 
30
  result = pipe(text)[0]
31
- print("⚙️ Pipeline result:", result)
32
 
33
- # استخراج رقم التصنيف
34
  label_id = int(result['label'].replace("LABEL_", ""))
35
- print("🔢 Label ID:", label_id)
36
 
37
- # استخراج الاسم من labels
38
  label_text = labels[label_id] if labels and label_id < len(labels) else result['label']
39
- print("🏷️ Final label:", label_text)
40
 
41
- # الثقة
42
  confidence = round(result['score'], 3)
43
- print("📊 Confidence:", confidence)
44
 
 
45
  json_output = {
46
  "prediction": label_text,
47
  "confidence": confidence
@@ -50,18 +57,17 @@ def predict(text):
50
  return label_text, json.dumps(json_output, indent=4, ensure_ascii=False)
51
 
52
  except Exception as e:
53
- err_msg = str(e)
54
- print("Error in predict:", err_msg)
55
- return "Error", json.dumps({"error": err_msg}, indent=4, ensure_ascii=False)
56
 
57
- # واجهة Gradio
58
  gr.Interface(
59
  fn=predict,
60
- inputs=gr.Textbox(lines=3, placeholder="اكتب جملة باللهجة المصرية..."),
61
  outputs=[
62
- gr.Label(label="التصنيف المتوقع"),
63
- gr.Textbox(label="النتيجة بصيغة JSON")
64
  ],
65
- title="🧠 تصنيف النصوص باللهجة المصرية",
66
- description="🔍 نموذج يصنف النصوص إلى: حياد، إساءة، عنصرية، تمييز ديني، إعلانات، إلخ."
67
  ).launch()
 
3
  import requests
4
  import json
5
 
6
+ # Model name on Hugging Face
7
  model_name = "Woolv7007/egyptian-text-classification"
8
 
9
+ # Load label names from labels.json
10
  labels_url = f"https://huggingface.co/{model_name}/resolve/main/labels.json"
11
  try:
12
  response = requests.get(labels_url)
13
  response.raise_for_status()
14
  labels = response.json()
15
+
16
+ # Convert to list if it's a dictionary
17
  if isinstance(labels, dict):
18
  labels = list(labels.values())
19
+
20
+ print("Labels loaded:", labels)
21
+
22
  except requests.exceptions.RequestException as e:
23
+ print("Failed to load labels.json:", e)
24
  labels = None
25
 
26
+ # Load the text classification pipeline
27
  pipe = pipeline("text-classification", model=model_name)
28
+ print("Model pipeline loaded.")
29
 
30
+ # Define the prediction function
31
  def predict(text):
32
+ print("Input text:", text)
33
+
34
  try:
35
+ # Run prediction
36
  result = pipe(text)[0]
37
+ print("Model output:", result)
38
 
39
+ # Extract label index from format like "LABEL_3"
40
  label_id = int(result['label'].replace("LABEL_", ""))
41
+ print("Label ID:", label_id)
42
 
43
+ # Get the label name using the index
44
  label_text = labels[label_id] if labels and label_id < len(labels) else result['label']
45
+ print("Final label:", label_text)
46
 
47
+ # Get the confidence score
48
  confidence = round(result['score'], 3)
49
+ print("Confidence:", confidence)
50
 
51
+ # Build JSON result
52
  json_output = {
53
  "prediction": label_text,
54
  "confidence": confidence
 
57
  return label_text, json.dumps(json_output, indent=4, ensure_ascii=False)
58
 
59
  except Exception as e:
60
+ print("Error:", e)
61
+ return "Error", json.dumps({"error": str(e)}, indent=4, ensure_ascii=False)
 
62
 
63
+ # Create the Gradio interface
64
  gr.Interface(
65
  fn=predict,
66
+ inputs=gr.Textbox(lines=3, placeholder="Enter a sentence in Egyptian Arabic..."),
67
  outputs=[
68
+ gr.Label(label="Predicted Category"),
69
+ gr.Textbox(label="JSON Output")
70
  ],
71
+ title="Egyptian Arabic Text Classifier",
72
+ description="This model classifies Egyptian Arabic text into categories such as Neutral, Offensive, Racist, Religious Discrimination, Ads, etc."
73
  ).launch()