Spaces:
Runtime error
Runtime error
File size: 1,999 Bytes
7e0e380 0551bb3 7e0e380 0551bb3 7e0e380 83f34c3 7e0e380 0551bb3 7e0e380 0551bb3 7e0e380 83f34c3 7e0e380 0551bb3 83f34c3 7e0e380 83f34c3 0551bb3 7e0e380 83f34c3 2bb51ab 83f34c3 0551bb3 7e0e380 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import os
import torch
import gradio as gr
from transformers import (
AutoTokenizer,
AutoModelForSequenceClassification,
TextClassificationPipeline
)
# === Config ===
MODEL_ID = "Omartificial-Intelligence-Space/SA-BERT-Classifier"
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
DEVICE = 0 if torch.cuda.is_available() else -1
# === Load model and tokenizer ===
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, use_auth_token=HF_TOKEN)
model = AutoModelForSequenceClassification.from_pretrained(
MODEL_ID, use_auth_token=HF_TOKEN
).to("cuda" if DEVICE == 0 else "cpu")
# === Build pipeline ===
pipeline = TextClassificationPipeline(
model=model,
tokenizer=tokenizer,
device=DEVICE,
top_k=None # replaces deprecated return_all_scores
)
# === Inference function ===
def classify_dialect(text):
results = pipeline(text)[0]
scores = {int(item["label"].split("_")[-1]): item["score"] for item in results}
p_non_saudi = scores.get(0, 0.0)
p_saudi = scores.get(1, 0.0)
prediction = "Saudi Dialect" if p_saudi > p_non_saudi else "Non-Saudi Dialect"
return round(p_saudi, 4), round(p_non_saudi, 4), prediction
# === Gradio Interface ===
demo = gr.Interface(
fn=classify_dialect,
inputs=gr.Textbox(lines=2, placeholder="اكتب جملة باللهجة العربية هنا..."),
outputs=[
gr.Label(label="Saudi Dialect (Probability)"),
gr.Label(label="Non-Saudi Dialect (Probability)"),
gr.Textbox(label="Final Prediction")
],
title="🗣️ Saudi Dialect Classifier",
description="🔍 نموذج BERT لتصنيف الجمل إلى لهجة سعودية أو غير سعودية.\n\n👩💻 Deployed by **Ayesha Shafique** [LinkedIn](https://www.linkedin.com/in/aieeshashafique/)\n\n🌐 Model credit: [Omartificial-Intelligence-Space](https://huggingface.co/Omartificial-Intelligence-Space)",
allow_flagging="never"
)
# === Launch App ===
if __name__ == "__main__":
demo.launch()
|