Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from TTS.api import TTS
|
| 3 |
+
|
| 4 |
+
# Load YourTTS / XTTS v2 model from Hugging Face
|
| 5 |
+
model_name = "tts_models/multilingual/multi-dataset/xtts_v2"
|
| 6 |
+
tts = TTS(model_name)
|
| 7 |
+
|
| 8 |
+
def generate_speech(text, emotion, ref_audio=None):
|
| 9 |
+
output_path = "output.wav"
|
| 10 |
+
|
| 11 |
+
# If a reference voice is provided, use it for cloning
|
| 12 |
+
if ref_audio:
|
| 13 |
+
tts.tts_to_file(
|
| 14 |
+
text=text,
|
| 15 |
+
file_path=output_path,
|
| 16 |
+
speaker_wav=ref_audio,
|
| 17 |
+
emotion=emotion
|
| 18 |
+
)
|
| 19 |
+
else:
|
| 20 |
+
tts.tts_to_file(
|
| 21 |
+
text=text,
|
| 22 |
+
file_path=output_path,
|
| 23 |
+
emotion=emotion
|
| 24 |
+
)
|
| 25 |
+
return output_path
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=generate_speech,
|
| 30 |
+
inputs=[
|
| 31 |
+
gr.Textbox(label="Text to Speak"),
|
| 32 |
+
gr.Dropdown(choices=["neutral", "happy", "sad", "angry"], label="Emotion"),
|
| 33 |
+
gr.Audio(source="upload", type="filepath", label="Reference Voice (optional)")
|
| 34 |
+
],
|
| 35 |
+
outputs=gr.Audio(type="filepath"),
|
| 36 |
+
title="Voice Cloning with Emotions",
|
| 37 |
+
description="Upload a short voice sample, type text, pick emotion → Get speech in that cloned voice."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
iface.launch()
|