File size: 8,211 Bytes
9f18b09
 
 
 
 
 
 
3695ec3
9f18b09
 
 
 
4eb6878
9f18b09
 
 
 
 
 
 
e80b05f
9f18b09
 
e80b05f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f18b09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8826dde
 
9f18b09
 
 
 
 
 
 
 
4eb6878
 
9f18b09
 
 
 
 
 
 
 
 
 
 
 
 
8826dde
9f18b09
 
 
 
 
 
 
 
 
 
 
 
105d0ac
9f18b09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64f43ed
 
 
 
 
105d0ac
0a16fb5
 
 
 
 
9f18b09
 
 
 
 
 
 
 
 
 
 
e80b05f
9f18b09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f2ba2a
 
9f18b09
 
 
 
 
 
 
 
 
 
 
 
105d0ac
9f18b09
105d0ac
4eb6878
9f18b09
 
 
 
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import warnings
warnings.filterwarnings("ignore")

# 外部库
import re
import requests
import argparse
import time
import os
import re
import tempfile
# import librosa
import numpy as np
# import torch
# from torch import no_grad, LongTensor
# import commons
import gradio as gr
# import gradio.utils as gr_utils
# import gradio.processing_utils as gr_processing_utils

all_example = "Today is a wonderful day to build something people love!"

microsoft_model_list = [
    "en-US-JennyMultilingualNeural",
    "en-US-RyanMultilingualNeural",
    "en-US-AndrewMultilingualNeural",
    "en-US-AvaMultilingualNeural",
    "en-US-BrianMultilingualNeural",
    "en-US-EmmaMultilingualNeural",
    "en-US-AlloyMultilingualNeural",
    "en-US-EchoMultilingualNeural",
    "en-US-FableMultilingualNeural",
    "en-US-OnyxMultilingualNeural",
    "en-US-NovaMultilingualNeural",
    "en-US-ShimmerMultilingualNeural",
    "en-US-AlloyMultilingualNeuralHD",
    "en-US-EchoMultilingualNeuralHD",
    "en-US-FableMultilingualNeuralHD",
    "en-US-OnyxMultilingualNeuralHD",
    "en-US-NovaMultilingualNeuralHD4",
    "en-US-ShimmerMultilingualNeuralHD"
]

openai_model_list = [
    "alloy",
    "echo",
    "fable",
    "onyx",
    "nova",
    "shimmer"
]

eleven_voice_id = [
    "21m00Tcm4TlvDq8ikWAM",
    "29vD33N1CtxCmqQRPOHJ",
    "2EiwWnXFnvU5JabPnv8n",
    "5Q0t7uMcjvnagumLfvZi",
    "AZnzlk1XvdvUeBnXmlld",
    "CYw3kZ02Hs0563khs1Fj",
    "D38z5RcWu1voky8WS1ja",
    "EXAVITQu4vr4xnSDxMaL",
    "ErXwobaYiN019PkySvjV",
    "GBv7mTt0atIp3Br8iCZE",
    "IKne3meq5aSn9XLyUdCD",
    "JBFqnCBsd6RMkjVDRZzb",
    "LcfcDJNUP1GQjkzn1xUU",
    "MF3mGyEYCl7XYWbV9V6O",
    "N2lVS1w4EtoT3dr4eOWO",
    "ODq5zmih8GrVes37Dizd",
    "SOYHLrjzK2X1ezoPC6cr",
    "TX3LPaxmHKxFdv7VOQHJ",
    "ThT5KcBeYPX3keUQqHPh",
    "TxGEqnHWrfWFTfGW9XjX",
    "VR6AewLTigWG4xSOukaG",
    "XB0fDUnXU5powFXDhCwa",
    "Xb7hH8MSUJpSbSDYk0k2",
    "XrExE9yKIg1WjnnlVkGX",
    "ZQe5CZNOzWyzPSCn5a3c",
    "Zlb1dXrM653N07WRdFW3",
    "bVMeCyTHy58xNoL34h3p",
    "flq6f7yk4E4fJM5XTYuZ",
    "g5CIjZEefAph4nQFvHAz",
    "iP95p4xoKVk53GoZ742B",
    "jBpfuIE2acCO8z3wKNLl",
    "jsCqWAovK2LkecY7zXl4",
    "nPczCjzI2devNBz1zQrb",
    "oWAxZDx7w5VEj9dCyTzz",
    "onwK4e9ZLuTAKqWW03F9",
    "pFZP5JQG7iQjIQuC4Bku",
    "pMsXgVXv3BLzUgSXRplE",
    "pNInz6obpgDQGcFmaJgB",
    "piTKgcLEGmPE4e6mEKli",
    "pqHfZKP75CvOlQylNhV4",
    "t0jbNlBVZ17f02VDIeMI",
    "yoZ06aMxZJJ28mfd3POQ",
    "z9fAnlkpzviPz146aGWa",
    "zcAOhNBS3c14rBihAFp1",
    "zrHiDhphv9ZnVXBqCLjz",
]

