Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -167,31 +167,36 @@ with gr.Blocks() as demo:
|
|
| 167 |
outputs=[audio_file, audio_record]
|
| 168 |
)
|
| 169 |
|
| 170 |
-
#
|
|
|
|
|
|
|
| 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
|
| 183 |
print("Starting Telegram bot...")
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
demo.launch()
|
| 189 |
|
| 190 |
-
# Launch Gradio app
|
| 191 |
if __name__ == "__main__":
|
| 192 |
import threading
|
| 193 |
-
# Start
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
# Run
|
| 197 |
-
|
|
|
|
| 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
|