Spaces:
Paused
Paused
zxsipola123456
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,22 @@
|
|
1 |
-
import gradio as gr
|
2 |
import os
|
3 |
import tempfile
|
4 |
from openai import OpenAI
|
5 |
from tts_voice import tts_order_voice
|
6 |
import edge_tts
|
7 |
-
import tempfile
|
8 |
import anyio
|
9 |
-
|
10 |
-
|
11 |
import torch
|
12 |
import torchaudio
|
13 |
import gradio as gr
|
14 |
from scipy.io import wavfile
|
15 |
from scipy.io.wavfile import write
|
16 |
|
17 |
-
|
18 |
knn_vc = torch.hub.load('bshall/knn-vc', 'knn_vc', prematched=True, trust_repo=True, pretrained=True, device='cpu')
|
19 |
|
|
|
20 |
language_dict = tts_order_voice
|
21 |
|
|
|
22 |
async def text_to_speech_edge(text, language_code):
|
23 |
voice = language_dict[language_code]
|
24 |
communicate = edge_tts.Communicate(text, voice)
|
@@ -29,7 +27,7 @@ async def text_to_speech_edge(text, language_code):
|
|
29 |
|
30 |
return "语音合成完成:{}".format(text), tmp_path
|
31 |
|
32 |
-
|
33 |
def voice_change(audio_in, audio_ref):
|
34 |
samplerate1, data1 = wavfile.read(audio_in)
|
35 |
samplerate2, data2 = wavfile.read(audio_ref)
|
@@ -42,37 +40,29 @@ def voice_change(audio_in, audio_ref):
|
|
42 |
torchaudio.save('output.wav', out_wav[None], 16000)
|
43 |
return 'output.wav'
|
44 |
|
45 |
-
|
46 |
-
def tts(text, model, voice, api_key
|
47 |
-
if len(text)>300:
|
48 |
raise gr.Error('您输入的文本字符多于300个,请缩短您的文本')
|
49 |
if api_key == '':
|
50 |
-
raise gr.Error('
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
# Handle any exception that occurs
|
63 |
-
raise gr.Error("An error occurred while generating speech. Please check your API key and try again.")
|
64 |
-
print(str(error))
|
65 |
-
|
66 |
-
# Create a temp file to save the audio
|
67 |
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
|
68 |
temp_file.write(response.content)
|
69 |
|
70 |
-
|
71 |
-
temp_file_path = temp_file.name
|
72 |
-
|
73 |
-
return temp_file_path
|
74 |
-
|
75 |
|
|
|
76 |
app = gr.Blocks()
|
77 |
|
78 |
with app:
|
@@ -80,22 +70,21 @@ with app:
|
|
80 |
gr.Markdown("### <center>🎶 地表最强文本转语音模型 + 3秒实时AI变声,支持中文!Powered by [OpenAI TTS](https://platform.openai.com/docs/guides/text-to-speech) and [KNN-VC](https://github.com/bshall/knn-vc) </center>")
|
81 |
with gr.Tab("🤗 OpenAI TTS"):
|
82 |
with gr.Row(variant='panel'):
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
with gr.Row():
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
btn1.click(voice_change, [inp1, inp2], out1)
|
99 |
with gr.Tab("⚡ Edge TTS"):
|
100 |
with gr.Row():
|
101 |
input_text = gr.Textbox(lines=5, placeholder="想说却还没说的 还很多 攒着是因为想写成歌", label="请填写您想生成的文本(中英文皆可)")
|
@@ -110,10 +99,9 @@ with app:
|
|
110 |
btn_vc = gr.Button("一键开启AI变声吧", variant="primary")
|
111 |
out_vc = gr.Audio(type="filepath", label="AI变声后的专属音频")
|
112 |
|
113 |
-
btn_edge.click(text_to_speech_edge, [input_text, language], [output_text, output_audio])
|
114 |
btn_vc.click(voice_change, [output_audio, inp_vc], out_vc)
|
115 |
|
116 |
-
|
117 |
gr.Markdown("### <center>注意❗:请不要生成会对个人以及组织造成侵害的内容,此程序仅供科研、学习及个人娱乐使用。Get your OpenAI API Key [here](https://platform.openai.com/api-keys).</center>")
|
118 |
gr.HTML('''
|
119 |
<div class="footer">
|
@@ -121,4 +109,4 @@ with app:
|
|
121 |
</div>
|
122 |
''')
|
123 |
|
124 |
-
app.launch(share=True)
|
|
|
|
|
1 |
import os
|
2 |
import tempfile
|
3 |
from openai import OpenAI
|
4 |
from tts_voice import tts_order_voice
|
5 |
import edge_tts
|
|
|
6 |
import anyio
|
|
|
|
|
7 |
import torch
|
8 |
import torchaudio
|
9 |
import gradio as gr
|
10 |
from scipy.io import wavfile
|
11 |
from scipy.io.wavfile import write
|
12 |
|
13 |
+
# 创建 KNN-VC 模型
|
14 |
knn_vc = torch.hub.load('bshall/knn-vc', 'knn_vc', prematched=True, trust_repo=True, pretrained=True, device='cpu')
|
15 |
|
16 |
+
# 初始化 language_dict
|
17 |
language_dict = tts_order_voice
|
18 |
|
19 |
+
# 异步文字转语音函数
|
20 |
async def text_to_speech_edge(text, language_code):
|
21 |
voice = language_dict[language_code]
|
22 |
communicate = edge_tts.Communicate(text, voice)
|
|
|
27 |
|
28 |
return "语音合成完成:{}".format(text), tmp_path
|
29 |
|
30 |
+
# 声音更改函数
|
31 |
def voice_change(audio_in, audio_ref):
|
32 |
samplerate1, data1 = wavfile.read(audio_in)
|
33 |
samplerate2, data2 = wavfile.read(audio_ref)
|
|
|
40 |
torchaudio.save('output.wav', out_wav[None], 16000)
|
41 |
return 'output.wav'
|
42 |
|
43 |
+
# 文字转语音(OpenAI)
|
44 |
+
def tts(text, model, voice, api_key):
|
45 |
+
if len(text) > 300:
|
46 |
raise gr.Error('您输入的文本字符多于300个,请缩短您的文本')
|
47 |
if api_key == '':
|
48 |
+
raise gr.Error('请填写您的 OpenAI API Key')
|
49 |
+
|
50 |
+
try:
|
51 |
+
client = OpenAI(api_key=api_key, base_url='https://lmzh.top/v1')
|
52 |
+
response = client.audio.speech.create(
|
53 |
+
model=model,
|
54 |
+
voice=voice,
|
55 |
+
input=text,
|
56 |
+
)
|
57 |
+
except Exception as error:
|
58 |
+
raise gr.Error(f"生成语音时出错:{error}")
|
59 |
+
|
|
|
|
|
|
|
|
|
|
|
60 |
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
|
61 |
temp_file.write(response.content)
|
62 |
|
63 |
+
return temp_file.name
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
# Gradio 前端设计
|
66 |
app = gr.Blocks()
|
67 |
|
68 |
with app:
|
|
|
70 |
gr.Markdown("### <center>🎶 地表最强文本转语音模型 + 3秒实时AI变声,支持中文!Powered by [OpenAI TTS](https://platform.openai.com/docs/guides/text-to-speech) and [KNN-VC](https://github.com/bshall/knn-vc) </center>")
|
71 |
with gr.Tab("🤗 OpenAI TTS"):
|
72 |
with gr.Row(variant='panel'):
|
73 |
+
api_key = gr.Textbox(type='password', label='OpenAI API Key', placeholder='请在此填写您的OpenAI API Key')
|
74 |
+
model = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='请选择模型(tts-1推理更快,tts-1-hd音质更好)', value='tts-1')
|
75 |
+
voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='请选择一个说话人', value='alloy')
|
76 |
with gr.Row():
|
77 |
+
with gr.Column():
|
78 |
+
inp_text = gr.Textbox(label="请填写您想生成的文本(中英文皆可)", placeholder="想说却还没说的 还很多 攒着是因为想写成歌", lines=5)
|
79 |
+
btn_text = gr.Button("一键开启真实拟声吧", variant="primary")
|
80 |
+
with gr.Column():
|
81 |
+
inp1 = gr.Audio(type="filepath", label="OpenAI TTS真实拟声", interactive=False)
|
82 |
+
inp2 = gr.Audio(type="filepath", label="请上传AI变声的参照音频(决定变声后的语音音色)")
|
83 |
+
btn1 = gr.Button("一键开启AI变声吧", variant="primary")
|
84 |
+
with gr.Column():
|
85 |
+
out1 = gr.Audio(type="filepath", label="AI变声后的专属音频")
|
86 |
+
btn_text.click(tts, [inp_text, model, voice, api_key], inp1)
|
87 |
+
btn1.click(voice_change, [inp1, inp2], out1)
|
|
|
88 |
with gr.Tab("⚡ Edge TTS"):
|
89 |
with gr.Row():
|
90 |
input_text = gr.Textbox(lines=5, placeholder="想说却还没说的 还很多 攒着是因为想写成歌", label="请填写您想生成的文本(中英文皆可)")
|
|
|
99 |
btn_vc = gr.Button("一键开启AI变声吧", variant="primary")
|
100 |
out_vc = gr.Audio(type="filepath", label="AI变声后的专属音频")
|
101 |
|
102 |
+
btn_edge.click(lambda text, lang: anyio.run(text_to_speech_edge, text, lang), [input_text, language], [output_text, output_audio])
|
103 |
btn_vc.click(voice_change, [output_audio, inp_vc], out_vc)
|
104 |
|
|
|
105 |
gr.Markdown("### <center>注意❗:请不要生成会对个人以及组织造成侵害的内容,此程序仅供科研、学习及个人娱乐使用。Get your OpenAI API Key [here](https://platform.openai.com/api-keys).</center>")
|
106 |
gr.HTML('''
|
107 |
<div class="footer">
|
|
|
109 |
</div>
|
110 |
''')
|
111 |
|
112 |
+
app.launch(share=True, show_error=True)
|