Maryaa4 commited on
Commit
3ada054
·
verified ·
1 Parent(s): 088b5a1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ from fastapi import FastAPI, Request
4
+ from transformers import pipeline
5
+
6
+ # 1) تحميل نفس موديلك من الريبو حقك
7
+ MODEL_ID = "maryaa4/my-arabic-sentiment-model"
8
+
9
+ sentiment_pipe = pipeline(
10
+ "sentiment-analysis",
11
+ model=MODEL_ID,
12
+ trust_remote_code=True,
13
+ )
14
+
15
+ # 2) تطبيق FastAPI
16
+ app = FastAPI()
17
+
18
+ # 3) توكن البوت (بنحطه في Secrets باسم TELEGRAM_TOKEN)
19
+ BOT_TOKEN = os.getenv("TELEGRAM_TOKEN")
20
+ TELEGRAM_API = (
21
+ f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
22
+ if BOT_TOKEN
23
+ else None
24
+ )
25
+
26
+ def build_reply(text: str) -> str:
27
+ result = sentiment_pipe(text)[0]
28
+ label = result["label"]
29
+ score = result["score"]
30
+
31
+ # ترجمة بسيطة للّيبَل
32
+ if label.lower() == "positive":
33
+ label_ar = "إيجابي 🌿"
34
+ elif label.lower() == "negative":
35
+ label_ar = "سلبي 💔"
36
+ elif label.lower() == "neutral":
37
+ label_ar = "محايد 😐"
38
+ else:
39
+ label_ar = label
40
+
41
+ return f"التصنيف: {label_ar}\nنسبة الثقة: {score:.2f}"
42
+
43
+ @app.post("/")
44
+ async def telegram_webhook(request: Request):
45
+ data = await request.json()
46
+
47
+ if "message" in data and "text" in data["message"]:
48
+ chat_id = data["message"]["chat"]["id"]
49
+ user_text = data["message"]["text"]
50
+
51
+ reply_text = build_reply(user_text)
52
+
53
+ if TELEGRAM_API:
54
+ try:
55
+ requests.post(
56
+ TELEGRAM_API,
57
+ json={"chat_id": chat_id, "text": reply_text},
58
+ )
59
+ except Exception as e:
60
+ print("Error sending message to Telegram:", e)
61
+
62
+ return {"ok": True}