Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 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 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
label_ar = "ุฅูุฌุงุจู ๐ฟ"
|
| 34 |
-
elif
|
| 35 |
label_ar = "ุณูุจู ๐"
|
| 36 |
-
elif
|
| 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 =
|
| 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}
|