Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,71 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
-
import torch
|
| 4 |
from fastapi import FastAPI, Request
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
sentiment_pipeline = pipeline(
|
| 10 |
"sentiment-analysis",
|
| 11 |
-
model=
|
| 12 |
trust_remote_code=True
|
| 13 |
)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
if not text or text.strip() == "":
|
| 17 |
-
return "رجاءً أرسل نص عربي عشان أقدر أحلله 🙂"
|
| 18 |
-
result = sentiment_pipeline(text)
|
| 19 |
-
label = result[0]["label"]
|
| 20 |
-
score = result[0]["score"]
|
| 21 |
-
return f"التصنيف: {label}\nدرجة الثقة: {score:.2f}"
|
| 22 |
|
| 23 |
-
# ========== 2) FastAPI app ==========
|
| 24 |
app = FastAPI()
|
| 25 |
|
| 26 |
-
# توكن تيليجرام من Secrets
|
| 27 |
-
BOT_TOKEN = os.getenv("TELEGRAM_TOKEN")
|
| 28 |
-
if BOT_TOKEN:
|
| 29 |
-
TELEGRAM_API = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
|
| 30 |
-
else:
|
| 31 |
-
TELEGRAM_API = None
|
| 32 |
-
print("⚠️ TELEGRAM_TOKEN is not set. Telegram replies will fail.")
|
| 33 |
|
| 34 |
@app.get("/")
|
| 35 |
-
|
| 36 |
-
return {"status": "ok", "message": "
|
|
|
|
| 37 |
|
| 38 |
@app.post("/telegram")
|
| 39 |
-
async def telegram_webhook(
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
""
|
| 43 |
-
|
| 44 |
-
print("📩 Incoming update:", data)
|
| 45 |
-
|
| 46 |
-
if "message" not in data or "text" not in data["message"]:
|
| 47 |
return {"ok": True}
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
|
| 54 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
try:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
print("✅ Sent to Telegram:", r.status_code, r.text)
|
| 62 |
except Exception as e:
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
|
|
|
| 65 |
return {"ok": True}
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
+
import torch # نتأكد إنه متوفر قبل استخدامه
|
| 4 |
from fastapi import FastAPI, Request
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
+
# ============= إعدادات البوت =============
|
| 8 |
+
|
| 9 |
+
# ⚠️ مهم: لا تحطي التوكن الحقيقي في الكود النهائي
|
| 10 |
+
# حطيه مؤقتًا الآن لو حابة تجربي، وبعدها جددي التوكن وحطيه كـ Secret من الإعدادات
|
| 11 |
+
BOT_TOKEN = "PUT_YOUR_TELEGRAM_BOT_TOKEN_HERE" # <-- غيريه بالتوكن حقك مؤقتًا
|
| 12 |
+
|
| 13 |
+
TELEGRAM_API_URL = f"https://api.telegram.org/bot{BOT_TOKEN}"
|
| 14 |
+
|
| 15 |
+
# ============= تحميل مودل المشاعر =============
|
| 16 |
+
|
| 17 |
+
MODEL_REPO_ID = "maryaa4/my-arabic-sentiment-model"
|
| 18 |
|
| 19 |
sentiment_pipeline = pipeline(
|
| 20 |
"sentiment-analysis",
|
| 21 |
+
model=MODEL_REPO_ID,
|
| 22 |
trust_remote_code=True
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# ============= إنشاء FastAPI app =============
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
| 27 |
app = FastAPI()
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
@app.get("/")
|
| 31 |
+
def read_root():
|
| 32 |
+
return {"status": "ok", "message": "Telegram sentiment bot is running 🥰"}
|
| 33 |
+
|
| 34 |
|
| 35 |
@app.post("/telegram")
|
| 36 |
+
async def telegram_webhook(update: Request):
|
| 37 |
+
data = await update.json()
|
| 38 |
+
|
| 39 |
+
# بعض أنواع التحديثات ما فيها "message" (مثل callback_query)
|
| 40 |
+
if "message" not in data:
|
|
|
|
|
|
|
|
|
|
| 41 |
return {"ok": True}
|
| 42 |
|
| 43 |
+
message = data["message"]
|
| 44 |
+
chat = message.get("chat", {})
|
| 45 |
+
chat_id = chat.get("id")
|
| 46 |
|
| 47 |
+
text = message.get("text", "")
|
| 48 |
|
| 49 |
+
if not chat_id:
|
| 50 |
+
return {"ok": False, "error": "No chat_id found"}
|
| 51 |
+
|
| 52 |
+
if not text:
|
| 53 |
+
reply_text = "أرسل لي نص عربي عشان أحلل لك المشاعر 🌟"
|
| 54 |
+
else:
|
| 55 |
try:
|
| 56 |
+
result = sentiment_pipeline(text)[0]
|
| 57 |
+
label = result["label"]
|
| 58 |
+
score = result["score"]
|
| 59 |
+
# لو اللابل عندك بالعربي من الكود السابق خليه زي ما هو
|
| 60 |
+
reply_text = f"التصنيف: {label}\nدرجة الثقة: {score:.4f}"
|
|
|
|
| 61 |
except Exception as e:
|
| 62 |
+
reply_text = f"صار خطأ أثناء التحليل: {e}"
|
| 63 |
+
|
| 64 |
+
# إرسال الرد لتيلقرام
|
| 65 |
+
requests.post(
|
| 66 |
+
f"{TELEGRAM_API_URL}/sendMessage",
|
| 67 |
+
json={"chat_id": chat_id, "text": reply_text}
|
| 68 |
+
)
|
| 69 |
|
| 70 |
+
# لازم نرجع أي JSON عشان تيليجرام يعتبر الطلب ناجح
|
| 71 |
return {"ok": True}
|