Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,16 @@
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
from telegram.ext import ApplicationBuilder, MessageHandler, filters
|
| 6 |
-
from transformers import pipeline, AutoTokenizer, VitsModel
|
| 7 |
-
from huggingface_hub import login
|
| 8 |
import torch
|
| 9 |
import librosa
|
| 10 |
import soundfile as sf
|
| 11 |
from pydub import AudioSegment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# ===== تهيئة التوكن =====
|
| 14 |
login(token=os.getenv("HF_TOKEN"))
|
|
@@ -93,7 +95,7 @@ async def text_to_speech(text: str) -> None:
|
|
| 93 |
except Exception as e:
|
| 94 |
logger.error(f"فشل تحويل النص إلى صوت: {str(e)}")
|
| 95 |
|
| 96 |
-
# ===== الدالة الرئيسية =====
|
| 97 |
async def process_voice(update: Update, context):
|
| 98 |
try:
|
| 99 |
voice_file = await update.message.voice.get_file()
|
|
@@ -112,16 +114,20 @@ async def process_voice(update: Update, context):
|
|
| 112 |
logger.error(f"خطأ غير متوقع: {str(e)}")
|
| 113 |
await update.message.reply_text("⚠️ عذرًا، حدث خطأ في المعالجة.")
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
| 117 |
application = ApplicationBuilder().token(os.getenv("TELEGRAM_TOKEN")).build()
|
| 118 |
application.add_handler(MessageHandler(filters.VOICE, process_voice))
|
| 119 |
|
| 120 |
-
# إيقاف التعامل مع الإشارات لتجنب الخطأ
|
| 121 |
application.run_polling(
|
| 122 |
close_loop=False,
|
| 123 |
stop_signals=[]
|
| 124 |
)
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
+
import threading
|
| 4 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 5 |
import torch
|
| 6 |
import librosa
|
| 7 |
import soundfile as sf
|
| 8 |
from pydub import AudioSegment
|
| 9 |
+
from telegram import Update
|
| 10 |
+
from telegram.ext import ApplicationBuilder, MessageHandler, filters
|
| 11 |
+
from transformers import pipeline, AutoTokenizer, VitsModel
|
| 12 |
+
from huggingface_hub import login
|
| 13 |
+
import asyncio
|
| 14 |
|
| 15 |
# ===== تهيئة التوكن =====
|
| 16 |
login(token=os.getenv("HF_TOKEN"))
|
|
|
|
| 95 |
except Exception as e:
|
| 96 |
logger.error(f"فشل تحويل النص إلى صوت: {str(e)}")
|
| 97 |
|
| 98 |
+
# ===== الدالة الرئيسية مع Threading =====
|
| 99 |
async def process_voice(update: Update, context):
|
| 100 |
try:
|
| 101 |
voice_file = await update.message.voice.get_file()
|
|
|
|
| 114 |
logger.error(f"خطأ غير متوقع: {str(e)}")
|
| 115 |
await update.message.reply_text("⚠️ عذرًا، حدث خطأ في المعالجة.")
|
| 116 |
|
| 117 |
+
def run_bot():
|
| 118 |
+
loop = asyncio.new_event_loop()
|
| 119 |
+
asyncio.set_event_loop(loop)
|
| 120 |
+
|
| 121 |
application = ApplicationBuilder().token(os.getenv("TELEGRAM_TOKEN")).build()
|
| 122 |
application.add_handler(MessageHandler(filters.VOICE, process_voice))
|
| 123 |
|
|
|
|
| 124 |
application.run_polling(
|
| 125 |
close_loop=False,
|
| 126 |
stop_signals=[]
|
| 127 |
)
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
+
# تشغيل البوت في خيط منفصل
|
| 131 |
+
bot_thread = threading.Thread(target=run_bot, daemon=True)
|
| 132 |
+
bot_thread.start()
|
| 133 |
+
bot_thread.join()
|