Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,38 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from TTS.api import TTS
|
| 4 |
-
from pydub import AudioSegment
|
| 5 |
import os
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2").to(device)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
if file_path.endswith(".mp3"):
|
| 14 |
-
sound = AudioSegment.from_mp3(file_path)
|
| 15 |
-
wav_path = file_path.replace(".mp3", ".wav")
|
| 16 |
-
sound.export(wav_path, format="wav")
|
| 17 |
-
return wav_path
|
| 18 |
-
return file_path
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
if not sample_voice or not text.strip():
|
| 23 |
-
return None
|
| 24 |
-
sample_voice = convert_to_wav(sample_voice)
|
| 25 |
-
output_path = "output.wav"
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Gradio UI
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
-
gr.Markdown("## π€ Voice Cloning
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from TTS.api import TTS
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
+
# β
Auto-accept Coqui license (non-commercial CPML)
|
| 6 |
+
os.environ["COQUI_TOS_AGREED"] = "1"
|
|
|
|
| 7 |
|
| 8 |
+
# Pick device
|
| 9 |
+
device = "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Load XTTS-v2
|
| 12 |
+
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2").to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
def clone_voice(sample_audio, text):
|
| 15 |
+
if sample_audio is None:
|
| 16 |
+
return "Please upload a voice sample.", None
|
| 17 |
+
|
| 18 |
+
output_path = "output.wav"
|
| 19 |
+
tts.tts_to_file(text=text,
|
| 20 |
+
speaker_wav=sample_audio,
|
| 21 |
+
language="en",
|
| 22 |
+
file_path=output_path)
|
| 23 |
+
return f"Generated speech for: {text}", output_path
|
| 24 |
|
| 25 |
# Gradio UI
|
| 26 |
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("## π€ Voice Cloning with XTTS-v2")
|
| 28 |
+
with gr.Row():
|
| 29 |
+
sample = gr.Audio(label="Upload a sample voice", type="filepath")
|
| 30 |
+
text = gr.Textbox(label="Enter text to speak")
|
| 31 |
+
btn = gr.Button("Generate Voice")
|
| 32 |
+
output_text = gr.Textbox(label="Status")
|
| 33 |
+
output_audio = gr.Audio(label="Cloned Voice", type="filepath")
|
| 34 |
+
|
| 35 |
+
btn.click(fn=clone_voice, inputs=[sample, text], outputs=[output_text, output_audio])
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
demo.launch()
|