kevinwang676's picture
Update app.py
ee680f2 verified
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)