zxsipola123456 commited on
Commit
3572db4
1 Parent(s): 9ab83b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -1
app.py CHANGED
@@ -101,6 +101,35 @@ def validate_api_key(api_key):
101
  except Exception:
102
  return False
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
  # 更新Edge TTS标签页状态的函数
106
  def update_edge_tts_tab(api_key):
@@ -113,7 +142,7 @@ app = gr.Blocks()
113
  with app:
114
  gr.Markdown("# <center>TTS文本生成语音 + AI秒变声+需要使用中转key</center>")
115
  gr.Markdown("### <center>中转key获取地址[here](https://buy.sipola.cn),ai文案生成可使用中转key,请访问 [here](https://ai.sipola.cn)</center>")
116
- with gr.Tab("TTS文本生语音"):
117
  with gr.Row(variant='panel'):
118
  api_key = gr.Textbox(type='password', label='API Key', placeholder='请在此填写您在https://buy.sipola.cn获取的中转API Key')
119
  model = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='请选择模型(tts-1推理更快,tts-1-hd音质更好)', value='tts-1')
@@ -130,6 +159,7 @@ with app:
130
  out1 = gr.Audio(type="filepath", label="AI变声后的专属音频")
131
  btn_text.click(tts, [inp_text, model, voice, api_key], inp1)
132
  btn1.click(voice_change, [inp1, inp2], out1)
 
133
 
134
  with gr.Tab("TTS-AI变声",interactive=False) as edge_tts_tab:
135
  with gr.Row():
@@ -149,6 +179,23 @@ with app:
149
  out_vc = gr.Audio(type="filepath", label="AI变声后的专属音频")
150
  btn_edge.click(lambda text, lang: anyio.run(text_to_speech_edge, text, lang), [input_text, language], [output_text, output_audio])
151
  btn_vc.click(voice_change, [output_audio, inp_vc], out_vc)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  # 监听API Key输入框的变化并更新Edge TTS标签页的状态
153
  api_key.change(
154
  update_edge_tts_tab,
 
101
  except Exception:
102
  return False
103
 
104
+ def tts1(text, model, voice, api_key):
105
+ if len(text)>300:
106
+ raise gr.Error('您输入的文本字符多于300个,请缩短您的文本')
107
+ if api_key == '':
108
+ raise gr.Error('Please enter your OpenAI API Key')
109
+ else:
110
+ try:
111
+ client = OpenAI(api_key=api_key)
112
+
113
+ response = client.audio.speech.create(
114
+ model=model, # "tts-1","tts-1-hd"
115
+ voice=voice, # 'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'
116
+ input=text,
117
+ )
118
+
119
+ except Exception as error:
120
+ # Handle any exception that occurs
121
+ raise gr.Error("An error occurred while generating speech. Please check your API key and try again.")
122
+ print(str(error))
123
+
124
+ # Create a temp file to save the audio
125
+ with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
126
+ temp_file.write(response.content)
127
+
128
+ # Get the file path of the temp file
129
+ temp_file_path = temp_file.name
130
+
131
+ return temp_file_path
132
+
133
 
134
  # 更新Edge TTS标签页状态的函数
135
  def update_edge_tts_tab(api_key):
 
142
  with app:
143
  gr.Markdown("# <center>TTS文本生成语音 + AI秒变声+需要使用中转key</center>")
144
  gr.Markdown("### <center>中转key获取地址[here](https://buy.sipola.cn),ai文案生成可使用中转key,请访问 [here](https://ai.sipola.cn)</center>")
145
+ with gr.Tab("中转key-TTS文本生语音"):
146
  with gr.Row(variant='panel'):
147
  api_key = gr.Textbox(type='password', label='API Key', placeholder='请在此填写您在https://buy.sipola.cn获取的中转API Key')
148
  model = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='请选择模型(tts-1推理更快,tts-1-hd音质更好)', value='tts-1')
 
159
  out1 = gr.Audio(type="filepath", label="AI变声后的专属音频")
160
  btn_text.click(tts, [inp_text, model, voice, api_key], inp1)
161
  btn1.click(voice_change, [inp1, inp2], out1)
162
+
163
 
164
  with gr.Tab("TTS-AI变声",interactive=False) as edge_tts_tab:
165
  with gr.Row():
 
179
  out_vc = gr.Audio(type="filepath", label="AI变声后的专属音频")
180
  btn_edge.click(lambda text, lang: anyio.run(text_to_speech_edge, text, lang), [input_text, language], [output_text, output_audio])
181
  btn_vc.click(voice_change, [output_audio, inp_vc], out_vc)
182
+ with gr.Tab("官方key-TTS文本生语音"):
183
+ with gr.Row(variant='panel'):
184
+ api_key = gr.Textbox(type='password', label='API Key', placeholder='请在此填写您在https://buy.sipola.cn获取的中转API Key')
185
+ model = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='请选择模型(tts-1推理更快,tts-1-hd音质更好)', value='tts-1')
186
+ voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='请选择一个说话人', value='alloy')
187
+ with gr.Row():
188
+ with gr.Column():
189
+ inp_text = gr.Textbox(label="请填写您想生成的文本中英文皆可", placeholder="请输入ai生成的文案,不要超过300字,最好200字左右", lines=5)
190
+ btn_text = gr.Button("一键生成音频", variant="primary")
191
+ with gr.Column():
192
+ inp1 = gr.Audio(type="filepath", label="TTS真实拟声", interactive=False)
193
+ inp2 = gr.Audio(type="filepath", label="请上传同文案参照音频,可自己读取同文案录音")
194
+ btn1 = gr.Button("一键AI变声合成", variant="primary")
195
+ with gr.Column():
196
+ out1 = gr.Audio(type="filepath", label="AI变声后的专属音频")
197
+ btn_text.click(tts1, [inp_text, model, voice, api_key], inp1)
198
+ btn1.click(voice_change, [inp1, inp2], out1)
199
  # 监听API Key输入框的变化并更新Edge TTS标签页的状态
200
  api_key.change(
201
  update_edge_tts_tab,