Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import requests
|
|
| 4 |
import uuid
|
| 5 |
import os
|
| 6 |
import soundfile as sf
|
|
|
|
| 7 |
from f5_tts.api import F5TTS
|
| 8 |
|
| 9 |
# ==========================================
|
|
@@ -11,15 +12,15 @@ from f5_tts.api import F5TTS
|
|
| 11 |
# ==========================================
|
| 12 |
GROQ_API_KEY = "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"
|
| 13 |
|
| 14 |
-
print("
|
| 15 |
whisper_model = whisper.load_model("base")
|
| 16 |
|
| 17 |
-
print("
|
| 18 |
f5tts = F5TTS()
|
| 19 |
print("Voice Simulation Matrix Active!")
|
| 20 |
|
| 21 |
# ==========================================
|
| 22 |
-
# 2. Dynamic Text Response Generation
|
| 23 |
# ==========================================
|
| 24 |
def get_ai_answer(question):
|
| 25 |
try:
|
|
@@ -37,17 +38,16 @@ def get_ai_answer(question):
|
|
| 37 |
timeout=10
|
| 38 |
)
|
| 39 |
data = response.json()
|
| 40 |
-
# CRITICAL FIX: Explicitly target array index [0] to parse live text properly
|
| 41 |
if "choices" in data and len(data["choices"]) > 0:
|
| 42 |
-
return data["choices"][
|
| 43 |
except Exception as e:
|
| 44 |
print(f"Groq API connection error: {e}")
|
| 45 |
|
| 46 |
-
#
|
| 47 |
return "Salesforce is a cloud-based customer relationship management platform that helps businesses manage sales, marketing, and customer service tracking in one place."
|
| 48 |
|
| 49 |
# ==========================================
|
| 50 |
-
# 3. Dynamic Voice Synthesis
|
| 51 |
# ==========================================
|
| 52 |
def clone_and_speak(text, dynamic_ref_audio):
|
| 53 |
try:
|
|
@@ -75,44 +75,55 @@ def clone_and_speak(text, dynamic_ref_audio):
|
|
| 75 |
extracted_ref_text = "Hello"
|
| 76 |
|
| 77 |
unique_filename = f"voice_clone_{uuid.uuid4().hex[:8]}.wav"
|
| 78 |
-
print("π Processing voice clone generation...")
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
if os.path.exists(clean_ref_path) and clean_ref_path != dynamic_ref_audio:
|
| 117 |
os.remove(clean_ref_path)
|
| 118 |
|
|
@@ -120,6 +131,7 @@ def clone_and_speak(text, dynamic_ref_audio):
|
|
| 120 |
print(f"π Generated track successfully saved to: {unique_filename}")
|
| 121 |
return unique_filename
|
| 122 |
|
|
|
|
| 123 |
return None
|
| 124 |
|
| 125 |
except Exception as e:
|
|
@@ -147,15 +159,15 @@ def chatbot_engine(text_input, audio_input, target_voice_profile):
|
|
| 147 |
return generated_audio_track
|
| 148 |
|
| 149 |
# ==========================================
|
| 150 |
-
# 5. Gradio
|
| 151 |
# ==========================================
|
| 152 |
with gr.Blocks(title="Dynamic Voice Simulation Studio") as interface:
|
| 153 |
-
gr.Markdown("# π
|
| 154 |
gr.Markdown("Drop any source audio file to clone voice attributes and play back the generated simulated response.")
|
| 155 |
|
| 156 |
with gr.Row():
|
| 157 |
with gr.Column(scale=1):
|
| 158 |
-
gr.Markdown("### π 1. Reference Audio Profile")
|
| 159 |
voice_profile_uploader = gr.Audio(
|
| 160 |
label="Target Voice to Clone",
|
| 161 |
type="filepath"
|
|
|
|
| 4 |
import uuid
|
| 5 |
import os
|
| 6 |
import soundfile as sf
|
| 7 |
+
import inspect
|
| 8 |
from f5_tts.api import F5TTS
|
| 9 |
|
| 10 |
# ==========================================
|
|
|
|
| 12 |
# ==========================================
|
| 13 |
GROQ_API_KEY = "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"
|
| 14 |
|
| 15 |
+
print("Initializing Whisper Speech Recognition...")
|
| 16 |
whisper_model = whisper.load_model("base")
|
| 17 |
|
| 18 |
+
print("Initializing F5-TTS Simulation Engine...")
|
| 19 |
f5tts = F5TTS()
|
| 20 |
print("Voice Simulation Matrix Active!")
|
| 21 |
|
| 22 |
# ==========================================
|
| 23 |
+
# 2. Pure Dynamic Text Response Generation
|
| 24 |
# ==========================================
|
| 25 |
def get_ai_answer(question):
|
| 26 |
try:
|
|
|
|
| 38 |
timeout=10
|
| 39 |
)
|
| 40 |
data = response.json()
|
|
|
|
| 41 |
if "choices" in data and len(data["choices"]) > 0:
|
| 42 |
+
return data["choices"]["message"]["content"].strip()
|
| 43 |
except Exception as e:
|
| 44 |
print(f"Groq API connection error: {e}")
|
| 45 |
|
| 46 |
+
# Static fallback message to protect pipeline execution flow
|
| 47 |
return "Salesforce is a cloud-based customer relationship management platform that helps businesses manage sales, marketing, and customer service tracking in one place."
|
| 48 |
|
| 49 |
# ==========================================
|
| 50 |
+
# 3. Dynamic Voice Synthesis (Signature Inspection)
|
| 51 |
# ==========================================
|
| 52 |
def clone_and_speak(text, dynamic_ref_audio):
|
| 53 |
try:
|
|
|
|
| 75 |
extracted_ref_text = "Hello"
|
| 76 |
|
| 77 |
unique_filename = f"voice_clone_{uuid.uuid4().hex[:8]}.wav"
|
|
|
|
| 78 |
|
| 79 |
+
# --- AUTOMATED SIGNATURE INSPECTION MATRIX ---
|
| 80 |
+
# Look inside the installed F5-TTS function parameters to build a safe argument map
|
| 81 |
+
sig = inspect.signature(f5tts.infer)
|
| 82 |
+
supported_args = sig.parameters.keys()
|
| 83 |
+
print(f"π Detected package argument parameters: {list(supported_args)}")
|
| 84 |
+
|
| 85 |
+
kwargs = {}
|
| 86 |
+
|
| 87 |
+
# Dynamically map reference file parameter keys
|
| 88 |
+
if "ref_audio" in supported_args:
|
| 89 |
+
kwargs["ref_audio"] = clean_ref_path
|
| 90 |
+
elif "ref_file" in supported_args:
|
| 91 |
+
kwargs["ref_file"] = clean_ref_path
|
| 92 |
+
|
| 93 |
+
# Dynamically map tracking inputs
|
| 94 |
+
if "ref_text" in supported_args:
|
| 95 |
+
kwargs["ref_text"] = extracted_ref_text
|
| 96 |
+
if "gen_text" in supported_args:
|
| 97 |
+
kwargs["gen_text"] = text
|
| 98 |
+
if "file_wave" in supported_args:
|
| 99 |
+
kwargs["file_wave"] = unique_filename
|
| 100 |
+
|
| 101 |
+
# Safe addition for secondary speed/step controllers if supported by your version
|
| 102 |
+
if "nfe_step" in supported_args:
|
| 103 |
+
kwargs["nfe_step"] = 16
|
| 104 |
+
if "speed" in supported_args:
|
| 105 |
+
kwargs["speed"] = 1.0
|
| 106 |
+
|
| 107 |
+
print(f"π Processing execution loop with arguments: {list(kwargs.keys())}")
|
| 108 |
+
outputs = f5tts.infer(**kwargs)
|
| 109 |
+
|
| 110 |
+
# Handle file saving fallback if package version didn't write to file_wave automatically
|
| 111 |
+
if not os.path.exists(unique_filename) or os.path.getsize(unique_filename) == 0:
|
| 112 |
+
if outputs is not None:
|
| 113 |
+
if isinstance(outputs, tuple):
|
| 114 |
+
if len(outputs) == 3:
|
| 115 |
+
wav_data, sr, _ = outputs
|
| 116 |
+
else:
|
| 117 |
+
wav_data, sr = outputs
|
| 118 |
+
else:
|
| 119 |
+
wav_data, sr = outputs, 24000
|
| 120 |
+
|
| 121 |
+
if hasattr(wav_data, "cpu"):
|
| 122 |
+
wav_data = wav_data.cpu().numpy()
|
| 123 |
+
|
| 124 |
+
sf.write(unique_filename, wav_data, sr, format='WAV', subtype='PCM_16')
|
| 125 |
+
|
| 126 |
+
# Clean temporary tracking assets
|
| 127 |
if os.path.exists(clean_ref_path) and clean_ref_path != dynamic_ref_audio:
|
| 128 |
os.remove(clean_ref_path)
|
| 129 |
|
|
|
|
| 131 |
print(f"π Generated track successfully saved to: {unique_filename}")
|
| 132 |
return unique_filename
|
| 133 |
|
| 134 |
+
print("β System warning: Output file generation sizing error.")
|
| 135 |
return None
|
| 136 |
|
| 137 |
except Exception as e:
|
|
|
|
| 159 |
return generated_audio_track
|
| 160 |
|
| 161 |
# ==========================================
|
| 162 |
+
# 5. Gradio Audio-Only UI Canvas (Single Page)
|
| 163 |
# ==========================================
|
| 164 |
with gr.Blocks(title="Dynamic Voice Simulation Studio") as interface:
|
| 165 |
+
gr.Markdown("# ποΈ Single-Page Voice Cloning Studio")
|
| 166 |
gr.Markdown("Drop any source audio file to clone voice attributes and play back the generated simulated response.")
|
| 167 |
|
| 168 |
with gr.Row():
|
| 169 |
with gr.Column(scale=1):
|
| 170 |
+
gr.Markdown("### ποΈ 1. Reference Audio Profile")
|
| 171 |
voice_profile_uploader = gr.Audio(
|
| 172 |
label="Target Voice to Clone",
|
| 173 |
type="filepath"
|