Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -23,14 +23,12 @@ logger = logging.getLogger(__name__)
|
|
23 |
|
24 |
# ===== تحميل النماذج =====
|
25 |
try:
|
26 |
-
# 1. نموذج التعرف على الصوت (ASR)
|
27 |
asr_pipeline = pipeline(
|
28 |
"automatic-speech-recognition",
|
29 |
model="jonatasgrosman/wav2vec2-large-xlsr-53-arabic",
|
30 |
token=os.getenv("HF_TOKEN")
|
31 |
)
|
32 |
|
33 |
-
# 2. نموذج توليف الصوت (TTS)
|
34 |
tts_tokenizer = AutoTokenizer.from_pretrained(
|
35 |
"facebook/mms-tts-ara",
|
36 |
token=os.getenv("HF_TOKEN")
|
@@ -46,7 +44,6 @@ except Exception as e:
|
|
46 |
|
47 |
# ===== دوال معالجة الصوت =====
|
48 |
def enhance_audio(input_path: str, output_path: str) -> bool:
|
49 |
-
"""تحسين جودة الملف الصوتي"""
|
50 |
try:
|
51 |
audio = AudioSegment.from_wav(input_path)
|
52 |
audio = audio.low_pass_filter(3000)
|
@@ -60,7 +57,6 @@ def enhance_audio(input_path: str, output_path: str) -> bool:
|
|
60 |
return False
|
61 |
|
62 |
async def speech_to_text(audio_path: str) -> str:
|
63 |
-
"""تحويل الصوت إلى نص"""
|
64 |
try:
|
65 |
audio, sr = librosa.load(audio_path, sr=16000)
|
66 |
sf.write("temp.wav", audio, sr)
|
@@ -71,7 +67,6 @@ async def speech_to_text(audio_path: str) -> str:
|
|
71 |
return ""
|
72 |
|
73 |
async def generate_response(text: str) -> str:
|
74 |
-
"""توليد رد الذكاء الاصطناعي"""
|
75 |
try:
|
76 |
chatbot = pipeline(
|
77 |
"text-generation",
|
@@ -90,7 +85,6 @@ async def generate_response(text: str) -> str:
|
|
90 |
return "حدث خطأ في توليد الرد."
|
91 |
|
92 |
async def text_to_speech(text: str) -> None:
|
93 |
-
"""تحويل النص إلى صوت"""
|
94 |
try:
|
95 |
inputs = tts_tokenizer(text, return_tensors="pt")
|
96 |
with torch.no_grad():
|
@@ -103,16 +97,13 @@ async def text_to_speech(text: str) -> None:
|
|
103 |
# ===== الدالة الرئيسية =====
|
104 |
async def process_voice(update: Update, context):
|
105 |
try:
|
106 |
-
# تحميل الصوت
|
107 |
voice_file = await update.message.voice.get_file()
|
108 |
await voice_file.download_to_drive("user_voice.ogg")
|
109 |
|
110 |
-
# معالجة الصوت
|
111 |
user_text = await speech_to_text("user_voice.ogg")
|
112 |
bot_response = await generate_response(user_text)
|
113 |
await text_to_speech(bot_response)
|
114 |
|
115 |
-
# إرسال الرد
|
116 |
if enhance_audio("bot_response.wav", "bot_response_enhanced.wav"):
|
117 |
await update.message.reply_voice("bot_response_enhanced.wav")
|
118 |
else:
|
@@ -122,11 +113,11 @@ async def process_voice(update: Update, context):
|
|
122 |
logger.error(f"خطأ غير متوقع: {str(e)}")
|
123 |
await update.message.reply_text("⚠️ عذرًا، حدث خطأ في المعالجة.")
|
124 |
|
125 |
-
# ===== التشغيل
|
126 |
-
|
127 |
application = ApplicationBuilder().token(os.getenv("TELEGRAM_TOKEN")).build()
|
128 |
application.add_handler(MessageHandler(filters.VOICE, process_voice))
|
129 |
-
|
130 |
|
131 |
if __name__ == "__main__":
|
132 |
-
|
|
|
23 |
|
24 |
# ===== تحميل النماذج =====
|
25 |
try:
|
|
|
26 |
asr_pipeline = pipeline(
|
27 |
"automatic-speech-recognition",
|
28 |
model="jonatasgrosman/wav2vec2-large-xlsr-53-arabic",
|
29 |
token=os.getenv("HF_TOKEN")
|
30 |
)
|
31 |
|
|
|
32 |
tts_tokenizer = AutoTokenizer.from_pretrained(
|
33 |
"facebook/mms-tts-ara",
|
34 |
token=os.getenv("HF_TOKEN")
|
|
|
44 |
|
45 |
# ===== دوال معالجة الصوت =====
|
46 |
def enhance_audio(input_path: str, output_path: str) -> bool:
|
|
|
47 |
try:
|
48 |
audio = AudioSegment.from_wav(input_path)
|
49 |
audio = audio.low_pass_filter(3000)
|
|
|
57 |
return False
|
58 |
|
59 |
async def speech_to_text(audio_path: str) -> str:
|
|
|
60 |
try:
|
61 |
audio, sr = librosa.load(audio_path, sr=16000)
|
62 |
sf.write("temp.wav", audio, sr)
|
|
|
67 |
return ""
|
68 |
|
69 |
async def generate_response(text: str) -> str:
|
|
|
70 |
try:
|
71 |
chatbot = pipeline(
|
72 |
"text-generation",
|
|
|
85 |
return "حدث خطأ في توليد الرد."
|
86 |
|
87 |
async def text_to_speech(text: str) -> None:
|
|
|
88 |
try:
|
89 |
inputs = tts_tokenizer(text, return_tensors="pt")
|
90 |
with torch.no_grad():
|
|
|
97 |
# ===== الدالة الرئيسية =====
|
98 |
async def process_voice(update: Update, context):
|
99 |
try:
|
|
|
100 |
voice_file = await update.message.voice.get_file()
|
101 |
await voice_file.download_to_drive("user_voice.ogg")
|
102 |
|
|
|
103 |
user_text = await speech_to_text("user_voice.ogg")
|
104 |
bot_response = await generate_response(user_text)
|
105 |
await text_to_speech(bot_response)
|
106 |
|
|
|
107 |
if enhance_audio("bot_response.wav", "bot_response_enhanced.wav"):
|
108 |
await update.message.reply_voice("bot_response_enhanced.wav")
|
109 |
else:
|
|
|
113 |
logger.error(f"خطأ غير متوقع: {str(e)}")
|
114 |
await update.message.reply_text("⚠️ عذرًا، حدث خطأ في المعالجة.")
|
115 |
|
116 |
+
# ===== التشغيل المعدل مع حل مشكلة الخيوط =====
|
117 |
+
def run_bot():
|
118 |
application = ApplicationBuilder().token(os.getenv("TELEGRAM_TOKEN")).build()
|
119 |
application.add_handler(MessageHandler(filters.VOICE, process_voice))
|
120 |
+
application.run_polling()
|
121 |
|
122 |
if __name__ == "__main__":
|
123 |
+
run_bot()
|