Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -204,18 +204,19 @@ def transcribe_speech_by_self(filepath):
|
|
204 |
return simplified_text
|
205 |
|
206 |
|
207 |
-
def transcribe_speech_by_openai(filepath):
|
|
|
208 |
audio_file = open(filepath, "rb")
|
209 |
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
210 |
print(transcript)
|
211 |
return transcript['text']
|
212 |
|
213 |
|
214 |
-
def transcribe_speech(filepath, a_type):
|
215 |
if a_type == 'self':
|
216 |
return transcribe_speech_by_self(filepath)
|
217 |
else:
|
218 |
-
return transcribe_speech_by_openai(filepath)
|
219 |
|
220 |
|
221 |
with gr.Blocks() as demo:
|
@@ -235,7 +236,7 @@ with gr.Blocks() as demo:
|
|
235 |
with gr.Column():
|
236 |
md = gr.Markdown("""操作说明 step 1:点击左侧区域,上传PDF,进行解析""") # 操作说明
|
237 |
chat_bot = gr.Chatbot(visible=False) # 聊天机器人
|
238 |
-
asr_type = gr.Radio(value='self',
|
239 |
audio_inputs = gr.Audio(source="microphone", type="filepath", label="点击录音输入", visible=False) # 录音输入
|
240 |
msg_txt = gr.Textbox(label='消息框', placeholder='输入消息', visible=False) # 消息框
|
241 |
chat_bu = gr.Button(value='发送', visible=False) # 发送按钮
|
@@ -246,7 +247,7 @@ with gr.Blocks() as demo:
|
|
246 |
audio_inputs, asr_type]) # 提交解析结果
|
247 |
index_llama_bu.click(doc_index_llama, [open_ai_key, txt],
|
248 |
[query_engine, msg_txt, chat_bu, md, chat_bot, index_type, audio_inputs, asr_type]) # 提交解析结果
|
249 |
-
audio_inputs.change(transcribe_speech, [audio_inputs, asr_type], [msg_txt]) # 录音输入
|
250 |
chat_bu.click(get_response,
|
251 |
[open_ai_key, msg_txt, chat_bot, doc_text_state, doc_emb_state, query_engine, index_type],
|
252 |
[chat_bot]) # 发送消息
|
|
|
204 |
return simplified_text
|
205 |
|
206 |
|
207 |
+
def transcribe_speech_by_openai(openai_key, filepath):
|
208 |
+
openai.api_key = openai_key # 设置OpenAI API Key
|
209 |
audio_file = open(filepath, "rb")
|
210 |
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
211 |
print(transcript)
|
212 |
return transcript['text']
|
213 |
|
214 |
|
215 |
+
def transcribe_speech(openai_key, filepath, a_type):
|
216 |
if a_type == 'self':
|
217 |
return transcribe_speech_by_self(filepath)
|
218 |
else:
|
219 |
+
return transcribe_speech_by_openai(openai_key, filepath)
|
220 |
|
221 |
|
222 |
with gr.Blocks() as demo:
|
|
|
236 |
with gr.Column():
|
237 |
md = gr.Markdown("""操作说明 step 1:点击左侧区域,上传PDF,进行解析""") # 操作说明
|
238 |
chat_bot = gr.Chatbot(visible=False) # 聊天机器人
|
239 |
+
asr_type = gr.Radio(value='self', choices=['self', 'openai'], label='语音识别方式', visible=False) # 语音识别方式
|
240 |
audio_inputs = gr.Audio(source="microphone", type="filepath", label="点击录音输入", visible=False) # 录音输入
|
241 |
msg_txt = gr.Textbox(label='消息框', placeholder='输入消息', visible=False) # 消息框
|
242 |
chat_bu = gr.Button(value='发送', visible=False) # 发送按钮
|
|
|
247 |
audio_inputs, asr_type]) # 提交解析结果
|
248 |
index_llama_bu.click(doc_index_llama, [open_ai_key, txt],
|
249 |
[query_engine, msg_txt, chat_bu, md, chat_bot, index_type, audio_inputs, asr_type]) # 提交解析结果
|
250 |
+
audio_inputs.change(transcribe_speech, [open_ai_key, audio_inputs, asr_type], [msg_txt]) # 录音输入
|
251 |
chat_bu.click(get_response,
|
252 |
[open_ai_key, msg_txt, chat_bot, doc_text_state, doc_emb_state, query_engine, index_type],
|
253 |
[chat_bot]) # 发送消息
|