Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,25 +3,28 @@ from TTS.api import TTS
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# 1. Load Model
|
| 6 |
-
# We check if CUDA is available to select GPU or CPU
|
| 7 |
device = "cuda" if os.environ.get("CUDA_VISIBLE_DEVICES") else "cpu"
|
| 8 |
print(f"Loading XTTS v2 on {device}...")
|
| 9 |
|
| 10 |
-
|
| 11 |
-
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=(device == "cuda"))
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
def clone_voice(text, speaker_wav_path
|
| 14 |
-
# DEBUGGING: Print what we received
|
| 15 |
print(f"--- NEW REQUEST ---")
|
| 16 |
-
print(f"Text: {text}")
|
| 17 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
if not text:
|
| 20 |
-
|
| 21 |
-
|
| 22 |
if not speaker_wav_path:
|
| 23 |
-
|
| 24 |
-
return None
|
| 25 |
|
| 26 |
output_path = "output.wav"
|
| 27 |
|
|
@@ -31,29 +34,43 @@ def clone_voice(text, speaker_wav_path, language="en"):
|
|
| 31 |
text=text,
|
| 32 |
file_path=output_path,
|
| 33 |
speaker_wav=speaker_wav_path,
|
| 34 |
-
language=
|
| 35 |
)
|
| 36 |
print(f"Success! Generated: {output_path}")
|
| 37 |
-
return output_path
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
-
print(f"CRASH
|
| 41 |
-
return None
|
| 42 |
-
|
| 43 |
-
# 2.
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
gr.
|
| 49 |
-
label="
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
|
|
|
| 59 |
demo.launch(ssr_mode=False)
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# 1. Load Model
|
|
|
|
| 6 |
device = "cuda" if os.environ.get("CUDA_VISIBLE_DEVICES") else "cpu"
|
| 7 |
print(f"Loading XTTS v2 on {device}...")
|
| 8 |
|
| 9 |
+
try:
|
| 10 |
+
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=(device == "cuda"))
|
| 11 |
+
except Exception as e:
|
| 12 |
+
print(f"Error loading model: {e}")
|
| 13 |
+
tts = None
|
| 14 |
|
| 15 |
+
def clone_voice(text, speaker_wav_path):
|
|
|
|
| 16 |
print(f"--- NEW REQUEST ---")
|
| 17 |
+
print(f"Text Input: {text}")
|
| 18 |
+
print(f"Audio Path Received: {speaker_wav_path}")
|
| 19 |
+
|
| 20 |
+
if not tts:
|
| 21 |
+
return None, "Error: Model failed to load."
|
| 22 |
|
| 23 |
if not text:
|
| 24 |
+
return None, "Error: Please enter text."
|
| 25 |
+
|
| 26 |
if not speaker_wav_path:
|
| 27 |
+
return None, "Error: No audio file found. Please upload a WAV/MP3 file."
|
|
|
|
| 28 |
|
| 29 |
output_path = "output.wav"
|
| 30 |
|
|
|
|
| 34 |
text=text,
|
| 35 |
file_path=output_path,
|
| 36 |
speaker_wav=speaker_wav_path,
|
| 37 |
+
language="en"
|
| 38 |
)
|
| 39 |
print(f"Success! Generated: {output_path}")
|
| 40 |
+
return output_path, "Success"
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
+
print(f"CRASH: {e}")
|
| 44 |
+
return None, f"Error: {str(e)}"
|
| 45 |
+
|
| 46 |
+
# 2. Robust UI Layout (Blocks)
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
gr.Markdown("# PLOT Voice Engine (XTTS v2)")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
with gr.Column():
|
| 52 |
+
text_input = gr.Textbox(label="Text to Speak", value="Hello, this is my cloned voice.")
|
| 53 |
+
|
| 54 |
+
# EXPLICIT SOURCES: Allows both upload and microphone
|
| 55 |
+
audio_input = gr.Audio(
|
| 56 |
+
label="Reference Audio (6-10s)",
|
| 57 |
+
type="filepath",
|
| 58 |
+
sources=["upload", "microphone"]
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
submit_btn = gr.Button("Generate Voice", variant="primary")
|
| 62 |
+
|
| 63 |
+
with gr.Column():
|
| 64 |
+
audio_output = gr.Audio(label="Result")
|
| 65 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
| 66 |
+
|
| 67 |
+
# 3. Explicit Event Wiring
|
| 68 |
+
submit_btn.click(
|
| 69 |
+
fn=clone_voice,
|
| 70 |
+
inputs=[text_input, audio_input],
|
| 71 |
+
outputs=[audio_output, status_output]
|
| 72 |
+
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
+
# Disable SSR to fix upload bugs
|
| 76 |
demo.launch(ssr_mode=False)
|