Nadasr commited on
Commit
ffee8e3
·
verified ·
1 Parent(s): e77e5c1

Create telegram_bot.py

Browse files
Files changed (1) hide show
  1. telegram_bot.py +41 -0
telegram_bot.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import telebot
3
+ from transformers import pipeline
4
+
5
+ # حطي التوكن كمتغير بيئة أو اكتبيه مؤقتاً هنا (بس لا ترفعيه على GitHub)
6
+ TELEGRAM_TOKEN = os.environ.get("TELEGRAM_TOKEN") # الأفضل من متغير بيئة
7
+ # أو مؤقتاً:
8
+ # TELEGRAM_TOKEN = "1234567890:ABCDEF...."
9
+
10
+ bot = telebot.TeleBot(TELEGRAM_TOKEN)
11
+
12
+ sentiment_pipeline = pipeline(
13
+ "text-classification",
14
+ model="Nadasr/sentAnalysisModel",
15
+ tokenizer="Nadasr/sentAnalysisModel",
16
+ return_all_scores=False
17
+ )
18
+
19
+ @bot.message_handler(func=lambda message: True)
20
+ def handle_message(message):
21
+ text = message.text.strip()
22
+ if not text:
23
+ bot.reply_to(message, "رجاءً أرسل نص عربي لتحليل مشاعره 🙂")
24
+ return
25
+
26
+ result = sentiment_pipeline(text)[0]
27
+ label = result["label"]
28
+ score = round(result["score"], 3)
29
+
30
+ if label in ["LABEL_1", "POSITIVE", "positive"]:
31
+ label_ar = "إيجابي 👍"
32
+ elif label in ["LABEL_0", "NEGATIVE", "negative"]:
33
+ label_ar = "سلبي 👎"
34
+ else:
35
+ label_ar = label
36
+
37
+ reply = f"التصنيف: {label_ar}\nنسبة الثقة: {score}"
38
+ bot.reply_to(message, reply)
39
+
40
+ print("Bot is running...")
41
+ bot.infinity_polling()