omar1232 commited on
Commit
7c0956d
·
verified ·
1 Parent(s): 6f6d8b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -21
app.py CHANGED
@@ -6,38 +6,33 @@ from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
6
 
7
  # ====== Telegram Bot Setup ======
8
 
9
- TELEGRAM_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
10
 
11
- # Simple Telegram command handler
12
  async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
13
  await update.message.reply_text("Hello! I'm your bot!")
14
 
15
- # Function to run the Telegram bot in a separate thread
16
- def run_telegram_bot():
17
- # Create and set a new event loop for this thread
18
- loop = asyncio.new_event_loop()
19
- asyncio.set_event_loop(loop)
20
-
21
- # Build and configure the Telegram bot application
22
- application = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
23
- application.add_handler(CommandHandler("start", start))
24
-
25
- # Run the bot polling loop
26
- application.run_polling()
27
-
28
  # ====== Gradio App Setup ======
29
 
 
30
  def greet(name):
31
  return f"Hello, {name}!"
32
 
 
33
  demo = gr.Interface(fn=greet, inputs="text", outputs="text")
34
 
 
 
 
 
35
  # ====== Start Both Gradio & Telegram Bot ======
36
 
37
  if __name__ == "__main__":
38
- # Start the Telegram bot in a separate thread
39
- telegram_thread = threading.Thread(target=run_telegram_bot)
40
- telegram_thread.start()
41
-
42
- # Start Gradio app (non-blocking, otherwise it would block the bot)
43
- demo.launch(share=True)
 
 
 
6
 
7
  # ====== Telegram Bot Setup ======
8
 
9
+ TELEGRAM_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN" # Replace this with your real bot token
10
 
11
+ # Define a simple Telegram command handler
12
  async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
13
  await update.message.reply_text("Hello! I'm your bot!")
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # ====== Gradio App Setup ======
16
 
17
+ # Define a simple Gradio function
18
  def greet(name):
19
  return f"Hello, {name}!"
20
 
21
+ # Create the Gradio interface
22
  demo = gr.Interface(fn=greet, inputs="text", outputs="text")
23
 
24
+ # Function to run Gradio in a separate thread
25
+ def run_gradio():
26
+ demo.launch(share=True) # 'share=True' creates a public link
27
+
28
  # ====== Start Both Gradio & Telegram Bot ======
29
 
30
  if __name__ == "__main__":
31
+ # Start Gradio in a separate thread
32
+ gradio_thread = threading.Thread(target=run_gradio)
33
+ gradio_thread.start()
34
+
35
+ # Start the Telegram bot in the main thread (avoids signal issues)
36
+ application = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
37
+ application.add_handler(CommandHandler("start", start))
38
+ application.run_polling()