File size: 1,055 Bytes
0cb0677 | 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 | from functools import lru_cache
import gradio as gr
from transformers import pipeline
MODEL_NAME = "TheMohanad1/marbert-arabic-sentiment-analyzer"
# Load once when the Space starts
sentiment_pipeline = pipeline(
task="text-classification",
model=MODEL_NAME,
tokenizer=MODEL_NAME,
)
@lru_cache(maxsize=1024)
def predict(text: str):
"""
Cache repeated requests for identical inputs.
"""
result = sentiment_pipeline(text)[0]
return {
"label": result["label"],
"score": round(result["score"], 4),
}
def infer(text):
text = text.strip()
if not text:
return "", 0.0
result = predict(text)
return result["label"], result["score"]
demo = gr.Interface(
fn=infer,
inputs=gr.Textbox(lines=4, placeholder="اكتب نصاً عربياً..."),
outputs=[
gr.Label(label="Sentiment"),
gr.Number(label="Confidence"),
],
title="Arabic Sentiment Analysis",
)
if __name__ == "__main__":
demo.launch() |