Woolv7007 commited on
Commit
eb9d9bc
·
verified ·
1 Parent(s): e213720

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -29
app.py CHANGED
@@ -1,50 +1,59 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import requests
4
  import json
 
5
  from fastapi import FastAPI
6
- from gradio.routes import App as GradioApp
7
 
8
- # Model
9
- model_name = "Woolv7007/egyptian-text-classification"
10
- pipe = pipeline("text-classification", model=model_name)
11
-
12
- # Load labels
13
- labels_url = f"https://huggingface.co/{model_name}/resolve/main/labels.json"
14
  try:
15
- response = requests.get(labels_url)
16
- response.raise_for_status()
17
- labels = response.json()
18
  if isinstance(labels, dict):
19
  labels = list(labels.values())
20
- except:
 
 
21
  labels = None
22
 
 
 
 
 
 
 
23
  def predict(text):
24
- result = pipe(text)[0]
25
- label_id = int(result['label'].replace("LABEL_", ""))
26
- label_text = labels[label_id] if labels and label_id < len(labels) else result['label']
27
- prediction_bool = label_text.lower() in ["ads", "neutral"]
28
- confidence = round(result['score'], 3)
29
-
30
- return str(prediction_bool), json.dumps({
31
- "prediction": prediction_bool,
32
- "original_label": label_text,
33
- "confidence": confidence
34
- }, indent=4, ensure_ascii=False)
35
-
36
- # Gradio Interface
 
 
 
 
 
 
 
37
  demo = gr.Interface(
38
  fn=predict,
39
- inputs=gr.Textbox(lines=3, placeholder="Enter Egyptian Arabic text..."),
40
  outputs=[
41
  gr.Textbox(label="Prediction (True/False)"),
42
  gr.Textbox(label="Full JSON Output")
43
  ],
44
  title="Egyptian Text Classification",
45
- description="Only 'ads' and 'neutral' are considered True."
46
  )
47
 
48
- # FastAPI + Gradio معًا
49
  app = FastAPI()
50
- app = gr.mount_gradio_app(app, demo, path="/") # UI on "/"
 
1
  import gradio as gr
2
  from transformers import pipeline
 
3
  import json
4
+ import os
5
  from fastapi import FastAPI
 
6
 
7
+ # تحميل التصنيفات من ملف محلي
8
+ labels_path = os.path.join(os.path.dirname(__file__), "labels.json")
 
 
 
 
9
  try:
10
+ with open(labels_path, "r", encoding="utf-8") as f:
11
+ labels = json.load(f)
 
12
  if isinstance(labels, dict):
13
  labels = list(labels.values())
14
+ print("Labels loaded:", labels)
15
+ except Exception as e:
16
+ print("Failed to load local labels.json:", e)
17
  labels = None
18
 
19
+ # تحميل النموذج
20
+ model_name = "Woolv7007/egyptian-text-classification"
21
+ pipe = pipeline("text-classification", model=model_name)
22
+ print("Model loaded.")
23
+
24
+ # دالة التنبؤ
25
  def predict(text):
26
+ try:
27
+ result = pipe(text)[0]
28
+ label_id = int(result['label'].replace("LABEL_", ""))
29
+ label_text = labels[label_id] if labels and label_id < len(labels) else result['label']
30
+ prediction_bool = label_text.lower() in ["ads", "neutral"]
31
+ confidence = round(result['score'], 3)
32
+
33
+ json_output = {
34
+ "prediction": prediction_bool,
35
+ "original_label": label_text,
36
+ "confidence": confidence
37
+ }
38
+
39
+ return str(prediction_bool), json.dumps(json_output, indent=4, ensure_ascii=False)
40
+
41
+ except Exception as e:
42
+ error_msg = str(e)
43
+ return "Error", json.dumps({"error": error_msg}, indent=4, ensure_ascii=False)
44
+
45
+ # واجهة Gradio
46
  demo = gr.Interface(
47
  fn=predict,
48
+ inputs=gr.Textbox(lines=3, placeholder="أدخل نصًا باللهجة المصرية..."),
49
  outputs=[
50
  gr.Textbox(label="Prediction (True/False)"),
51
  gr.Textbox(label="Full JSON Output")
52
  ],
53
  title="Egyptian Text Classification",
54
+ description="هذا النموذج يصنف النصوص المكتوبة باللهجة المصرية. فقط التصنيفات 'ads' و 'neutral' تعتبر True."
55
  )
56
 
57
+ # إنشاء تطبيق FastAPI وتوصيله بـ Gradio
58
  app = FastAPI()
59
+ app = gr.mount_gradio_app(app, demo, path="/")