| from functools import lru_cache
|
|
|
| import gradio as gr
|
| from transformers import pipeline
|
|
|
| MODEL_NAME = "TheMohanad1/marbert-arabic-sentiment-analyzer"
|
|
|
|
|
| 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() |