Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -167,36 +167,31 @@ with gr.Blocks() as demo:
|
|
| 167 |
outputs=[audio_file, audio_record]
|
| 168 |
)
|
| 169 |
|
| 170 |
-
#
|
| 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 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
| 194 |
|
| 195 |
-
# Launch
|
| 196 |
if __name__ == "__main__":
|
| 197 |
import threading
|
| 198 |
-
# Start
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
# Run
|
| 202 |
-
|
|
|
|
| 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 background thread
|
| 187 |
+
def run_gradio():
|
| 188 |
+
demo.launch(ssr_mode=False)
|
| 189 |
|
| 190 |
+
# Launch Telegram bot in the main thread, Gradio in a background thread
|
| 191 |
if __name__ == "__main__":
|
| 192 |
import threading
|
| 193 |
+
# Start Gradio in a background 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()
|