Update app.py
Browse files
app.py
CHANGED
|
@@ -3,25 +3,22 @@ import gradio as gr
|
|
| 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 |
|
| 10 |
-
#
|
|
|
|
|
|
|
| 11 |
GROQ_API_KEY = "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"
|
| 12 |
voice_file = "my_voice_fixed.wav"
|
| 13 |
|
| 14 |
-
# Automatically detect if space has access to GPU acceleration or fallback to CPU
|
| 15 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
-
print(f"π Initializing simulation engine on device: {device}")
|
| 17 |
-
|
| 18 |
print("Loading Whisper Model...")
|
| 19 |
-
whisper_model = whisper.load_model("base"
|
| 20 |
|
| 21 |
-
print("Loading F5-TTS Simulation Model...")
|
| 22 |
-
f5tts = F5TTS(
|
| 23 |
|
| 24 |
-
# Automatically extract reference text from
|
| 25 |
print(f"Extracting reference text from {voice_file}...")
|
| 26 |
try:
|
| 27 |
ref_result = whisper_model.transcribe(voice_file)
|
|
@@ -33,7 +30,9 @@ except Exception as e:
|
|
| 33 |
print(f"β οΈ Text extraction warning: {e}")
|
| 34 |
auto_reference_text = "Hello"
|
| 35 |
|
| 36 |
-
#
|
|
|
|
|
|
|
| 37 |
def get_ai_answer(question):
|
| 38 |
try:
|
| 39 |
response = requests.post(
|
|
@@ -50,44 +49,52 @@ def get_ai_answer(question):
|
|
| 50 |
timeout=10
|
| 51 |
)
|
| 52 |
if response.status_code == 200:
|
|
|
|
| 53 |
return response.json()["choices"][0]["message"]["content"].strip()
|
| 54 |
except Exception as e:
|
| 55 |
-
print(f"Groq API connection
|
| 56 |
|
| 57 |
-
#
|
| 58 |
q_lower = question.lower()
|
| 59 |
if "salesforce" in q_lower:
|
| 60 |
return "Salesforce is a cloud-based customer relationship management platform that helps businesses manage sales, marketing, and customer service tracking in one place."
|
| 61 |
-
return "Hello!
|
| 62 |
|
| 63 |
-
#
|
|
|
|
|
|
|
| 64 |
def clone_and_speak(text):
|
| 65 |
try:
|
| 66 |
unique_filename = f"voice_{uuid.uuid4().hex[:8]}.wav"
|
| 67 |
-
print(f"π
|
| 68 |
|
| 69 |
-
#
|
| 70 |
f5tts.infer(
|
| 71 |
ref_file=voice_file,
|
| 72 |
ref_text=auto_reference_text,
|
| 73 |
gen_text=text,
|
| 74 |
-
file_wave=unique_filename
|
|
|
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
-
#
|
| 78 |
if os.path.exists(unique_filename) and os.path.getsize(unique_filename) > 0:
|
|
|
|
| 79 |
data, samplerate = sf.read(unique_filename)
|
| 80 |
sf.write(unique_filename, data, 24000, format='WAV', subtype='PCM_16')
|
| 81 |
-
print(f"π Cloned audio track generated successfully: {unique_filename}")
|
| 82 |
return unique_filename
|
| 83 |
else:
|
| 84 |
-
print("β Audio
|
| 85 |
return None
|
| 86 |
|
| 87 |
except Exception as e:
|
| 88 |
-
print(f"β F5-TTS
|
| 89 |
return None
|
| 90 |
|
|
|
|
|
|
|
|
|
|
| 91 |
def chatbot(text_input, audio_input):
|
| 92 |
if audio_input is not None:
|
| 93 |
result = whisper_model.transcribe(audio_input)
|
|
@@ -101,7 +108,9 @@ def chatbot(text_input, audio_input):
|
|
| 101 |
audio_path = clone_and_speak(answer)
|
| 102 |
return answer, audio_path
|
| 103 |
|
| 104 |
-
#
|
|
|
|
|
|
|
| 105 |
interface = gr.Interface(
|
| 106 |
fn=chatbot,
|
| 107 |
inputs=[
|
|
|
|
| 3 |
import requests
|
| 4 |
import uuid
|
| 5 |
import os
|
|
|
|
| 6 |
import soundfile as sf
|
| 7 |
from f5_tts.api import F5TTS
|
| 8 |
|
| 9 |
+
# ==========================================
|
| 10 |
+
# 1. Setup Configuration & Model Loading
|
| 11 |
+
# ==========================================
|
| 12 |
GROQ_API_KEY = "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"
|
| 13 |
voice_file = "my_voice_fixed.wav"
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
print("Loading Whisper Model...")
|
| 16 |
+
whisper_model = whisper.load_model("base")
|
| 17 |
|
| 18 |
+
print("Loading F5-TTS Core Simulation Model...")
|
| 19 |
+
f5tts = F5TTS()
|
| 20 |
|
| 21 |
+
# Automatically extract reference text from your voice file at startup
|
| 22 |
print(f"Extracting reference text from {voice_file}...")
|
| 23 |
try:
|
| 24 |
ref_result = whisper_model.transcribe(voice_file)
|
|
|
|
| 30 |
print(f"β οΈ Text extraction warning: {e}")
|
| 31 |
auto_reference_text = "Hello"
|
| 32 |
|
| 33 |
+
# ==========================================
|
| 34 |
+
# 2. Text Response Function (Groq API)
|
| 35 |
+
# ==========================================
|
| 36 |
def get_ai_answer(question):
|
| 37 |
try:
|
| 38 |
response = requests.post(
|
|
|
|
| 49 |
timeout=10
|
| 50 |
)
|
| 51 |
if response.status_code == 200:
|
| 52 |
+
# Fixed list indexing to unpack payload properly
|
| 53 |
return response.json()["choices"][0]["message"]["content"].strip()
|
| 54 |
except Exception as e:
|
| 55 |
+
print(f"Groq API connection error: {e}")
|
| 56 |
|
| 57 |
+
# Text fallback baseline to prevent downstream TTS pipeline crashes
|
| 58 |
q_lower = question.lower()
|
| 59 |
if "salesforce" in q_lower:
|
| 60 |
return "Salesforce is a cloud-based customer relationship management platform that helps businesses manage sales, marketing, and customer service tracking in one place."
|
| 61 |
+
return "Hello! Your voice simulation system is online and ready for audio cloning."
|
| 62 |
|
| 63 |
+
# ==========================================
|
| 64 |
+
# 3. Voice Generation Block (F5-TTS)
|
| 65 |
+
# ==========================================
|
| 66 |
def clone_and_speak(text):
|
| 67 |
try:
|
| 68 |
unique_filename = f"voice_{uuid.uuid4().hex[:8]}.wav"
|
| 69 |
+
print(f"π Initiating F5-TTS generation -> Targeting: {unique_filename}")
|
| 70 |
|
| 71 |
+
# Generates and writes the file directly to your workspace using the unique name
|
| 72 |
f5tts.infer(
|
| 73 |
ref_file=voice_file,
|
| 74 |
ref_text=auto_reference_text,
|
| 75 |
gen_text=text,
|
| 76 |
+
file_wave=unique_filename,
|
| 77 |
+
nfe_step=32, # Balanced steps for clean sound quality output
|
| 78 |
+
speed=1.0
|
| 79 |
)
|
| 80 |
|
| 81 |
+
# Verify the file exists on disk and is formatted correctly for your browser player
|
| 82 |
if os.path.exists(unique_filename) and os.path.getsize(unique_filename) > 0:
|
| 83 |
+
print(f"π Voice generation successful! File written to disk: {unique_filename}")
|
| 84 |
data, samplerate = sf.read(unique_filename)
|
| 85 |
sf.write(unique_filename, data, 24000, format='WAV', subtype='PCM_16')
|
|
|
|
| 86 |
return unique_filename
|
| 87 |
else:
|
| 88 |
+
print("β Audio generation failed: File was not created on disk.")
|
| 89 |
return None
|
| 90 |
|
| 91 |
except Exception as e:
|
| 92 |
+
print(f"β F5-TTS Pipeline Exception Breakdown: {e}")
|
| 93 |
return None
|
| 94 |
|
| 95 |
+
# ==========================================
|
| 96 |
+
# 4. Main Chatbot Routing & Logic
|
| 97 |
+
# ==========================================
|
| 98 |
def chatbot(text_input, audio_input):
|
| 99 |
if audio_input is not None:
|
| 100 |
result = whisper_model.transcribe(audio_input)
|
|
|
|
| 108 |
audio_path = clone_and_speak(answer)
|
| 109 |
return answer, audio_path
|
| 110 |
|
| 111 |
+
# ==========================================
|
| 112 |
+
# 5. Gradio Interface Construction
|
| 113 |
+
# ==========================================
|
| 114 |
interface = gr.Interface(
|
| 115 |
fn=chatbot,
|
| 116 |
inputs=[
|