eleven_name = [
    "Rachel",
    "Drew",
    "Clyde",
    "Paul",
    "Domi",
    "Dave",
    "Fin",
    "Sarah",
    "Antoni",
    "Thomas",
    "Charlie",
    "George",
    "Emily",
    "Elli",
    "Callum",
    "Patrick",
    "Harry",
    "Liam",
    "Dorothy",
    "Josh",
    "Arnold",
    "Charlotte",
    "Alice",
    "Matilda",
    "James",
    "Joseph",
    "Jeremy",
    "Michael",
    "Ethan",
    "Chris",
    "Gigi",
    "Freya",
    "Brian",
    "Grace",
    "Daniel",
    "Lily",
    "Serena",
    "Adam",
    "Nicole",
    "Bill",
    "Jessie",
    "Sam",
    "Glinda",
    "Giovanni",
    "Mimi",
]
eleven_id_model_name_dict = dict(zip(eleven_name, eleven_voice_id))

def openai(text, name):

    headers = {
        'Authorization': 'Bearer ' + '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',
        'Content-Type': 'application/json',
    }

    json_data = {
        'model': 'tts-1-hd',
        'input': text,
        'voice': name,
    }

    response = requests.post('https://api.openai.com/v1/audio/speech', headers=headers, json=json_data)

    # Note: json_data will not be serialized by requests
    # exactly as it was in the original request.
    #data = '{\n    "model": "tts-1",\n    "input": "The quick brown fox jumped over the lazy dog.",\n    "voice": "alloy"\n  }'
    #response = requests.post('https://api.openai.com/v1/audio/speech', headers=headers, data=data)
    out_arr = np.frombuffer(response.content, dtype=np.uint8)
    return "Success", (24000,out_arr)

def elevenlabs(text,name):
    url = f"https://api.elevenlabs.io/v1/text-to-speech/{eleven_id_model_name_dict[name]}"
    CHUNK_SIZE = 1024
    #url = "https://api.elevenlabs.io/v1/text-to-speech/<voice-id>"

    headers = {
    "Accept": "audio/mpeg",
    "Content-Type": "application/json",
    "xi-api-key": "a3391f0e3ff8472b61978dbb70ccc6fe"
    }

    data = {
    "text": text,
    "model_id": "eleven_monolingual_v1",
    "voice_settings": {
        "stability": 0.5,
        "similarity_boost": 0.5
    }
    }

    response = requests.post(url, json=data, headers=headers)
    # with open('output.mp3', 'wb') as f:
    #     for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
    #         if chunk:
    #             f.write(chunk)
    return "Success", response

def microsoft(text, name, style="Neural"):
    """
    :param text:
    :param name:
    :param style:
    :return:
    """
    headers = {
        'Ocp-Apim-Subscription-Key': '1f1ef0ce53b84261be94fab81df7e628',
        'Content-Type': 'application/ssml+xml',
        'X-Microsoft-OutputFormat': 'audio-16khz-128kbitrate-mono-mp3',
        'User-Agent': 'curl',
    }

    data = ("<speak version='1.0' xml:lang='en-US'>"
        f"<voice xml:lang='en-US' name='{name}'>" # xml:gender='Female' 
        f"{text}"
        "</voice>"
        "</speak>")

    response = requests.post(
        'https://japaneast.tts.speech.microsoft.com/cognitiveservices/v1',
        headers=headers,
        data=data,
    )
    # breakpoint()
    timestamp = int(time.time()*10000)
    path = f'/tmp/output_{timestamp}.wav' # TODO: disk might full.
    with open(path, 'wb') as f:
        f.write(response.content)
    return "Success", path
    
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--device', type=str, default='cuda')
    parser.add_argument("--share", action="store_true", default=True, help="share gradio app")
    parser.add_argument("--port", type=int, default=8081, help="port")
    parser.add_argument('--model_info_path', type=str, default='/gluster/speech_data/info.json')
    args = parser.parse_args()

    app = gr.Blocks()
    with app:
        gr.Markdown("## English TTS Demo")
        with gr.Tabs():
         
            with gr.TabItem("11Labs"):
                tts_input1 = gr.TextArea(label="Text", value=all_example)
                tts_input2 = gr.Dropdown(eleven_name, label="name")                             
                tts_submit = gr.Button("Generate", variant="primary")
                tts_output1 = gr.Textbox(label="Output Message")
                tts_output2 = gr.Audio(label="Output Audio")
                tts_submit.click(elevenlabs, [tts_input1, tts_input2],
                                    [tts_output1, tts_output2])

            with gr.TabItem("微软"):
                tts_input1 = gr.TextArea(label="Text", value=all_example)
                tts_input2 = gr.Dropdown(microsoft_model_list, label="name")                             
                tts_submit = gr.Button("Generate", variant="primary")
                tts_output1 = gr.Textbox(label="Output Message")
                #tts_output2 = gr.Textbox(label="Output Audio")
                tts_output2 = gr.Audio(label="Output Audio")
                tts_submit.click(microsoft, [tts_input1, tts_input2],
                                    [tts_output1, tts_output2])
                
            with gr.TabItem("openai"):
                tts_input1 = gr.TextArea(label="Text", value=all_example)
                tts_input2 = gr.Dropdown(openai_model_list, label="name")
                tts_submit = gr.Button("Generate", variant="primary")
                tts_output1 = gr.Textbox(label="Output Message")
                tts_output2 = gr.Audio(label="Output Audio")
                tts_submit.click(openai, [tts_input1, tts_input2],
                                    [tts_output1, tts_output2])

    app.queue(max_size=10)
    app.launch(share=True)
    # _, audio = microsoft(all_example,microsoft_model_list[0])
    # breakpoint()
    # print(audio)
    # with open("test99.mp3", "wb") as f:
    #     f.write(audio.content)