Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
|
|
|
| 3 |
from telegram import Update
|
| 4 |
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
| 5 |
from transformers import pipeline
|
|
|
|
| 6 |
|
| 7 |
-
# Telegram bot token (
|
| 8 |
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 9 |
|
| 10 |
-
# Load
|
| 11 |
generator = pipeline("text-generation", model="distilgpt2")
|
| 12 |
|
| 13 |
# Telegram bot handlers
|
|
@@ -16,7 +18,6 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 16 |
|
| 17 |
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 18 |
user_message = update.message.text
|
| 19 |
-
# Generate a response using the AI model
|
| 20 |
response = generator(user_message, max_length=50, num_return_sequences=1, truncation=True)[0]['generated_text']
|
| 21 |
await update.message.reply_text(response)
|
| 22 |
|
|
@@ -28,10 +29,10 @@ async def error_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 28 |
text="Bot conflict detected. Restarting..."
|
| 29 |
)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
async def
|
| 33 |
if not TELEGRAM_BOT_TOKEN:
|
| 34 |
-
print("Telegram bot token not found. Please set TELEGRAM_BOT_TOKEN
|
| 35 |
return
|
| 36 |
|
| 37 |
application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
|
|
@@ -41,11 +42,10 @@ async def main():
|
|
| 41 |
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
| 42 |
application.add_error_handler(error_handler)
|
| 43 |
|
| 44 |
-
# Clear
|
| 45 |
print("Clearing any existing webhook and updates...")
|
| 46 |
try:
|
| 47 |
await application.bot.delete_webhook(drop_pending_updates=True)
|
| 48 |
-
# Fetch and discard any pending updates to clear the queue
|
| 49 |
updates = await application.bot.get_updates(timeout=1)
|
| 50 |
if updates:
|
| 51 |
last_update_id = updates[-1].update_id
|
|
@@ -53,10 +53,26 @@ async def main():
|
|
| 53 |
except Exception as e:
|
| 54 |
print(f"Error clearing updates: {e}")
|
| 55 |
|
| 56 |
-
# Start
|
| 57 |
print("Starting Telegram bot...")
|
| 58 |
await application.run_polling(allowed_updates=Update.ALL_TYPES)
|
| 59 |
|
| 60 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
if __name__ == "__main__":
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
| 3 |
+
import threading
|
| 4 |
from telegram import Update
|
| 5 |
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
| 6 |
from transformers import pipeline
|
| 7 |
+
import gradio as gr
|
| 8 |
|
| 9 |
+
# Telegram bot token (from environment variable)
|
| 10 |
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 11 |
|
| 12 |
+
# Load AI model for text generation
|
| 13 |
generator = pipeline("text-generation", model="distilgpt2")
|
| 14 |
|
| 15 |
# Telegram bot handlers
|
|
|
|
| 18 |
|
| 19 |
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 20 |
user_message = update.message.text
|
|
|
|
| 21 |
response = generator(user_message, max_length=50, num_return_sequences=1, truncation=True)[0]['generated_text']
|
| 22 |
await update.message.reply_text(response)
|
| 23 |
|
|
|
|
| 29 |
text="Bot conflict detected. Restarting..."
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# Telegram bot logic
|
| 33 |
+
async def telegram_bot():
|
| 34 |
if not TELEGRAM_BOT_TOKEN:
|
| 35 |
+
print("Telegram bot token not found. Please set TELEGRAM_BOT_TOKEN.")
|
| 36 |
return
|
| 37 |
|
| 38 |
application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
|
|
|
|
| 42 |
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
| 43 |
application.add_error_handler(error_handler)
|
| 44 |
|
| 45 |
+
# Clear webhook and updates
|
| 46 |
print("Clearing any existing webhook and updates...")
|
| 47 |
try:
|
| 48 |
await application.bot.delete_webhook(drop_pending_updates=True)
|
|
|
|
| 49 |
updates = await application.bot.get_updates(timeout=1)
|
| 50 |
if updates:
|
| 51 |
last_update_id = updates[-1].update_id
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
print(f"Error clearing updates: {e}")
|
| 55 |
|
| 56 |
+
# Start bot polling
|
| 57 |
print("Starting Telegram bot...")
|
| 58 |
await application.run_polling(allowed_updates=Update.ALL_TYPES)
|
| 59 |
|
| 60 |
+
# Gradio logic
|
| 61 |
+
def gradio_app():
|
| 62 |
+
iface = gr.Interface(fn=lambda x: "Hello " + x, inputs="text", outputs="text")
|
| 63 |
+
iface.launch()
|
| 64 |
+
|
| 65 |
+
# Combine both
|
| 66 |
if __name__ == "__main__":
|
| 67 |
+
# Step 1: Start Gradio in a separate thread
|
| 68 |
+
gradio_thread = threading.Thread(target=gradio_app)
|
| 69 |
+
gradio_thread.start()
|
| 70 |
+
|
| 71 |
+
# Step 2: Start Telegram bot in the main thread, handling event loop properly
|
| 72 |
+
try:
|
| 73 |
+
loop = asyncio.get_running_loop()
|
| 74 |
+
except RuntimeError:
|
| 75 |
+
loop = asyncio.new_event_loop()
|
| 76 |
+
asyncio.set_event_loop(loop)
|
| 77 |
+
|
| 78 |
+
loop.run_until_complete(telegram_bot())
|