Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,21 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import threading
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import pipeline
|
| 5 |
-
import telebot # pyTelegramBotAPI
|
| 6 |
-
|
| 7 |
-
# ---------- 1) إعداد نموذج المشاعر ----------
|
| 8 |
-
MODEL_ID = "Nadasr/sentAnalysisModel"
|
| 9 |
|
| 10 |
sentiment_pipeline = pipeline(
|
| 11 |
"text-classification",
|
| 12 |
-
model=
|
| 13 |
-
tokenizer=
|
| 14 |
return_all_scores=False # يرجع أفضل لابل فقط
|
| 15 |
)
|
| 16 |
|
| 17 |
-
def predict_sentiment(text
|
| 18 |
text = text.strip()
|
| 19 |
if not text:
|
| 20 |
return "رجاءً أدخل نصاً للتصنيف 🙂"
|
| 21 |
|
| 22 |
result = sentiment_pipeline(text)[0]
|
| 23 |
label = result["label"]
|
| 24 |
-
score = round(
|
| 25 |
|
| 26 |
if label in ["LABEL_1", "POSITIVE", "positive"]:
|
| 27 |
label_ar = "إيجابي 👍"
|
|
@@ -32,8 +26,6 @@ def predict_sentiment(text: str) -> str:
|
|
| 32 |
|
| 33 |
return f"{label_ar} (الاحتمال = {score})"
|
| 34 |
|
| 35 |
-
|
| 36 |
-
# ---------- 2) واجهة Gradio ----------
|
| 37 |
demo = gr.Interface(
|
| 38 |
fn=predict_sentiment,
|
| 39 |
inputs=gr.Textbox(lines=4, label="أدخل النص العربي هنا"),
|
|
@@ -42,40 +34,5 @@ demo = gr.Interface(
|
|
| 42 |
description="نموذج لتحليل المشاعر للنصوص العربية (إيجابي / سلبي)."
|
| 43 |
)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
# ---------- 3) بوت تيليجرام ----------TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
|
| 47 |
-
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
|
| 48 |
-
|
| 49 |
-
bot = telebot.TeleBot(TELEGRAM_TOKEN) if TELEGRAM_TOKEN else None
|
| 50 |
-
|
| 51 |
-
if bot:
|
| 52 |
-
@bot.message_handler(commands=['start', 'help'])
|
| 53 |
-
def send_welcome(message):
|
| 54 |
-
bot.reply_to(
|
| 55 |
-
message,
|
| 56 |
-
"مرحباً 👋\n"
|
| 57 |
-
"أرسل لي أي نص عربي وسأحاول تحليل المشاعر (إيجابي / سلبي)."
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
@bot.message_handler(func=lambda m: True)
|
| 61 |
-
def handle_text(message):
|
| 62 |
-
user_text = message.text or ""
|
| 63 |
-
reply = predict_sentiment(user_text)
|
| 64 |
-
bot.reply_to(message, reply)
|
| 65 |
-
|
| 66 |
-
def run_telegram_bot():
|
| 67 |
-
print("🚀 Starting Telegram bot polling ...")
|
| 68 |
-
bot.infinity_polling()
|
| 69 |
-
else:
|
| 70 |
-
def run_telegram_bot():
|
| 71 |
-
print("⚠️ TELEGRAM_BOT_TOKEN not set. Telegram bot will NOT start.")
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
# ---------- 4) تشغيل الاثنين معاً ----------
|
| 75 |
if __name__ == "__main__":
|
| 76 |
-
|
| 77 |
-
t = threading.Thread(target=run_telegram_bot, daemon=True)
|
| 78 |
-
t.start()
|
| 79 |
-
|
| 80 |
-
# تشغيل واجهة Gradio (اللي يحتاجها Hugging Face Space)
|
| 81 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
sentiment_pipeline = pipeline(
|
| 5 |
"text-classification",
|
| 6 |
+
model="Nadasr/sentAnalysisModel",
|
| 7 |
+
tokenizer="Nadasr/sentAnalysisModel",
|
| 8 |
return_all_scores=False # يرجع أفضل لابل فقط
|
| 9 |
)
|
| 10 |
|
| 11 |
+
def predict_sentiment(text):
|
| 12 |
text = text.strip()
|
| 13 |
if not text:
|
| 14 |
return "رجاءً أدخل نصاً للتصنيف 🙂"
|
| 15 |
|
| 16 |
result = sentiment_pipeline(text)[0]
|
| 17 |
label = result["label"]
|
| 18 |
+
score = round(result["score"], 3)
|
| 19 |
|
| 20 |
if label in ["LABEL_1", "POSITIVE", "positive"]:
|
| 21 |
label_ar = "إيجابي 👍"
|
|
|
|
| 26 |
|
| 27 |
return f"{label_ar} (الاحتمال = {score})"
|
| 28 |
|
|
|
|
|
|
|
| 29 |
demo = gr.Interface(
|
| 30 |
fn=predict_sentiment,
|
| 31 |
inputs=gr.Textbox(lines=4, label="أدخل النص العربي هنا"),
|
|
|
|
| 34 |
description="نموذج لتحليل المشاعر للنصوص العربية (إيجابي / سلبي)."
|
| 35 |
)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
if __name__ == "__main__":
|
| 38 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|