Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import uuid
|
| 5 |
import os
|
|
|
|
| 6 |
import soundfile as sf
|
| 7 |
from f5_tts.api import F5TTS
|
| 8 |
|
|
@@ -45,10 +46,10 @@ def get_ai_answer(question):
|
|
| 45 |
q_lower = question.lower()
|
| 46 |
if "salesforce" in q_lower:
|
| 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 |
-
return f"I processed your query about '{question}'. The
|
| 49 |
|
| 50 |
# ==========================================
|
| 51 |
-
# 3. Dynamic Voice Synthesis (
|
| 52 |
# ==========================================
|
| 53 |
def clone_and_speak(text, dynamic_ref_audio):
|
| 54 |
try:
|
|
@@ -56,7 +57,7 @@ def clone_and_speak(text, dynamic_ref_audio):
|
|
| 56 |
print("⚠️ Error: No reference enrollment voice track provided.")
|
| 57 |
return None
|
| 58 |
|
| 59 |
-
# Automatically extract text from reference file
|
| 60 |
print(f"🔊 Analyzing voice file characteristics: {dynamic_ref_audio}")
|
| 61 |
ref_transcription = whisper_model.transcribe(dynamic_ref_audio)
|
| 62 |
extracted_ref_text = ref_transcription["text"].strip()
|
|
@@ -65,11 +66,11 @@ def clone_and_speak(text, dynamic_ref_audio):
|
|
| 65 |
extracted_ref_text = "Hello"
|
| 66 |
print(f"🎯 Successfully extracted reference transcript: '{extracted_ref_text}'")
|
| 67 |
|
| 68 |
-
# Create unique
|
| 69 |
unique_filename = f"voice_clone_{uuid.uuid4().hex[:8]}.wav"
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
ref_file=dynamic_ref_audio,
|
| 74 |
ref_text=extracted_ref_text,
|
| 75 |
gen_text=text,
|
|
@@ -77,7 +78,18 @@ def clone_and_speak(text, dynamic_ref_audio):
|
|
| 77 |
speed=1.0
|
| 78 |
)
|
| 79 |
|
| 80 |
-
# FIX:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
sf.write(unique_filename, wav_data, sr, format='WAV', subtype='PCM_16')
|
| 82 |
|
| 83 |
if os.path.exists(unique_filename) and os.path.getsize(unique_filename) > 0:
|
|
|
|
| 3 |
import requests
|
| 4 |
import uuid
|
| 5 |
import os
|
| 6 |
+
import torch
|
| 7 |
import soundfile as sf
|
| 8 |
from f5_tts.api import F5TTS
|
| 9 |
|
|
|
|
| 46 |
q_lower = question.lower()
|
| 47 |
if "salesforce" in q_lower:
|
| 48 |
return "Salesforce is a cloud-based customer relationship management platform that helps businesses manage sales, marketing, and customer service tracking in one place."
|
| 49 |
+
return f"I processed your query about '{question}'. The voice cloning engine is computing the response track now."
|
| 50 |
|
| 51 |
# ==========================================
|
| 52 |
+
# 3. Dynamic Voice Synthesis (Adaptive Unpacking)
|
| 53 |
# ==========================================
|
| 54 |
def clone_and_speak(text, dynamic_ref_audio):
|
| 55 |
try:
|
|
|
|
| 57 |
print("⚠️ Error: No reference enrollment voice track provided.")
|
| 58 |
return None
|
| 59 |
|
| 60 |
+
# Automatically extract text from the uploaded reference file
|
| 61 |
print(f"🔊 Analyzing voice file characteristics: {dynamic_ref_audio}")
|
| 62 |
ref_transcription = whisper_model.transcribe(dynamic_ref_audio)
|
| 63 |
extracted_ref_text = ref_transcription["text"].strip()
|
|
|
|
| 66 |
extracted_ref_text = "Hello"
|
| 67 |
print(f"🎯 Successfully extracted reference transcript: '{extracted_ref_text}'")
|
| 68 |
|
| 69 |
+
# Create unique filename to clear browser playback cache issues
|
| 70 |
unique_filename = f"voice_clone_{uuid.uuid4().hex[:8]}.wav"
|
| 71 |
|
| 72 |
+
# Execute F5-TTS simulation matrix inference
|
| 73 |
+
outputs = f5tts.infer(
|
| 74 |
ref_file=dynamic_ref_audio,
|
| 75 |
ref_text=extracted_ref_text,
|
| 76 |
gen_text=text,
|
|
|
|
| 78 |
speed=1.0
|
| 79 |
)
|
| 80 |
|
| 81 |
+
# FIX: Adaptively unpack output regardless of package version layout
|
| 82 |
+
if isinstance(outputs, tuple):
|
| 83 |
+
wav_data = outputs[0]
|
| 84 |
+
sr = outputs[1]
|
| 85 |
+
else:
|
| 86 |
+
wav_data, sr = outputs
|
| 87 |
+
|
| 88 |
+
# Convert torch tensor back to NumPy structure if needed
|
| 89 |
+
if hasattr(wav_data, "cpu"):
|
| 90 |
+
wav_data = wav_data.cpu().numpy()
|
| 91 |
+
|
| 92 |
+
# FIX: Save raw memory structures explicitly onto your host disk space
|
| 93 |
sf.write(unique_filename, wav_data, sr, format='WAV', subtype='PCM_16')
|
| 94 |
|
| 95 |
if os.path.exists(unique_filename) and os.path.getsize(unique_filename) > 0:
|