Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,6 +19,7 @@ from twilio.rest import Client
|
|
| 19 |
import logging
|
| 20 |
import whisper
|
| 21 |
import speech_recognition as sr
|
|
|
|
| 22 |
#model = whisper.load_model("base")
|
| 23 |
|
| 24 |
|
|
@@ -86,12 +87,31 @@ def get_questions(prompt, input_text, num_questions=3, max_retries=10):
|
|
| 86 |
|
| 87 |
return new_questions
|
| 88 |
|
| 89 |
-
async def generate_question_audio(question, voice="en-
|
| 90 |
clean_question = re.sub(r'[^A-Za-z0-9.,?! ]+', '', question)
|
| 91 |
tts = edge_tts.Communicate(text=clean_question, voice=voice)
|
| 92 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
########################################///////////////////////////////////////////////////#########################################
|
| 97 |
|
|
|
|
| 19 |
import logging
|
| 20 |
import whisper
|
| 21 |
import speech_recognition as sr
|
| 22 |
+
from gtts import gTTS
|
| 23 |
#model = whisper.load_model("base")
|
| 24 |
|
| 25 |
|
|
|
|
| 87 |
|
| 88 |
return new_questions
|
| 89 |
|
| 90 |
+
async def generate_question_audio(question, voice="en-US-AriaNeural"):
|
| 91 |
clean_question = re.sub(r'[^A-Za-z0-9.,?! ]+', '', question)
|
| 92 |
tts = edge_tts.Communicate(text=clean_question, voice=voice)
|
| 93 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 94 |
+
try:
|
| 95 |
+
# 🔹 Try Edge-TTS first
|
| 96 |
+
tts = edge_tts.Communicate(text=clean_question, voice=voice)
|
| 97 |
+
await tts.save(tmp_file.name)
|
| 98 |
+
print("✅ Edge-TTS audio generated successfully.")
|
| 99 |
+
return tmp_file.name
|
| 100 |
+
|
| 101 |
+
except Exception as e:
|
| 102 |
+
print(f"⚠️ Edge-TTS failed: {e}")
|
| 103 |
+
print("⏪ Falling back to Google TTS...")
|
| 104 |
+
|
| 105 |
+
try:
|
| 106 |
+
# 🔹 Fallback: Google TTS (works inside Hugging Face)
|
| 107 |
+
tts = gTTS(text=clean_question, lang="en")
|
| 108 |
+
tts.save(tmp_file.name)
|
| 109 |
+
print("✅ gTTS fallback audio generated successfully.")
|
| 110 |
+
return tmp_file.name
|
| 111 |
+
|
| 112 |
+
except Exception as e2:
|
| 113 |
+
print(f"❌ gTTS also failed: {e2}")
|
| 114 |
+
return None
|
| 115 |
|
| 116 |
########################################///////////////////////////////////////////////////#########################################
|
| 117 |
|