souljoy commited on
Commit
5d931dc
1 Parent(s): 959d09a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -6
app.py CHANGED
@@ -8,11 +8,13 @@ from cnocr import CnOcr
8
  import numpy as np
9
  import openai
10
  from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader, Prompt
 
11
 
12
  ocr = CnOcr() # 初始化ocr模型
13
  history_max_len = 500 # 机器人记忆的最大长度
14
  all_max_len = 2000 # 输入的最大长度
15
-
 
16
 
17
  def get_text_emb(open_ai_key, text): # 文本向量化
18
  openai.api_key = open_ai_key # 设置openai的key
@@ -29,7 +31,8 @@ def doc_index_self(open_ai_key, doc): # 文档向量化
29
  for text in texts: # 遍历每一行
30
  emb_list.append(get_text_emb(open_ai_key, text)) # 获取向量
31
  return texts, emb_list, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
32
- value="""操作说明 step 3:建立索引(by self)成功! 🙋 可以开始对话啦~"""), gr.Chatbot.update(visible=True), 1
 
33
 
34
 
35
  def doc_index_llama(open_ai_key, txt): # 建立索引
@@ -53,7 +56,8 @@ def doc_index_llama(open_ai_key, txt): # 建立索引
53
  qa_template = Prompt(template) # 将模板转换成Prompt对象
54
  query_engine = index.as_query_engine(text_qa_template=qa_template) # 建立查询引擎
55
  return query_engine, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
56
- value="""操作说明 step 3:建立索引(by llama_index)成功! 🙋 可以开始对话啦~"""), gr.Chatbot.update(visible=True), 0
 
57
 
58
 
59
  def get_response_by_self(open_ai_key, msg, bot, doc_text_list, doc_embeddings): # 获取机器人回复
@@ -181,6 +185,20 @@ def up_file(files): # 上传文件
181
  value="操作说明 step 2:确认PDF解析结果(可修正),点击“建立索引”,随后进行对话")
182
 
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  with gr.Blocks() as demo:
185
  with gr.Row():
186
  with gr.Column():
@@ -198,14 +216,17 @@ with gr.Blocks() as demo:
198
  with gr.Column():
199
  md = gr.Markdown("""操作说明 step 1:点击左侧区域,上传PDF,进行解析""") # 操作说明
200
  chat_bot = gr.Chatbot(visible=False) # 聊天机器人
201
- msg_txt = gr.Textbox(label='消息框', placeholder='输入消息,点击发送', visible=False) # 消息框
 
202
  chat_bu = gr.Button(value='发送', visible=False) # 发送按钮
203
 
204
  file.change(up_file, [file], [txt, index_self_bu, index_llama_bu, md]) # 上传文件
205
  index_self_bu.click(doc_index_self, [open_ai_key, txt],
206
- [doc_text_state, doc_emb_state, msg_txt, chat_bu, md, chat_bot, index_type]) # 提交解析结果
 
207
  index_llama_bu.click(doc_index_llama, [open_ai_key, txt],
208
- [query_engine, msg_txt, chat_bu, md, chat_bot, index_type]) # 提交解析结果
 
209
  chat_bu.click(get_response,
210
  [open_ai_key, msg_txt, chat_bot, doc_text_state, doc_emb_state, query_engine, index_type],
211
  [chat_bot]) # 发送消息
 
8
  import numpy as np
9
  import openai
10
  from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader, Prompt
11
+ from transformers import pipeline
12
 
13
  ocr = CnOcr() # 初始化ocr模型
14
  history_max_len = 500 # 机器人记忆的最大长度
15
  all_max_len = 2000 # 输入的最大长度
16
+ asr_model_id = "souljoy/whisper-tiny" # 更新为你的模型ID
17
+ asr_pipe = pipeline("automatic-speech-recognition", model=asr_model_id)
18
 
19
  def get_text_emb(open_ai_key, text): # 文本向量化
20
  openai.api_key = open_ai_key # 设置openai的key
 
31
  for text in texts: # 遍历每一行
32
  emb_list.append(get_text_emb(open_ai_key, text)) # 获取向量
33
  return texts, emb_list, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
34
+ value="""操作说明 step 3:建立索引(by self)成功! 🙋 可以开始对话啦~"""), gr.Chatbot.update(visible=True), 1, gr.Audio.update(
35
+ visible=True)
36
 
37
 
38
  def doc_index_llama(open_ai_key, txt): # 建立索引
 
56
  qa_template = Prompt(template) # 将模板转换成Prompt对象
57
  query_engine = index.as_query_engine(text_qa_template=qa_template) # 建立查询引擎
58
  return query_engine, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
59
+ value="""操作说明 step 3:建立索引(by llama_index)成功! 🙋 可以开始对话啦~"""), gr.Chatbot.update(
60
+ visible=True), 0, gr.Audio.update(visible=True)
61
 
62
 
63
  def get_response_by_self(open_ai_key, msg, bot, doc_text_list, doc_embeddings): # 获取机器人回复
 
185
  value="操作说明 step 2:确认PDF解析结果(可修正),点击“建立索引”,随后进行对话")
186
 
187
 
188
+ def transcribe_speech(filepath):
189
+ output = asr_pipe(
190
+ filepath,
191
+ max_new_tokens=256,
192
+ generate_kwargs={
193
+ "task": "transcribe",
194
+ "language": "chinese",
195
+ }, # 更新为你微调的语言
196
+ chunk_length_s=30,
197
+ batch_size=8,
198
+ )
199
+ return output["text"]
200
+
201
+
202
  with gr.Blocks() as demo:
203
  with gr.Row():
204
  with gr.Column():
 
216
  with gr.Column():
217
  md = gr.Markdown("""操作说明 step 1:点击左侧区域,上传PDF,进行解析""") # 操作说明
218
  chat_bot = gr.Chatbot(visible=False) # 聊天机器人
219
+ audio_inputs = gr.Audio(source="microphone", type="filepath", label="点击录音输入", visible=False) # 录音输入
220
+ msg_txt = gr.Textbox(label='消息框', placeholder='输入消息', visible=False) # 消息框
221
  chat_bu = gr.Button(value='发送', visible=False) # 发送按钮
222
 
223
  file.change(up_file, [file], [txt, index_self_bu, index_llama_bu, md]) # 上传文件
224
  index_self_bu.click(doc_index_self, [open_ai_key, txt],
225
+ [doc_text_state, doc_emb_state, msg_txt, chat_bu, md, chat_bot, index_type,
226
+ audio_inputs]) # 提交解析结果
227
  index_llama_bu.click(doc_index_llama, [open_ai_key, txt],
228
+ [query_engine, msg_txt, chat_bu, md, chat_bot, index_type, audio_inputs]) # 提交解析结果
229
+ audio_inputs.change(transcribe_speech, [audio_inputs], [msg_txt]) # 录音输入
230
  chat_bu.click(get_response,
231
  [open_ai_key, msg_txt, chat_bot, doc_text_state, doc_emb_state, query_engine, index_type],
232
  [chat_bot]) # 发送消息