Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 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 |
-
|
|
|
|
|
|
|
| 18 |
except requests.exceptions.RequestException as e:
|
| 19 |
-
print("
|
| 20 |
labels = None
|
| 21 |
|
| 22 |
-
#
|
| 23 |
pipe = pipeline("text-classification", model=model_name)
|
| 24 |
-
print("
|
| 25 |
|
| 26 |
-
#
|
| 27 |
def predict(text):
|
| 28 |
-
print("
|
|
|
|
| 29 |
try:
|
|
|
|
| 30 |
result = pipe(text)[0]
|
| 31 |
-
print("
|
| 32 |
|
| 33 |
-
#
|
| 34 |
label_id = int(result['label'].replace("LABEL_", ""))
|
| 35 |
-
print("
|
| 36 |
|
| 37 |
-
#
|
| 38 |
label_text = labels[label_id] if labels and label_id < len(labels) else result['label']
|
| 39 |
-
print("
|
| 40 |
|
| 41 |
-
#
|
| 42 |
confidence = round(result['score'], 3)
|
| 43 |
-
print("
|
| 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 |
-
|
| 54 |
-
|
| 55 |
-
return "Error", json.dumps({"error": err_msg}, indent=4, ensure_ascii=False)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
gr.Interface(
|
| 59 |
fn=predict,
|
| 60 |
-
inputs=gr.Textbox(lines=3, placeholder="
|
| 61 |
outputs=[
|
| 62 |
-
gr.Label(label="
|
| 63 |
-
gr.Textbox(label="
|
| 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()
|