gradio / 11LabsTTS.py
Mira1sen's picture
Upload folder using huggingface_hub
e569c5f verified
"""
import requests
import gradio as gr
CHUNK_SIZE = 1024
headers1 = {
"Accept": "application/json",
"xi-api-key": "54f884da3108725f26af02d5907d1eb4"
}
headers2 = {
"Accept": "audio/mpeg",
"Content-Type": "application/json",
"xi-api-key": "54f884da3108725f26af02d5907d1eb4"
}
name_list = []
def elevenlabs_tts(text, voice_name):
# 获取声音列表
url1 = "https://api.elevenlabs.io/v1/voices"
response1 = requests.get(url1, headers=headers1)
voices = response1.json()['voices']
for voice in voices:
vid = voice['voice_id']
vname = voice['name']
label = voice['labels']
info = {"voice_id": vid, "name": vname, "labels": label}
name_list.append(info["name"])
if info['name'] == voice_name:
voice_id = info['voice_id']
url2 = "https://api.elevenlabs.io/v1/text-to-speech/" + voice_id
# print(infos)
# return infos
# 根据指定的人名合成语音
data = {
"text": text,
"model_id": "eleven_monolingual_v1",
"voice_settings": {
"stability": 0.5,
"similarity_boost": 0.5
}
}
response2 = requests.post(url2, json=data, headers=headers2)
with open('output.mp3', 'wb') as f:
for chunk in response2.iter_content(chunk_size=CHUNK_SIZE):
if chunk:
f.write(chunk)
return 'output.mp3'
demo = gr.Interface(
fn = elevenlabs_tts,
# demo输入设置
inputs = [
gr.Dropdown(name_list, label="发音人"),
gr.Textbox(label="输入文本"),
],
# demo输出设置
outputs = [
"audio",
"text",
],
# demo其他设置
title = "Text to Speech Synthesis",
description = "Synthesize speech from text using Elevenlabs",
examples = [
["Rachel", "Hello World!"],
["Clyde", "This is a test."],
["Domi", "Gradio is awesome!"],
]
)
if __name__ == "__main__":
demo.launch(share=True, server_name='0.0.0.0', server_port=8121)
#print(name_list)
"""
import requests
import gradio as gr
CHUNK_SIZE = 1024
headers1 = {
"Accept": "application/json",
"xi-api-key": "54f884da3108725f26af02d5907d1eb4"
}
headers2 = {
"Accept": "audio/mpeg",
"Content-Type": "application/json",
"xi-api-key": "54f884da3108725f26af02d5907d1eb4"
}
def get_voice_names():
url1 = "https://api.elevenlabs.io/v1/voices"
response1 = requests.get(url1, headers=headers1)
voices = response1.json()['voices']
names = [voice['name'] for voice in voices]
return names
name_list = get_voice_names()
def elevenlabs_tts(voice_name, text):
# 获取声音列表
url1 = "https://api.elevenlabs.io/v1/voices"
response1 = requests.get(url1, headers=headers1)
voices = response1.json()['voices']
#print(voice_name)
#print(voices)
for voice in voices:
if voice['name'] == voice_name:
voice_id = voice['voice_id']
label = voice['labels']
url2 = "https://api.elevenlabs.io/v1/text-to-speech/" + voice_id
#print(voice_id)
#print(label)
break
data = {
"text": text,
"model_id": "eleven_monolingual_v1",
"voice_settings": {
"stability": 0.5,
"similarity_boost": 0.5
}
}
response2 = requests.post(url2, json=data, headers=headers2)
with open('output.mp3', 'wb') as f:
for chunk in response2.iter_content(chunk_size=CHUNK_SIZE):
if chunk:
f.write(chunk)
return 'output.mp3', label
demo = gr.Interface(
fn = elevenlabs_tts,
inputs = [
gr.Dropdown(name_list, label="发音人"),
gr.Textbox(label="输入文本"),
],
outputs = [
"audio",
"text",
],
title = "Text to Speech Synthesis",
description = "Synthesize speech from text using Elevenlabs",
examples = [
["Rachel", "Hello World!"],
["Clyde", "This is a test."],
["Domi", "Gradio is awesome!"],
]
)
if __name__ == "__main__":
demo.launch(share=True, server_name='0.0.0.0', server_port=8121)