File size: 1,530 Bytes
a53c904
f6ef3c0
33efd3a
 
a53c904
 
33efd3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a53c904
33efd3a
 
a53c904
33efd3a
 
a53c904
33efd3a
 
 
 
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
import os
import gradio as gr
from elevenlabs import voices, generate, set_api_key, UnauthenticatedRateLimitError

def generate_voice(api_key, text, voice_name):
    os.environ["ELEVEN_API_KEY"] = api_key
    try:
        audio = generate(
            text[:5000], # Limit to 250 characters
            voice=voice_name, 
            model="eleven_multilingual_v2"
        )
        with open("output" + ".mp3", mode='wb') as f:
          f.write(audio)       
        return "output.mp3"        

    except UnauthenticatedRateLimitError as e:
        raise gr.Error("Thanks for trying out ElevenLabs TTS! You've reached the free tier limit. Please provide an API key to continue.") 
    except Exception as e:
        raise gr.Error(e)

all_voices = voices() 

with gr.Blocks() as app:
    gr.Markdown("# <center>🌊💕🎶 11Labs TTS</center>")
    with gr.Column():
        with gr.Row():
            inp0 = gr.Textbox(type='password', label='请输入您的11Labs API Key')
            inp1 = gr.Textbox(label="需要语音合成的文本")
            inp2 = gr.Dropdown(choices=[ voice.name for voice in all_voices ], label='请选择一个说话人音色', info="试听音色链接:https://huggingface.co/spaces/elevenlabs/tts", value='Rachel')
            btn = gr.Button("一键AI配音", variant="primary")
        with gr.Row():
            out1 = gr.Audio(label="为您合成的音频文件", type="filepath")
    btn.click(generate_voice, [inp0, inp1, inp2], [out1]) 

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