Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import requests
|
|
| 4 |
import uuid
|
| 5 |
import os
|
| 6 |
import soundfile as sf
|
| 7 |
-
import numpy as np
|
| 8 |
from f5_tts.api import F5TTS
|
| 9 |
|
| 10 |
# ==========================================
|
|
@@ -20,7 +19,7 @@ f5tts = F5TTS()
|
|
| 20 |
print("Voice Simulation Matrix Active!")
|
| 21 |
|
| 22 |
# ==========================================
|
| 23 |
-
# 2.
|
| 24 |
# ==========================================
|
| 25 |
def get_ai_answer(question):
|
| 26 |
try:
|
|
@@ -38,13 +37,17 @@ def get_ai_answer(question):
|
|
| 38 |
timeout=10
|
| 39 |
)
|
| 40 |
data = response.json()
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
print(f"Groq API connection error: {e}")
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# ==========================================
|
| 47 |
-
# 3.
|
| 48 |
# ==========================================
|
| 49 |
def clone_and_speak(text, dynamic_ref_audio):
|
| 50 |
try:
|
|
@@ -52,17 +55,16 @@ def clone_and_speak(text, dynamic_ref_audio):
|
|
| 52 |
print("β Reference enrollment track missing.")
|
| 53 |
return None
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
# Ensures any uploaded file format is safely downmixed to Mono for F5-TTS
|
| 57 |
clean_ref_path = f"processed_ref_{uuid.uuid4().hex[:4]}.wav"
|
| 58 |
try:
|
| 59 |
data, samplerate = sf.read(dynamic_ref_audio)
|
| 60 |
if len(data.shape) > 1:
|
| 61 |
-
data = data[:, 0]
|
| 62 |
sf.write(clean_ref_path, data, samplerate, format='WAV', subtype='PCM_16')
|
| 63 |
except Exception as audio_err:
|
| 64 |
print(f"Audio normalization warning: {audio_err}")
|
| 65 |
-
clean_ref_path = dynamic_ref_audio
|
| 66 |
|
| 67 |
# Automatically extract text transcript from reference clip
|
| 68 |
print(f"π Transcribing profile properties: {clean_ref_path}")
|
|
@@ -72,48 +74,48 @@ def clone_and_speak(text, dynamic_ref_audio):
|
|
| 72 |
if not extracted_ref_text:
|
| 73 |
extracted_ref_text = "Hello"
|
| 74 |
|
| 75 |
-
# Unique filename generation to bypass browser cache freezes
|
| 76 |
unique_filename = f"voice_clone_{uuid.uuid4().hex[:8]}.wav"
|
| 77 |
-
|
| 78 |
print("π Processing voice clone generation...")
|
|
|
|
|
|
|
| 79 |
try:
|
| 80 |
-
#
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
ref_file=clean_ref_path,
|
| 84 |
ref_text=extracted_ref_text,
|
| 85 |
gen_text=text,
|
| 86 |
-
|
| 87 |
-
nfe_step=9,
|
| 88 |
speed=1.0
|
| 89 |
)
|
| 90 |
except TypeError:
|
| 91 |
-
#
|
| 92 |
outputs = f5tts.infer(
|
| 93 |
ref_file=clean_ref_path,
|
| 94 |
ref_text=extracted_ref_text,
|
| 95 |
gen_text=text,
|
| 96 |
-
nfe_step=
|
| 97 |
speed=1.0
|
| 98 |
)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
else:
|
| 105 |
-
wav_data, sr = outputs
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
-
|
|
|
|
| 111 |
|
| 112 |
-
# Clean
|
| 113 |
if os.path.exists(clean_ref_path) and clean_ref_path != dynamic_ref_audio:
|
| 114 |
os.remove(clean_ref_path)
|
| 115 |
|
| 116 |
-
# Double check data properties on disk space
|
| 117 |
if os.path.exists(unique_filename) and os.path.getsize(unique_filename) > 0:
|
| 118 |
print(f"π Generated track successfully saved to: {unique_filename}")
|
| 119 |
return unique_filename
|
|
@@ -145,15 +147,15 @@ def chatbot_engine(text_input, audio_input, target_voice_profile):
|
|
| 145 |
return generated_audio_track
|
| 146 |
|
| 147 |
# ==========================================
|
| 148 |
-
# 5. Gradio
|
| 149 |
# ==========================================
|
| 150 |
with gr.Blocks(title="Dynamic Voice Simulation Studio") as interface:
|
| 151 |
-
gr.Markdown("# π
|
| 152 |
gr.Markdown("Drop any source audio file to clone voice attributes and play back the generated simulated response.")
|
| 153 |
|
| 154 |
with gr.Row():
|
| 155 |
with gr.Column(scale=1):
|
| 156 |
-
gr.Markdown("### π
|
| 157 |
voice_profile_uploader = gr.Audio(
|
| 158 |
label="Target Voice to Clone",
|
| 159 |
type="filepath"
|
|
|
|
| 4 |
import uuid
|
| 5 |
import os
|
| 6 |
import soundfile as sf
|
|
|
|
| 7 |
from f5_tts.api import F5TTS
|
| 8 |
|
| 9 |
# ==========================================
|
|
|
|
| 19 |
print("Voice Simulation Matrix Active!")
|
| 20 |
|
| 21 |
# ==========================================
|
| 22 |
+
# 2. Dynamic Text Response Generation
|
| 23 |
# ==========================================
|
| 24 |
def get_ai_answer(question):
|
| 25 |
try:
|
|
|
|
| 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"][0]["message"]["content"].strip()
|
| 43 |
except Exception as e:
|
| 44 |
print(f"Groq API connection error: {e}")
|
| 45 |
+
|
| 46 |
+
# Clean programmatic default response if the network times out
|
| 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 Engine
|
| 51 |
# ==========================================
|
| 52 |
def clone_and_speak(text, dynamic_ref_audio):
|
| 53 |
try:
|
|
|
|
| 55 |
print("β Reference enrollment track missing.")
|
| 56 |
return None
|
| 57 |
|
| 58 |
+
# Automatically normalize audio format to single-channel Mono
|
|
|
|
| 59 |
clean_ref_path = f"processed_ref_{uuid.uuid4().hex[:4]}.wav"
|
| 60 |
try:
|
| 61 |
data, samplerate = sf.read(dynamic_ref_audio)
|
| 62 |
if len(data.shape) > 1:
|
| 63 |
+
data = data[:, 0]
|
| 64 |
sf.write(clean_ref_path, data, samplerate, format='WAV', subtype='PCM_16')
|
| 65 |
except Exception as audio_err:
|
| 66 |
print(f"Audio normalization warning: {audio_err}")
|
| 67 |
+
clean_ref_path = dynamic_ref_audio
|
| 68 |
|
| 69 |
# Automatically extract text transcript from reference clip
|
| 70 |
print(f"π Transcribing profile properties: {clean_ref_path}")
|
|
|
|
| 74 |
if not extracted_ref_text:
|
| 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 |
+
# PARAMETER PATCH: Use try/except blocks to map both 'ref_audio' and 'ref_file' arguments
|
| 81 |
try:
|
| 82 |
+
# Native F5-TTS Library Parameter Format (uses ref_audio)
|
| 83 |
+
outputs = f5tts.infer(
|
| 84 |
+
ref_audio=clean_ref_path,
|
|
|
|
| 85 |
ref_text=extracted_ref_text,
|
| 86 |
gen_text=text,
|
| 87 |
+
nfe_step=16,
|
|
|
|
| 88 |
speed=1.0
|
| 89 |
)
|
| 90 |
except TypeError:
|
| 91 |
+
# Alternative Custom Wrapper Format (uses ref_file)
|
| 92 |
outputs = f5tts.infer(
|
| 93 |
ref_file=clean_ref_path,
|
| 94 |
ref_text=extracted_ref_text,
|
| 95 |
gen_text=text,
|
| 96 |
+
nfe_step=16,
|
| 97 |
speed=1.0
|
| 98 |
)
|
| 99 |
+
|
| 100 |
+
# Unpack returned signal arrays safely
|
| 101 |
+
if isinstance(outputs, tuple):
|
| 102 |
+
if len(outputs) == 3:
|
| 103 |
+
wav_data, sr, _ = outputs
|
| 104 |
else:
|
| 105 |
+
wav_data, sr = outputs
|
| 106 |
+
else:
|
| 107 |
+
wav_data, sr = outputs, 24000
|
| 108 |
|
| 109 |
+
if hasattr(wav_data, "cpu"):
|
| 110 |
+
wav_data = wav_data.cpu().numpy()
|
| 111 |
|
| 112 |
+
# Write data to physical disk space for browser player rendering
|
| 113 |
+
sf.write(unique_filename, wav_data, sr, format='WAV', subtype='PCM_16')
|
| 114 |
|
| 115 |
+
# Clean temporary tracking paths
|
| 116 |
if os.path.exists(clean_ref_path) and clean_ref_path != dynamic_ref_audio:
|
| 117 |
os.remove(clean_ref_path)
|
| 118 |
|
|
|
|
| 119 |
if os.path.exists(unique_filename) and os.path.getsize(unique_filename) > 0:
|
| 120 |
print(f"π Generated track successfully saved to: {unique_filename}")
|
| 121 |
return unique_filename
|
|
|
|
| 147 |
return generated_audio_track
|
| 148 |
|
| 149 |
# ==========================================
|
| 150 |
+
# 5. Gradio Single-Page Interface Layout
|
| 151 |
# ==========================================
|
| 152 |
with gr.Blocks(title="Dynamic Voice Simulation Studio") as interface:
|
| 153 |
+
gr.Markdown("# π *Single-Page Voice Cloning Studio*")
|
| 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"
|