omar1232 commited on
Commit
1cb5b8a
·
verified ·
1 Parent(s): b30b59c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -2
app.py CHANGED
@@ -4,6 +4,7 @@ from pydub import AudioSegment
4
  import tempfile
5
  from langdetect import detect
6
  import os
 
7
  from telegram import Update
8
  from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
9
 
@@ -166,12 +167,16 @@ with gr.Blocks() as demo:
166
  outputs=[audio_file, audio_record]
167
  )
168
 
169
- # Start the Telegram bot in a separate thread
170
  def run_telegram_bot():
171
  if not TELEGRAM_BOT_TOKEN:
172
  print("Telegram bot token not found. Please set TELEGRAM_BOT_TOKEN in the Space secrets.")
173
  return
174
 
 
 
 
 
175
  application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
176
 
177
  # Add handlers
@@ -180,7 +185,10 @@ def run_telegram_bot():
180
 
181
  # Start the bot
182
  print("Starting Telegram bot...")
183
- application.run_polling(allowed_updates=Update.ALL_TYPES)
 
 
 
184
 
185
  # Launch Gradio app and Telegram bot
186
  if __name__ == "__main__":
 
4
  import tempfile
5
  from langdetect import detect
6
  import os
7
+ import asyncio
8
  from telegram import Update
9
  from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
10
 
 
167
  outputs=[audio_file, audio_record]
168
  )
169
 
170
+ # Start the Telegram bot in a separate thread with its own event loop
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
+ # Create a new event loop for this thread
177
+ loop = asyncio.new_event_loop()
178
+ asyncio.set_event_loop(loop)
179
+
180
  application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
181
 
182
  # Add handlers
 
185
 
186
  # Start the bot
187
  print("Starting Telegram bot...")
188
+ try:
189
+ loop.run_until_complete(application.run_polling(allowed_updates=Update.ALL_TYPES))
190
+ finally:
191
+ loop.close()
192
 
193
  # Launch Gradio app and Telegram bot
194
  if __name__ == "__main__":