Spaces:
Sleeping
Sleeping
first commit
Browse files
app.py
CHANGED
|
@@ -3,20 +3,29 @@
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
# تحميل نموذج
|
| 7 |
model_name = "HooshvareLab/bert-fa-base-uncased-sentiment-digikala"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 10 |
|
| 11 |
-
# إنشاء
|
| 12 |
-
sentiment_analyzer = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
def analyze_sentiment(text):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# واجهة Gradio
|
| 22 |
iface = gr.Interface(
|
|
@@ -24,7 +33,7 @@ iface = gr.Interface(
|
|
| 24 |
inputs=gr.Textbox(lines=2, placeholder="متن خود را وارد کنید..."),
|
| 25 |
outputs="text",
|
| 26 |
title="تحلیل احساسات متون فارسی",
|
| 27 |
-
description="
|
| 28 |
)
|
| 29 |
|
| 30 |
-
iface.launch()
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# تحميل نموذج التحليل الفارسي
|
| 7 |
model_name = "HooshvareLab/bert-fa-base-uncased-sentiment-digikala"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 10 |
|
| 11 |
+
# إنشاء الـ pipeline
|
| 12 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer, return_all_scores=True)
|
| 13 |
|
| 14 |
+
# دالة التحليل
|
| 15 |
def analyze_sentiment(text):
|
| 16 |
+
results = sentiment_analyzer(text)[0] # نجيب كل النتائج (إيجابي، سلبي، محايد)
|
| 17 |
+
output = ""
|
| 18 |
+
for r in results:
|
| 19 |
+
label = r["label"]
|
| 20 |
+
score = round(r["score"], 3)
|
| 21 |
+
if label.lower() == "positive":
|
| 22 |
+
label_fa = "مثبت (إيجابي)"
|
| 23 |
+
elif label.lower() == "negative":
|
| 24 |
+
label_fa = "منفی (سلبي)"
|
| 25 |
+
else:
|
| 26 |
+
label_fa = "خنثی (محايد)"
|
| 27 |
+
output += f"{label_fa}: {score}\n"
|
| 28 |
+
return output
|
| 29 |
|
| 30 |
# واجهة Gradio
|
| 31 |
iface = gr.Interface(
|
|
|
|
| 33 |
inputs=gr.Textbox(lines=2, placeholder="متن خود را وارد کنید..."),
|
| 34 |
outputs="text",
|
| 35 |
title="تحلیل احساسات متون فارسی",
|
| 36 |
+
description="نمایش احتمال مثبت، منفی و خنثی بودن متن فارسی."
|
| 37 |
)
|
| 38 |
|
| 39 |
+
iface.launch()
|