Spaces:
Sleeping

Mk / app.py
Mahmoud-Elhadry's picture
Update app.py
510d51f verified
Raw History Blame Contribute Delete
2.18 kB
import gradio as gr
import google.generativeai as genai
import threading
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters
# ==========================================
# 🛑 مفاتيحك (جاهزة)
# ==========================================
GOOGLE_API_KEY = "AIzaSyCMv_BapYh2e57p6HNG0FLdi_Mu7RC_yDo"
TELEGRAM_BOT_TOKEN = "8574416672:AAEt3aOJBv99stk1cOx8rsQ4WCLBXW3Hbsk"
# إعداد نموذج جوجل
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel('gemini-2.5-flash')
# ==========================================
# كود البوت
# ==========================================
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(
chat_id=update.effective_chat.id,
text="هلا! أنا رجعت اشتغل على Hugging Face بقوة 🚀"
)
async def chat_gemini(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_text = update.message.text
chat_id = update.effective_chat.id
# يكتب...
await context.bot.send_chat_action(chat_id=chat_id, action="typing")
try:
response = model.generate_content(user_text)
reply = response.text
except Exception:
reply = "حدث خطأ في الاتصال بجوجل."
await context.bot.send_message(chat_id=chat_id, text=reply)
# دالة التشغيل في الخلفية
def run_bot():
app = ApplicationBuilder().token(TELEGRAM_BOT_TOKEN).build()
app.add_handler(CommandHandler('start', start))
app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), chat_gemini))
print("Bot is running...")
app.run_polling()
# ==========================================
# واجهة Gradio (لإبقاء الموقع حياً)
# ==========================================
if __name__ == "__main__":
# تشغيل البوت في خيط منفصل
threading.Thread(target=run_bot, daemon=True).start()
# واجهة بسيطة
with gr.Blocks() as demo:
gr.Markdown("# 🤖 Bot is Running on Hugging Face")
demo.launch()