zxsipola123456 commited on
Commit
6cac856
·
verified ·
1 Parent(s): 72c126a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -14
app.py CHANGED
@@ -70,6 +70,22 @@ def voice_change(audio_in, audio_ref):
70
  return output_path
71
 
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  # 文字转语音(OpenAI)
74
  def tts(text, model, voice, api_key):
75
  if len(text) > 300:
@@ -94,14 +110,7 @@ def tts(text, model, voice, api_key):
94
 
95
  return temp_file_path
96
 
97
- def validate_api_key(api_key):
98
- try:
99
- client = OpenAI(api_key=api_key, base_url='https://lmzh.top/v1')
100
- # 测试调用一个简单的API来验证Key
101
- response = client.models.list()
102
- return True
103
- except Exception:
104
- return False
105
 
106
  def tts1(text, model, voice, api_key):
107
  if len(text)>300:
@@ -133,10 +142,7 @@ def tts1(text, model, voice, api_key):
133
  return temp_file_path
134
 
135
 
136
- # 更新Edge TTS标签页状态的函数
137
- def update_edge_tts_tab(api_key):
138
- is_valid = validate_api_key(api_key)
139
- return gr.update(interactive=is_valid)
140
 
141
  # Gradio 前端设计
142
  app = gr.Blocks()
@@ -146,7 +152,7 @@ with app:
146
  gr.Markdown("### <center>key获取地址[here](https://buy.sipola.cn),ai文案生成(可使用中转key和官方key)请访问 [here](https://ai.sipola.cn)</center>")
147
  with gr.Tab("中转key-TTS文本生语音"):
148
  with gr.Row(variant='panel'):
149
- api_key = gr.Textbox(type='password', label='API Key', placeholder='请在此填写您在https://buy.sipola.cn获取的中转API Key')
150
  model = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='请选择模型(tts-1推理更快,tts-1-hd音质更好)', value='tts-1')
151
  voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='请选择一个说话人', value='alloy')
152
  with gr.Row():
@@ -181,6 +187,7 @@ with app:
181
  out_vc = gr.Audio(type="filepath", label="AI变声后的专属音频")
182
  btn_edge.click(lambda text, lang: anyio.run(text_to_speech_edge, text, lang), [input_text, language], [output_text, output_audio])
183
  btn_vc.click(voice_change, [output_audio, inp_vc], out_vc)
 
184
  with gr.Tab("官方key-TTS文本生语音"):
185
  with gr.Row(variant='panel'):
186
  api_key = gr.Textbox(type='password', label='API Key', placeholder='请在此填写您在https://buy.sipola.cn 获取的官方API Key')
@@ -199,15 +206,17 @@ with app:
199
  btn_text.click(tts1, [inp_text, model, voice, api_key], inp1)
200
  btn1.click(voice_change, [inp1, inp2], out1)
201
  # 监听API Key输入框的变化并更新Edge TTS标签页的状态
202
- api_key.change(
203
  update_edge_tts_tab,
204
  inputs=[api_key],
205
  outputs=[edge_tts_tab]
206
  )
 
207
  gr.HTML('''
208
  <div class="footer">
209
  <center><p>Power by sipola </p></center>
210
  </div>
211
  ''')
212
 
 
213
  app.launch(show_error=True)
 
70
  return output_path
71
 
72
 
73
+ #验证中转api key是否有效
74
+ def validate_api_key(api_key):
75
+ try:
76
+ client = OpenAI(api_key=api_key, base_url='https://lmzh.top/v1')
77
+ # 测试调用一个简单的API来验证Key
78
+ response = client.models.list()
79
+ return True
80
+ except Exception:
81
+ return False
82
+
83
+ # 更新Edge TTS标签页状态的函数
84
+ def update_edge_tts_tab(api_key):
85
+ is_valid = validate_api_key(api_key)
86
+ return gr.update(interactive=is_valid)
87
+
88
+
89
  # 文字转语音(OpenAI)
90
  def tts(text, model, voice, api_key):
91
  if len(text) > 300:
 
110
 
111
  return temp_file_path
112
 
113
+
 
 
 
 
 
 
 
114
 
115
  def tts1(text, model, voice, api_key):
116
  if len(text)>300:
 
142
  return temp_file_path
143
 
144
 
145
+
 
 
 
146
 
147
  # Gradio 前端设计
148
  app = gr.Blocks()
 
152
  gr.Markdown("### <center>key获取地址[here](https://buy.sipola.cn),ai文案生成(可使用中转key和官方key)请访问 [here](https://ai.sipola.cn)</center>")
153
  with gr.Tab("中转key-TTS文本生语音"):
154
  with gr.Row(variant='panel'):
155
+ api_proxy_key = gr.Textbox(type='password', label='API Key', placeholder='请在此填写您在https://buy.sipola.cn获取的中转API Key')
156
  model = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='请选择模型(tts-1推理更快,tts-1-hd音质更好)', value='tts-1')
157
  voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='请选择一个说话人', value='alloy')
158
  with gr.Row():
 
187
  out_vc = gr.Audio(type="filepath", label="AI变声后的专属音频")
188
  btn_edge.click(lambda text, lang: anyio.run(text_to_speech_edge, text, lang), [input_text, language], [output_text, output_audio])
189
  btn_vc.click(voice_change, [output_audio, inp_vc], out_vc)
190
+
191
  with gr.Tab("官方key-TTS文本生语音"):
192
  with gr.Row(variant='panel'):
193
  api_key = gr.Textbox(type='password', label='API Key', placeholder='请在此填写您在https://buy.sipola.cn 获取的官方API Key')
 
206
  btn_text.click(tts1, [inp_text, model, voice, api_key], inp1)
207
  btn1.click(voice_change, [inp1, inp2], out1)
208
  # 监听API Key输入框的变化并更新Edge TTS标签页的状态
209
+ api_proxy_key.change(
210
  update_edge_tts_tab,
211
  inputs=[api_key],
212
  outputs=[edge_tts_tab]
213
  )
214
+
215
  gr.HTML('''
216
  <div class="footer">
217
  <center><p>Power by sipola </p></center>
218
  </div>
219
  ''')
220
 
221
+
222
  app.launch(show_error=True)