Spaces:
Running
Running
Crawford.Zhou
commited on
Commit
•
4b5c1de
1
Parent(s):
72681b1
添加gpt问答接口
Browse files
app.py
CHANGED
@@ -1,9 +1,49 @@
|
|
1 |
import sys, os
|
2 |
-
|
3 |
if sys.platform == "darwin":
|
4 |
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
|
5 |
|
6 |
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
logging.getLogger("numba").setLevel(logging.WARNING)
|
9 |
logging.getLogger("markdown_it").setLevel(logging.WARNING)
|
@@ -51,9 +91,10 @@ def get_text(text, language_str, hps):
|
|
51 |
|
52 |
return bert, phone, tone, language
|
53 |
|
54 |
-
def infer(text, sdp_ratio, noise_scale, noise_scale_w, length_scale, sid):
|
55 |
global net_g
|
56 |
-
|
|
|
57 |
with torch.no_grad():
|
58 |
x_tst=phones.to(device).unsqueeze(0)
|
59 |
tones=tones.to(device).unsqueeze(0)
|
@@ -67,9 +108,9 @@ def infer(text, sdp_ratio, noise_scale, noise_scale_w, length_scale, sid):
|
|
67 |
del x_tst, tones, lang_ids, bert, x_tst_lengths, speakers
|
68 |
return audio
|
69 |
|
70 |
-
def tts_fn(text, speaker, sdp_ratio, noise_scale, noise_scale_w, length_scale):
|
71 |
with torch.no_grad():
|
72 |
-
audio = infer(text, sdp_ratio=sdp_ratio, noise_scale=noise_scale, noise_scale_w=noise_scale_w, length_scale=length_scale, sid=speaker)
|
73 |
return "Success", (hps.data.sampling_rate, audio)
|
74 |
|
75 |
|
@@ -114,42 +155,30 @@ if __name__ == "__main__":
|
|
114 |
with gr.Column():
|
115 |
gr.Markdown(value="""
|
116 |
【AI嘉然①】在线语音合成(Bert-Vits2)\n
|
117 |
-
|
|
|
118 |
声音归属:嘉然今天吃什么 https://space.bilibili.com/672328094\n
|
119 |
Bert-VITS2项目:https://github.com/Stardust-minus/Bert-VITS2\n
|
120 |
-
|
121 |
-
|
122 |
使用本模型请严格遵守法律法规!\n
|
123 |
发布二创作品请标注本项目作者及链接、作品使用Bert-VITS2 AI生成!\n
|
124 |
""")
|
125 |
text = gr.TextArea(label="Text", placeholder="Input Text Here",
|
126 |
value="大家好我是嘉然戴安娜,关注嘉然,顿顿解馋,谢谢!")
|
|
|
|
|
127 |
speaker = gr.Dropdown(choices=speakers, value=speakers[0], label='Speaker')
|
128 |
sdp_ratio = gr.Slider(minimum=0.1, maximum=1, value=0.2, step=0.01, label='SDP/DP混合比')
|
129 |
noise_scale = gr.Slider(minimum=0.1, maximum=1, value=0.5, step=0.01, label='感情调节')
|
130 |
noise_scale_w = gr.Slider(minimum=0.1, maximum=1, value=0.9, step=0.01, label='音素长度')
|
131 |
length_scale = gr.Slider(minimum=0.1, maximum=2, value=1, step=0.01, label='生成长度')
|
132 |
btn = gr.Button("点击生成", variant="primary")
|
133 |
-
with gr.Column():
|
134 |
-
text_output = gr.Textbox(label="Message")
|
135 |
-
audio_output = gr.Audio(label="Output Audio")
|
136 |
-
gr.Markdown(value="""
|
137 |
-
【AI塔菲】https://huggingface.co/spaces/XzJosh/Taffy-Bert-VITS2\n
|
138 |
-
【AI东雪莲】https://huggingface.co/spaces/XzJosh/Azuma-Bert-VITS2\n
|
139 |
-
【AI奶绿】https://huggingface.co/spaces/XzJosh/LAPLACE-Bert-VITS2\n
|
140 |
-
【AI尼奈】https://huggingface.co/spaces/XzJosh/nine1-Bert-VITS2\n
|
141 |
-
【AI珈乐】https://huggingface.co/spaces/XzJosh/Carol-Bert-VITS2\n
|
142 |
-
【AI电棍】https://huggingface.co/spaces/XzJosh/otto-Bert-VITS2\n
|
143 |
-
【AI七海】https://huggingface.co/spaces/XzJosh/Nana7mi-Bert-VITS2\n
|
144 |
-
【AI阿梓】https://huggingface.co/spaces/XzJosh/Azusa-Bert-VITS2\n
|
145 |
-
【AI星瞳】https://huggingface.co/spaces/XzJosh/XingTong-Bert-VITS2\n
|
146 |
-
【AI向晚】https://huggingface.co/spaces/XzJosh/Ava-Bert-VITS2\n
|
147 |
-
""")
|
148 |
btn.click(tts_fn,
|
149 |
-
inputs=[text, speaker, sdp_ratio, noise_scale, noise_scale_w, length_scale],
|
150 |
outputs=[text_output, audio_output])
|
151 |
-
|
152 |
# webbrowser.open("http://127.0.0.1:6006")
|
153 |
# app.launch(server_port=6006, show_error=True)
|
154 |
-
|
155 |
app.launch(show_error=True)
|
|
|
1 |
import sys, os
|
|
|
2 |
if sys.platform == "darwin":
|
3 |
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
|
4 |
|
5 |
import logging
|
6 |
+
import openai
|
7 |
+
|
8 |
+
# openai.log = "debug"
|
9 |
+
openai.api_key = "sk-"
|
10 |
+
openai.api_base = "https://api.chatanywhere.com.cn/v1"
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
# 非流式响应
|
15 |
+
# completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world!"}])
|
16 |
+
# print(completion.choices[0].message.content)
|
17 |
+
|
18 |
+
def gpt_35_api_stream(key, messages: list):
|
19 |
+
openai.api_key = "sk-" + key
|
20 |
+
"""为提供的对话消息创建新的回答 (流式传输)
|
21 |
+
|
22 |
+
Args:
|
23 |
+
messages (list): 完整的对话消息
|
24 |
+
api_key (str): OpenAI API 密钥
|
25 |
+
|
26 |
+
Returns:
|
27 |
+
tuple: (results, error_desc)
|
28 |
+
"""
|
29 |
+
try:
|
30 |
+
response = openai.ChatCompletion.create(
|
31 |
+
model='gpt-3.5-turbo',
|
32 |
+
messages=messages,
|
33 |
+
stream=True,
|
34 |
+
)
|
35 |
+
completion = {'role': '', 'content': ''}
|
36 |
+
for event in response:
|
37 |
+
if event['choices'][0]['finish_reason'] == 'stop':
|
38 |
+
print(f'收到的完成数据: {completion}')
|
39 |
+
break
|
40 |
+
for delta_k, delta_v in event['choices'][0]['delta'].items():
|
41 |
+
print(f'流响应数据: {delta_k} = {delta_v}')
|
42 |
+
completion[delta_k] += delta_v
|
43 |
+
messages.append(completion) # 直接在传入参数 messages 中追加消息
|
44 |
+
return True, ''
|
45 |
+
except Exception as err:
|
46 |
+
return False, f'OpenAI API 异常: {err}'
|
47 |
|
48 |
logging.getLogger("numba").setLevel(logging.WARNING)
|
49 |
logging.getLogger("markdown_it").setLevel(logging.WARNING)
|
|
|
91 |
|
92 |
return bert, phone, tone, language
|
93 |
|
94 |
+
def infer(text, key, sdp_ratio, noise_scale, noise_scale_w, length_scale, sid):
|
95 |
global net_g
|
96 |
+
message = gpt_35_api_stream(text)
|
97 |
+
bert, phones, tones, lang_ids = get_text(message, "ZH", hps)
|
98 |
with torch.no_grad():
|
99 |
x_tst=phones.to(device).unsqueeze(0)
|
100 |
tones=tones.to(device).unsqueeze(0)
|
|
|
108 |
del x_tst, tones, lang_ids, bert, x_tst_lengths, speakers
|
109 |
return audio
|
110 |
|
111 |
+
def tts_fn(text, key, speaker, sdp_ratio, noise_scale, noise_scale_w, length_scale):
|
112 |
with torch.no_grad():
|
113 |
+
audio = infer(text, key, sdp_ratio=sdp_ratio, noise_scale=noise_scale, noise_scale_w=noise_scale_w, length_scale=length_scale, sid=speaker)
|
114 |
return "Success", (hps.data.sampling_rate, audio)
|
115 |
|
116 |
|
|
|
155 |
with gr.Column():
|
156 |
gr.Markdown(value="""
|
157 |
【AI嘉然①】在线语音合成(Bert-Vits2)\n
|
158 |
+
音声作者:Xz乔希 https://space.bilibili.com/5859321\n
|
159 |
+
集成作者:碎语碎念 https://space.bilibili.com/4269384\n
|
160 |
声音归属:嘉然今天吃什么 https://space.bilibili.com/672328094\n
|
161 |
Bert-VITS2项目:https://github.com/Stardust-minus/Bert-VITS2\n
|
162 |
+
GPT_API_free项目:https://github.com/chatanywhere/GPT_API_free\n
|
163 |
+
本项目中的apiKey可以免费从https://github.com/chatanywhere/GPT_API_free获取,参考项目文档即可!\n
|
164 |
使用本模型请严格遵守法律法规!\n
|
165 |
发布二创作品请标注本项目作者及链接、作品使用Bert-VITS2 AI生成!\n
|
166 |
""")
|
167 |
text = gr.TextArea(label="Text", placeholder="Input Text Here",
|
168 |
value="大家好我是嘉然戴安娜,关注嘉然,顿顿解馋,谢谢!")
|
169 |
+
text = gr.TextArea(label="Key", placeholder="请输入上面提示中获取的gpt key",
|
170 |
+
value="key")
|
171 |
speaker = gr.Dropdown(choices=speakers, value=speakers[0], label='Speaker')
|
172 |
sdp_ratio = gr.Slider(minimum=0.1, maximum=1, value=0.2, step=0.01, label='SDP/DP混合比')
|
173 |
noise_scale = gr.Slider(minimum=0.1, maximum=1, value=0.5, step=0.01, label='感情调节')
|
174 |
noise_scale_w = gr.Slider(minimum=0.1, maximum=1, value=0.9, step=0.01, label='音素长度')
|
175 |
length_scale = gr.Slider(minimum=0.1, maximum=2, value=1, step=0.01, label='生成长度')
|
176 |
btn = gr.Button("点击生成", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
btn.click(tts_fn,
|
178 |
+
inputs=[text, key, speaker, sdp_ratio, noise_scale, noise_scale_w, length_scale],
|
179 |
outputs=[text_output, audio_output])
|
180 |
+
|
181 |
# webbrowser.open("http://127.0.0.1:6006")
|
182 |
# app.launch(server_port=6006, show_error=True)
|
183 |
+
|
184 |
app.launch(show_error=True)
|