Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,89 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from TTS.api import TTS
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
with gr.Column():
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
btn.click(text_to_speech, inputs=[text_input, audio_input_mic, audio_input_file], outputs=audio_output)
|
| 88 |
-
|
| 89 |
-
radio.change(toggle, radio, [audio_input_mic, audio_input_file])
|
| 90 |
-
btn_clear.click(clear_color, [text_input, radio, error_box], [text_input, radio, error_box])
|
| 91 |
-
btn.click(change_color, text_input, text_input)
|
| 92 |
-
|
| 93 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from TTS.api import TTS
|
| 3 |
+
import torch
|
| 4 |
+
import os
|
| 5 |
+
from pydub import AudioSegment
|
| 6 |
+
|
| 7 |
+
# β
Environment fixes
|
| 8 |
+
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 9 |
+
os.environ["TTS_MODELS_PATH"] = "/home/user/app/coqui_models"
|
| 10 |
+
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
|
| 13 |
+
# β
Load XTTS model efficiently
|
| 14 |
+
try:
|
| 15 |
+
tts = TTS(
|
| 16 |
+
model_name="tts_models/multilingual/multi-dataset/xtts_v2",
|
| 17 |
+
progress_bar=True, # Speeds up processing
|
| 18 |
+
gpu=(device == "cuda") # β
Remove `trust_remote_code=True`
|
| 19 |
+
)
|
| 20 |
+
tts.to(device)
|
| 21 |
+
print(f"[INFO] XTTS model loaded successfully on {device}")
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"[ERROR] Failed to load XTTS model: {e}")
|
| 24 |
+
raise e
|
| 25 |
+
|
| 26 |
+
# β
Optimize MP3 to WAV conversion
|
| 27 |
+
def convert_mp3_to_wav(mp3_path: str) -> str:
|
| 28 |
+
wav_path = mp3_path.replace(".mp3", ".wav")
|
| 29 |
+
if not os.path.exists(wav_path):
|
| 30 |
+
try:
|
| 31 |
+
audio = AudioSegment.from_file(mp3_path)
|
| 32 |
+
audio.export(wav_path, format="wav")
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"[ERROR] MP3 conversion failed: {e}")
|
| 35 |
+
return None
|
| 36 |
+
return wav_path
|
| 37 |
+
|
| 38 |
+
# β
Fix Speaker File Handling & Text Processing for Long Inputs
|
| 39 |
+
def text_to_speech(text: str, speaker_wav: str, speaker_wav_file: str):
|
| 40 |
+
text = text.strip().replace("\n", " ")[:1500] # β
Supports up to 10+ lines
|
| 41 |
+
|
| 42 |
+
speaker_audio = speaker_wav_file or speaker_wav
|
| 43 |
+
if not text:
|
| 44 |
+
return None, "β οΈ Error: Text input is empty."
|
| 45 |
+
if not speaker_audio or not os.path.exists(speaker_audio):
|
| 46 |
+
return None, "β οΈ Error: No valid speaker audio provided."
|
| 47 |
+
|
| 48 |
+
if speaker_audio.endswith(".mp3"):
|
| 49 |
+
speaker_audio = convert_mp3_to_wav(speaker_audio)
|
| 50 |
+
if not speaker_audio:
|
| 51 |
+
return None, "β οΈ Error converting MP3 to WAV."
|
| 52 |
+
|
| 53 |
+
output_path = "output.wav"
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
# β
Ensure correct tensor shape for attention mask
|
| 57 |
+
attention_mask = torch.ones((1, len(text.split())), dtype=torch.float32).to(device)
|
| 58 |
+
|
| 59 |
+
tts.tts_to_file(text=text, speaker_wav=speaker_audio, language="en", file_path=output_path, attention_mask=attention_mask)
|
| 60 |
+
|
| 61 |
+
if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
|
| 62 |
+
return output_path, ""
|
| 63 |
+
else:
|
| 64 |
+
return None, "β οΈ Error: Audio was not generated."
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return None, f"β οΈ Error during synthesis: {str(e)}"
|
| 67 |
+
|
| 68 |
+
# β
Gradio UI setup
|
| 69 |
+
with gr.Blocks() as demo:
|
| 70 |
+
with gr.Row():
|
| 71 |
+
with gr.Column():
|
| 72 |
+
text_input = gr.Textbox(label="Enter text to clone", max_lines=15, lines=15) # β
Supports long input
|
| 73 |
+
radio = gr.Radio(["mic", "file"], value="mic", label="Upload speaker audio")
|
| 74 |
+
audio_input_mic = gr.Audio(label="Use Microphone", sources="microphone", type="filepath", visible=True)
|
| 75 |
+
audio_input_file = gr.Audio(label="Upload File (.wav/.mp3)", type="filepath", visible=False)
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
with gr.Column():
|
| 79 |
+
btn_clear = gr.ClearButton([text_input, radio, audio_input_file])
|
| 80 |
+
with gr.Column():
|
| 81 |
+
btn = gr.Button("Generate Voice", variant="primary")
|
| 82 |
+
|
| 83 |
+
with gr.Column():
|
| 84 |
+
audio_output = gr.Audio(label="Generated Voice", visible=True, autoplay=True)
|
| 85 |
+
error_box = gr.Textbox(label="Status", value="", visible=False)
|
| 86 |
+
|
| 87 |
+
btn.click(text_to_speech, inputs=[text_input, audio_input_mic, audio_input_file], outputs=[audio_output, error_box])
|
| 88 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
demo.launch()
|