Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 9 |
-
|
| 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 |
-
|
| 16 |
-
|
| 17 |
-
labels = response.json()
|
| 18 |
if isinstance(labels, dict):
|
| 19 |
labels = list(labels.values())
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
labels = None
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def predict(text):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
demo = gr.Interface(
|
| 38 |
fn=predict,
|
| 39 |
-
inputs=gr.Textbox(lines=3, placeholder="
|
| 40 |
outputs=[
|
| 41 |
gr.Textbox(label="Prediction (True/False)"),
|
| 42 |
gr.Textbox(label="Full JSON Output")
|
| 43 |
],
|
| 44 |
title="Egyptian Text Classification",
|
| 45 |
-
description="
|
| 46 |
)
|
| 47 |
|
| 48 |
-
# FastAPI
|
| 49 |
app = FastAPI()
|
| 50 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 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="/")
|