Maryaa4 commited on
Commit
d6ee12d
ยท
verified ยท
1 Parent(s): 6fe8e04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -23
app.py CHANGED
@@ -1,62 +1,79 @@
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}
 
1
  import os
2
  import requests
3
  from fastapi import FastAPI, Request
 
4
 
5
+ # ุฑุงุจุท ุงู„ู€ Inference API ู„ู…ูˆุฏูŠู„ูƒ
6
+ HF_MODEL_URL = "https://api-inference.huggingface.co/models/maryaa4/my-arabic-sentiment-model"
7
 
8
+ HF_TOKEN = os.getenv("HF_TOKEN")
 
 
 
 
 
 
 
 
 
9
  BOT_TOKEN = os.getenv("TELEGRAM_TOKEN")
10
+
11
  TELEGRAM_API = (
12
  f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
13
  if BOT_TOKEN
14
  else None
15
  )
16
 
17
+ app = FastAPI()
18
+
19
+ def call_hf_model(text: str) -> str:
20
+ if not HF_TOKEN:
21
+ return "โŒ ู…ูู‚ูˆุฏ HF_TOKEN ููŠ Secrets ุญู‚ ุงู„ุณุจูŠุณ"
22
+
23
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
24
+ payload = {"inputs": text}
25
+
26
+ resp = requests.post(HF_MODEL_URL, headers=headers, json=payload)
27
 
28
+ if resp.status_code != 200:
29
+ return f"โŒ Error from HF API: {resp.status_code} - {resp.text}"
30
+
31
+ data = resp.json()
32
+ # ู…ุชูˆู‚ุน ูŠุฑุฌุน list ููŠู‡ุง dict ุฒูŠ: [{"label": "...", "score": 0.9}]
33
+ try:
34
+ result = data[0]
35
+ label = result["label"]
36
+ score = result["score"]
37
+ except Exception:
38
+ return f"โš ๏ธ Unexpected HF response: {data}"
39
+
40
+ label_lower = label.lower()
41
+ if "pos" in label_lower:
42
  label_ar = "ุฅูŠุฌุงุจูŠ ๐ŸŒฟ"
43
+ elif "neg" in label_lower:
44
  label_ar = "ุณู„ุจูŠ ๐Ÿ’”"
45
+ elif "neu" in label_lower:
46
  label_ar = "ู…ุญุงูŠุฏ ๐Ÿ˜"
47
  else:
48
  label_ar = label
49
 
50
  return f"ุงู„ุชุตู†ูŠู: {label_ar}\nู†ุณุจุฉ ุงู„ุซู‚ุฉ: {score:.2f}"
51
 
52
+ @app.get("/")
53
+ def root():
54
+ return {"status": "ok", "message": "Telegram bot webhook for Arabic sentiment is running."}
55
+
56
  @app.post("/")
57
  async def telegram_webhook(request: Request):
58
  data = await request.json()
59
+ print("๐Ÿ“ฉ Incoming update:", data)
60
 
61
  if "message" in data and "text" in data["message"]:
62
  chat_id = data["message"]["chat"]["id"]
63
  user_text = data["message"]["text"]
64
 
65
+ reply_text = call_hf_model(user_text)
66
 
67
  if TELEGRAM_API:
68
  try:
69
+ r = requests.post(
70
  TELEGRAM_API,
71
  json={"chat_id": chat_id, "text": reply_text},
72
  )
73
+ print("๐Ÿ“ค Sent to Telegram, status:", r.status_code, r.text)
74
  except Exception as e:
75
  print("Error sending message to Telegram:", e)
76
+ else:
77
+ print("โŒ TELEGRAM_API is None (ู…ุง ู„ู‚ูŠุช TELEGRAM_TOKEN ููŠ Secrets)")
78
 
79
  return {"ok": True}