omar1232 commited on
Commit
fb96244
·
verified ·
1 Parent(s): b554eea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -167,31 +167,36 @@ with gr.Blocks() as demo:
167
  outputs=[audio_file, audio_record]
168
  )
169
 
170
- # Start the Telegram bot in the main thread
 
 
171
  def run_telegram_bot():
172
  if not TELEGRAM_BOT_TOKEN:
173
  print("Telegram bot token not found. Please set TELEGRAM_BOT_TOKEN in the Space secrets.")
174
  return
175
 
 
 
 
 
176
  application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
177
 
178
  # Add handlers
179
  application.add_handler(CommandHandler("start", start))
180
  application.add_handler(MessageHandler(filters.AUDIO, handle_audio))
181
 
182
- # Start the bot in the main thread
183
  print("Starting Telegram bot...")
184
- asyncio.run(application.run_polling(allowed_updates=Update.ALL_TYPES))
185
-
186
- # Launch Gradio app in a separate thread
187
- def run_gradio():
188
- demo.launch()
189
 
190
- # Launch Gradio app and Telegram bot
191
  if __name__ == "__main__":
192
  import threading
193
- # Start Gradio in a separate thread
194
- gradio_thread = threading.Thread(target=run_gradio)
195
- gradio_thread.start()
196
- # Run Telegram bot in the main thread
197
- run_telegram_bot()
 
167
  outputs=[audio_file, audio_record]
168
  )
169
 
170
+ # [Previous code remains unchanged until the launch logic]
171
+
172
+ # Start the Telegram bot in a separate thread with its own event loop
173
  def run_telegram_bot():
174
  if not TELEGRAM_BOT_TOKEN:
175
  print("Telegram bot token not found. Please set TELEGRAM_BOT_TOKEN in the Space secrets.")
176
  return
177
 
178
+ # Create a new event loop for this thread
179
+ loop = asyncio.new_event_loop()
180
+ asyncio.set_event_loop(loop)
181
+
182
  application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
183
 
184
  # Add handlers
185
  application.add_handler(CommandHandler("start", start))
186
  application.add_handler(MessageHandler(filters.AUDIO, handle_audio))
187
 
188
+ # Start the bot
189
  print("Starting Telegram bot...")
190
+ try:
191
+ loop.run_until_complete(application.run_polling(allowed_updates=Update.ALL_TYPES))
192
+ finally:
193
+ loop.close()
 
194
 
195
+ # Launch Gradio app in the main thread
196
  if __name__ == "__main__":
197
  import threading
198
+ # Start the Telegram bot in a separate thread
199
+ bot_thread = threading.Thread(target=run_telegram_bot)
200
+ bot_thread.start()
201
+ # Run Gradio app in the main thread
202
+ demo.launch(ssr_mode=False) # SSR is already disabled from the previous update