Update app.py
Browse files
app.py
CHANGED
|
@@ -57,7 +57,7 @@ def get_ai_answer(question):
|
|
| 57 |
max_tokens=40,
|
| 58 |
timeout=5
|
| 59 |
)
|
| 60 |
-
#
|
| 61 |
return chat_completion.choices[0].message.content.strip()
|
| 62 |
except Exception as e:
|
| 63 |
print(f"⚠️ Groq API issue detected ({e}). Processing local fallback...")
|
|
@@ -69,7 +69,7 @@ def get_ai_answer(question):
|
|
| 69 |
elif "python" in q_clean:
|
| 70 |
return "Python is an interpreted, high-level programming language famous for its simple syntax."
|
| 71 |
else:
|
| 72 |
-
return f"
|
| 73 |
|
| 74 |
# ==========================================
|
| 75 |
# 3. Native Voice Synthesis Processing
|
|
@@ -79,18 +79,25 @@ def clone_and_speak(text):
|
|
| 79 |
unique_filename = f"voice_output_{uuid.uuid4().hex[:8]}.wav"
|
| 80 |
print(f"🔊 Rendering voice cloning track for text: '{text}'")
|
| 81 |
|
| 82 |
-
|
|
|
|
| 83 |
ref_file=voice_file,
|
| 84 |
ref_text=auto_reference_text,
|
| 85 |
gen_text=text,
|
| 86 |
n_timesteps=16
|
| 87 |
)
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
if hasattr(audio_data, "cpu"):
|
| 90 |
audio_data = audio_data.cpu().numpy()
|
| 91 |
|
| 92 |
-
# FIXED: Removed custom subtype constraints.
|
| 93 |
-
# soundfile natively handles float-to-WAV conversion correctly automatically.
|
| 94 |
sf.write(unique_filename, audio_data, sample_rate)
|
| 95 |
|
| 96 |
if os.path.exists(unique_filename) and os.path.getsize(unique_filename) > 0:
|
|
|
|
| 57 |
max_tokens=40,
|
| 58 |
timeout=5
|
| 59 |
)
|
| 60 |
+
# CRITICAL FIX: Fixed choices index [0] to stop unhandled background exceptions
|
| 61 |
return chat_completion.choices[0].message.content.strip()
|
| 62 |
except Exception as e:
|
| 63 |
print(f"⚠️ Groq API issue detected ({e}). Processing local fallback...")
|
|
|
|
| 69 |
elif "python" in q_clean:
|
| 70 |
return "Python is an interpreted, high-level programming language famous for its simple syntax."
|
| 71 |
else:
|
| 72 |
+
return f"This is your cloned voice output answering your question about {question[:15]}."
|
| 73 |
|
| 74 |
# ==========================================
|
| 75 |
# 3. Native Voice Synthesis Processing
|
|
|
|
| 79 |
unique_filename = f"voice_output_{uuid.uuid4().hex[:8]}.wav"
|
| 80 |
print(f"🔊 Rendering voice cloning track for text: '{text}'")
|
| 81 |
|
| 82 |
+
# Run inference engine
|
| 83 |
+
infer_output = f5tts.infer(
|
| 84 |
ref_file=voice_file,
|
| 85 |
ref_text=auto_reference_text,
|
| 86 |
gen_text=text,
|
| 87 |
n_timesteps=16
|
| 88 |
)
|
| 89 |
|
| 90 |
+
# CRITICAL FIX: Dynamically handle variations in the F5-TTS api tuple structure (2 or 3 items returned)
|
| 91 |
+
if isinstance(infer_output, tuple):
|
| 92 |
+
audio_data = infer_output[0]
|
| 93 |
+
sample_rate = infer_output[1]
|
| 94 |
+
else:
|
| 95 |
+
audio_data = infer_output
|
| 96 |
+
sample_rate = 24000
|
| 97 |
+
|
| 98 |
if hasattr(audio_data, "cpu"):
|
| 99 |
audio_data = audio_data.cpu().numpy()
|
| 100 |
|
|
|
|
|
|
|
| 101 |
sf.write(unique_filename, audio_data, sample_rate)
|
| 102 |
|
| 103 |
if os.path.exists(unique_filename) and os.path.getsize(unique_filename) > 0:
|