Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from TTS.api import TTS
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# 1. Load the Model (Uses GPU if available, otherwise CPU)
|
| 6 |
+
# The first run will take about 2-3 minutes to download the 2GB model.
|
| 7 |
+
device = "cuda" if os.environ.get("CUDA_VISIBLE_DEVICES") else "cpu"
|
| 8 |
+
print(f"Loading XTTS v2 on {device}...")
|
| 9 |
+
|
| 10 |
+
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=(device == "cuda"))
|
| 11 |
+
|
| 12 |
+
# 2. Define the Generator Function
|
| 13 |
+
def clone_voice(text, speaker_wav_path, language="en"):
|
| 14 |
+
if not text or not speaker_wav_path:
|
| 15 |
+
return None
|
| 16 |
+
|
| 17 |
+
output_path = "output.wav"
|
| 18 |
+
|
| 19 |
+
# Generate the audio using the uploaded sample
|
| 20 |
+
tts.tts_to_file(
|
| 21 |
+
text=text,
|
| 22 |
+
file_path=output_path,
|
| 23 |
+
speaker_wav=speaker_wav_path,
|
| 24 |
+
language=language
|
| 25 |
+
)
|
| 26 |
+
return output_path
|
| 27 |
+
|
| 28 |
+
# 3. Create the API Interface
|
| 29 |
+
demo = gr.Interface(
|
| 30 |
+
fn=clone_voice,
|
| 31 |
+
inputs=[
|
| 32 |
+
gr.Textbox(label="Text to Speak"),
|
| 33 |
+
gr.Audio(type="filepath", label="Upload Voice Sample (WAV/MP3)")
|
| 34 |
+
],
|
| 35 |
+
outputs=gr.Audio(label="Generated Audio"),
|
| 36 |
+
title="PLOT Voice Engine (XTTS v2)",
|
| 37 |
+
description="Upload a 6-second voice sample to clone it."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
demo.launch()
|