File size: 1,163 Bytes
e3e1200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

import gradio as gr
from TTS.api import TTS
import tempfile

# Load XTTS model
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")

def clone_voice(text, speaker_wav):
    if speaker_wav is None:
        return None, "Please upload a reference audio file."

    # Save uploaded audio
    with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
        tmp.write(speaker_wav.read())
        speaker_path = tmp.name

    output_path = "cloned_output.wav"

    # Generate audio using XTTS
    tts.tts_to_file(
        text=text,
        speaker_wav=speaker_path,
        language="zh",
        file_path=output_path
    )

    return output_path, "Voice cloning completed successfully."

# Gradio UI
demo = gr.Interface(
    fn=clone_voice,
    inputs=[
        gr.Textbox(label="Enter Chinese Text"),
        gr.Audio(label="Upload Speaker Audio (.wav)", type="file")
    ],
    outputs=[
        gr.Audio(label="Cloned Output Audio"),
        gr.Textbox(label="Status")
    ],
    title="XTTS Voice Cloning Demo",
    description="Upload reference audio and enter Chinese text to generate speech in cloned voice."
)

demo.launch()