omar3241 commited on
Commit
5481384
·
verified ·
1 Parent(s): 95ce394

first commit

Browse files
Files changed (1) hide show
  1. app.py +19 -10
app.py CHANGED
@@ -3,20 +3,29 @@
3
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
4
  import gradio as gr
5
 
6
- # تحميل نموذج تحليل المشاعر الفارسي من HooshvareLab
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
- result = sentiment_analyzer(text)[0]
17
- label = result['label']
18
- score = round(result['score'], 3)
19
- return f"نتیجه: {label} ({score})"
 
 
 
 
 
 
 
 
 
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()