sentiment-test / app.py
omar3241's picture
Update app.py
c1cc37c verified
# app.py
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
import gradio as gr
# تحميل الموديل الفارسي
model_name = "HooshvareLab/bert-fa-base-uncased-sentiment-snappfood"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
sentiment_analyzer = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
def analyze_sentiment(text):
result = sentiment_analyzer(text)[0]
label = result["label"]
score = result["score"]
# نحسب الاحتمالات بشكل يدوي لو الموديل فيه فئتين فقط
if label.upper() == "HAPPY":
positive = round(score, 3)
negative = round(1 - score, 3)
else:
negative = round(score, 3)
positive = round(1 - score, 3)
neutral = round(1 - (positive + negative), 3)
if neutral < 0:
neutral = 0.0 # علشان ميطلعش سالب بالخطأ
output = f"مثبت (إيجابي): {positive}\nمنفی (سلبي): {negative}\nخنثی (محايد): {neutral}"
return output
iface = gr.Interface(
fn=analyze_sentiment,
inputs=gr.Textbox(lines=2, placeholder="متن خود را وارد کنید..."),
outputs="text",
title="تحلیل احساسات فارسی",
description="تحلیل متن به فارسی و نمایش احتمال مثبت، منفی و خنثی."
)
iface.launch()