Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,11 @@ import logging
|
|
| 3 |
from telegram import Update
|
| 4 |
from telegram.ext import Application, MessageHandler, filters
|
| 5 |
from transformers import pipeline, AutoTokenizer, VitsModel
|
| 6 |
-
import torchaudio
|
| 7 |
import librosa
|
| 8 |
-
import soundfile as sf
|
| 9 |
from pydub import AudioSegment
|
| 10 |
import numpy as np
|
|
|
|
| 11 |
|
| 12 |
# تهيئة النظام
|
| 13 |
logging.basicConfig(
|
|
@@ -42,7 +42,7 @@ def enhance_audio(input_path, output_path):
|
|
| 42 |
async def speech_to_text(audio_path):
|
| 43 |
try:
|
| 44 |
audio, sr = librosa.load(audio_path, sr=16000)
|
| 45 |
-
sf.write("temp.wav", audio, sr)
|
| 46 |
result = asr_pipeline("temp.wav")
|
| 47 |
return result["text"]
|
| 48 |
except Exception as e:
|
|
@@ -72,7 +72,10 @@ async def text_to_speech(text):
|
|
| 72 |
with torch.no_grad():
|
| 73 |
output = tts_model(**inputs)
|
| 74 |
waveform = output.waveform[0].numpy()
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
logger.error(f"فشل تحويل النص إلى صوت: {str(e)}")
|
| 78 |
|
|
|
|
| 3 |
from telegram import Update
|
| 4 |
from telegram.ext import Application, MessageHandler, filters
|
| 5 |
from transformers import pipeline, AutoTokenizer, VitsModel
|
|
|
|
| 6 |
import librosa
|
| 7 |
+
import soundfile as sf # <-- المكتبة البديلة
|
| 8 |
from pydub import AudioSegment
|
| 9 |
import numpy as np
|
| 10 |
+
import torch
|
| 11 |
|
| 12 |
# تهيئة النظام
|
| 13 |
logging.basicConfig(
|
|
|
|
| 42 |
async def speech_to_text(audio_path):
|
| 43 |
try:
|
| 44 |
audio, sr = librosa.load(audio_path, sr=16000)
|
| 45 |
+
sf.write("temp.wav", audio, sr) # <-- استبدال torchaudio بـ soundfile
|
| 46 |
result = asr_pipeline("temp.wav")
|
| 47 |
return result["text"]
|
| 48 |
except Exception as e:
|
|
|
|
| 72 |
with torch.no_grad():
|
| 73 |
output = tts_model(**inputs)
|
| 74 |
waveform = output.waveform[0].numpy()
|
| 75 |
+
|
| 76 |
+
# ===== التغيير الرئيسي هنا =====
|
| 77 |
+
sf.write("bot_response.wav", waveform, tts_model.config.sampling_rate) # <-- استبدال torchaudio
|
| 78 |
+
|
| 79 |
except Exception as e:
|
| 80 |
logger.error(f"فشل تحويل النص إلى صوت: {str(e)}")
|
| 81 |
|