File size: 4,200 Bytes
e569c5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
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)