File size: 1,475 Bytes
e325f0e
 
 
 
 
c1cc37c
69336a1
 
e325f0e
 
 
c1cc37c
e325f0e
 
c1cc37c
6a30518
c1cc37c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5481384
e325f0e
 
 
 
 
c1cc37c
 
e325f0e
 
69336a1
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
# 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()