File size: 2,879 Bytes
bd2e9c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae6dc20
bd2e9c6
 
 
 
 
ca7888c
 
a8b078b
 
 
 
 
 
 
 
 
 
 
ca7888c
 
 
bd2e9c6
 
 
 
 
 
 
 
ee680f2
bd2e9c6
 
 
fc33771
ca7888c
 
 
bd2e9c6
ca7888c
bd2e9c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import gradio as gr
from elevenlabs.client import ElevenLabs


def convert(api_key, text, tgt):
  client = ElevenLabs(
    api_key=api_key, # Defaults to ELEVEN_API_KEY
  )

  voice = client.clone(
      name="huggingface",
      description="", 
      files=[tgt],
  )
  audio = client.generate(text=text, voice=voice)
  audio = b"".join(audio)
  with open("output.mp3", "wb") as f:
      f.write(audio)
  return "output.mp3"

def tts(api_key, text, voice):
    client = ElevenLabs(
      api_key=api_key, # Defaults to ELEVEN_API_KEY
    )
    
    #response = client.voices.get_all()
    audio = client.generate(text=text, voice=voice) #response.voices[0]
    audio = b"".join(audio)
    with open("output_tts.mp3", "wb") as f:
        f.write(audio)
    return "output_tts.mp3"

from pydub import AudioSegment

from pydub.effects import speedup

def change_speed(input_file, speed=1.0):
    # Load the audio file
    audio = AudioSegment.from_file(input_file)

    # Change the speed of the audio
    faster_audio = speedup(audio, playback_speed=speed)

    # Export the modified audio to a new file
    faster_audio.export("speed_changed_speech.wav", format="wav")
    return "speed_changed_speech.wav"


with gr.Blocks() as app:
    gr.Markdown("# <center>🌊💕🎶 11Labs TTS</center>")

    with gr.Tab("🎶文本转语音"):
        with gr.Column():
            with gr.Row():
                inp0 = gr.Textbox(type='password', label='请输入您的11Labs API Key')
                inp1 = gr.Textbox(label="需要语音合成的文本")
                inp2 = gr.Dropdown(choices=["Rachel", "Alice", "Chris", "Adam"], label='请选择一个说话人音色', value='Chris')
                btn = gr.Button("一键AI配音", variant="primary")
            with gr.Row():
                out1 = gr.Audio(label="为您合成的音频文件", type="filepath")
                inp_speed = gr.Slider(label="设置AI配音的速度", minimum=1.02, maximum=1.5, value=1.02, step=0.01)
                btn2 = gr.Button("一键改变AI配音速度")
                out2 = gr.Audio(label="变速后的AI配音", type="filepath")

        btn.click(tts, [inp0, inp1, inp2], [out1]) 
        btn2.click(change_speed, [out1, inp_speed], [out2])
    with gr.Tab("💕声音克隆"):
        with gr.Column():
            with gr.Row():
                inp10 = gr.Textbox(type='password', label='请输入您的11Labs API Key')
                inp11 = gr.Textbox(label="需要语音合成的文本")
                inp12 = gr.Audio(label="请上传音色的参考音频", type="filepath")
                btn1 = gr.Button("一键AI配音", variant="primary")
            with gr.Row():
                out11 = gr.Audio(label="为您合成的音频文件", type="filepath")
        btn1.click(convert, [inp10, inp11, inp12], [out11]) 


app.launch(share=False, show_error=True)