Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,7 @@ def doc_index_self(open_ai_key, doc): # 文档向量化
|
|
| 35 |
emb_list.append(get_text_emb(open_ai_key, text)) # 获取向量
|
| 36 |
return texts, emb_list, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
|
| 37 |
value="""操作说明 step 3:建立索引(by self)成功! 🙋 可以开始对话啦~"""), gr.Chatbot.update(visible=True), 1, gr.Audio.update(
|
| 38 |
-
visible=True)
|
| 39 |
|
| 40 |
|
| 41 |
def doc_index_llama(open_ai_key, txt): # 建立索引
|
|
@@ -60,7 +60,7 @@ def doc_index_llama(open_ai_key, txt): # 建立索引
|
|
| 60 |
query_engine = index.as_query_engine(text_qa_template=qa_template) # 建立查询引擎
|
| 61 |
return query_engine, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
|
| 62 |
value="""操作说明 step 3:建立索引(by llama_index)成功! 🙋 可以开始对话啦~"""), gr.Chatbot.update(
|
| 63 |
-
visible=True), 0, gr.Audio.update(visible=True)
|
| 64 |
|
| 65 |
|
| 66 |
def get_response_by_self(open_ai_key, msg, bot, doc_text_list, doc_embeddings): # 获取机器人回复
|
|
@@ -188,7 +188,7 @@ def up_file(files): # 上传文件
|
|
| 188 |
value="操作说明 step 2:确认PDF解析结果(可修正),点击“建立索引”,随后进行对话")
|
| 189 |
|
| 190 |
|
| 191 |
-
def
|
| 192 |
output = asr_pipe(
|
| 193 |
filepath,
|
| 194 |
max_new_tokens=256,
|
|
@@ -204,6 +204,20 @@ def transcribe_speech(filepath):
|
|
| 204 |
return simplified_text
|
| 205 |
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
with gr.Blocks() as demo:
|
| 208 |
with gr.Row():
|
| 209 |
with gr.Column():
|
|
@@ -221,6 +235,7 @@ with gr.Blocks() as demo:
|
|
| 221 |
with gr.Column():
|
| 222 |
md = gr.Markdown("""操作说明 step 1:点击左侧区域,上传PDF,进行解析""") # 操作说明
|
| 223 |
chat_bot = gr.Chatbot(visible=False) # 聊天机器人
|
|
|
|
| 224 |
audio_inputs = gr.Audio(source="microphone", type="filepath", label="点击录音输入", visible=False) # 录音输入
|
| 225 |
msg_txt = gr.Textbox(label='消息框', placeholder='输入消息', visible=False) # 消息框
|
| 226 |
chat_bu = gr.Button(value='发送', visible=False) # 发送按钮
|
|
@@ -228,10 +243,10 @@ with gr.Blocks() as demo:
|
|
| 228 |
file.change(up_file, [file], [txt, index_self_bu, index_llama_bu, md]) # 上传文件
|
| 229 |
index_self_bu.click(doc_index_self, [open_ai_key, txt],
|
| 230 |
[doc_text_state, doc_emb_state, msg_txt, chat_bu, md, chat_bot, index_type,
|
| 231 |
-
audio_inputs]) # 提交解析结果
|
| 232 |
index_llama_bu.click(doc_index_llama, [open_ai_key, txt],
|
| 233 |
-
[query_engine, msg_txt, chat_bu, md, chat_bot, index_type, audio_inputs]) # 提交解析结果
|
| 234 |
-
audio_inputs.change(transcribe_speech, [audio_inputs], [msg_txt]) # 录音输入
|
| 235 |
chat_bu.click(get_response,
|
| 236 |
[open_ai_key, msg_txt, chat_bot, doc_text_state, doc_emb_state, query_engine, index_type],
|
| 237 |
[chat_bot]) # 发送消息
|
|
|
|
| 35 |
emb_list.append(get_text_emb(open_ai_key, text)) # 获取向量
|
| 36 |
return texts, emb_list, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
|
| 37 |
value="""操作说明 step 3:建立索引(by self)成功! 🙋 可以开始对话啦~"""), gr.Chatbot.update(visible=True), 1, gr.Audio.update(
|
| 38 |
+
visible=True), gr.Radio.update(visible=True)
|
| 39 |
|
| 40 |
|
| 41 |
def doc_index_llama(open_ai_key, txt): # 建立索引
|
|
|
|
| 60 |
query_engine = index.as_query_engine(text_qa_template=qa_template) # 建立查询引擎
|
| 61 |
return query_engine, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
|
| 62 |
value="""操作说明 step 3:建立索引(by llama_index)成功! 🙋 可以开始对话啦~"""), gr.Chatbot.update(
|
| 63 |
+
visible=True), 0, gr.Audio.update(visible=True), gr.Radio.update(visible=True)
|
| 64 |
|
| 65 |
|
| 66 |
def get_response_by_self(open_ai_key, msg, bot, doc_text_list, doc_embeddings): # 获取机器人回复
|
|
|
|
| 188 |
value="操作说明 step 2:确认PDF解析结果(可修正),点击“建立索引”,随后进行对话")
|
| 189 |
|
| 190 |
|
| 191 |
+
def transcribe_speech_by_self(filepath):
|
| 192 |
output = asr_pipe(
|
| 193 |
filepath,
|
| 194 |
max_new_tokens=256,
|
|
|
|
| 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:
|
| 222 |
with gr.Row():
|
| 223 |
with gr.Column():
|
|
|
|
| 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', options=['self', 'openai'], label='语音识别方式', visible=False) # 语音识别方式
|
| 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) # 发送按钮
|
|
|
|
| 243 |
file.change(up_file, [file], [txt, index_self_bu, index_llama_bu, md]) # 上传文件
|
| 244 |
index_self_bu.click(doc_index_self, [open_ai_key, txt],
|
| 245 |
[doc_text_state, doc_emb_state, msg_txt, chat_bu, md, chat_bot, index_type,
|
| 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]) # 发送消息
|