omar3241 commited on
Commit
c1cc37c
·
verified ·
1 Parent(s): 6a30518

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -23
app.py CHANGED
@@ -3,43 +3,41 @@
3
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
4
  import gradio as gr
5
 
6
- # موديل التحليل الفارسي
7
  model_name = "HooshvareLab/bert-fa-base-uncased-sentiment-snappfood"
8
 
9
- # تحميل الموديل والتوكنيزر
10
  tokenizer = AutoTokenizer.from_pretrained(model_name)
11
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
12
 
13
- # إنشاء الـ pipeline مع عرض كل النتائج
14
- sentiment_analyzer = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer, return_all_scores=True)
15
 
16
- # دالة التحليل
17
  def analyze_sentiment(text):
18
- results = sentiment_analyzer(text)[0]
19
-
20
- # احتمال تكون التسميات LABEL_0 / LABEL_1 / LABEL_2
21
- labels_map = {
22
- "LABEL_0": "منفی (سلبي)",
23
- "LABEL_1": "خنثی (محايد)",
24
- "LABEL_2": "مثبت (إيجابي)"
25
- }
26
-
27
- output = ""
28
- for r in results:
29
- label = r["label"]
30
- label_fa = labels_map.get(label, label)
31
- score = round(r["score"], 3)
32
- output += f"{label_fa}: {score}\n"
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  return output
35
 
36
- # واجهة Gradio
37
  iface = gr.Interface(
38
  fn=analyze_sentiment,
39
  inputs=gr.Textbox(lines=2, placeholder="متن خود را وارد کنید..."),
40
  outputs="text",
41
- title="تحلیل احساسات متون فارسی",
42
- description="نمایش احتمال مثبت، منفی و خنثی بودن متن فارسی با مدل Snapfood."
43
  )
44
 
45
  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-snappfood"
8
 
 
9
  tokenizer = AutoTokenizer.from_pretrained(model_name)
10
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
11
 
12
+ sentiment_analyzer = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
 
13
 
 
14
  def analyze_sentiment(text):
15
+ result = sentiment_analyzer(text)[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ label = result["label"]
18
+ score = result["score"]
19
+
20
+ # نحسب الاحتمالات بشكل يدوي لو الموديل فيه فئتين فقط
21
+ if label.upper() == "HAPPY":
22
+ positive = round(score, 3)
23
+ negative = round(1 - score, 3)
24
+ else:
25
+ negative = round(score, 3)
26
+ positive = round(1 - score, 3)
27
+
28
+ neutral = round(1 - (positive + negative), 3)
29
+ if neutral < 0:
30
+ neutral = 0.0 # علشان ميطلعش سالب بالخطأ
31
+
32
+ output = f"مثبت (إيجابي): {positive}\nمنفی (سلبي): {negative}\nخنثی (محايد): {neutral}"
33
  return output
34
 
 
35
  iface = gr.Interface(
36
  fn=analyze_sentiment,
37
  inputs=gr.Textbox(lines=2, placeholder="متن خود را وارد کنید..."),
38
  outputs="text",
39
+ title="تحلیل احساسات فارسی",
40
+ description="تحلیل متن به فارسی و نمایش احتمال مثبت، منفی و خنثی."
41
  )
42
 
43
  iface.launch()