2023-04-11 12:30:36 +08:00
|
|
|
|
import gradio as gr
|
|
|
|
|
|
import os
|
|
|
|
|
|
import shutil
|
2023-04-14 00:42:21 +08:00
|
|
|
|
from chains.local_doc_qa import LocalDocQA
|
|
|
|
|
|
from configs.model_config import *
|
2023-04-11 12:30:36 +08:00
|
|
|
|
|
2023-04-15 20:01:36 +08:00
|
|
|
|
# return top-k text chunk from vector store
|
|
|
|
|
|
VECTOR_SEARCH_TOP_K = 6
|
|
|
|
|
|
|
|
|
|
|
|
# LLM input history length
|
|
|
|
|
|
LLM_HISTORY_LEN = 3
|
2023-04-11 12:30:36 +08:00
|
|
|
|
|
|
|
|
|
|
def get_file_list():
|
|
|
|
|
|
if not os.path.exists("content"):
|
|
|
|
|
|
return []
|
|
|
|
|
|
return [f for f in os.listdir("content")]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file_list = get_file_list()
|
|
|
|
|
|
|
2023-04-14 00:42:21 +08:00
|
|
|
|
embedding_model_dict_list = list(embedding_model_dict.keys())
|
2023-04-11 12:30:36 +08:00
|
|
|
|
|
2023-04-14 00:42:21 +08:00
|
|
|
|
llm_model_dict_list = list(llm_model_dict.keys())
|
|
|
|
|
|
|
|
|
|
|
|
local_doc_qa = LocalDocQA()
|
2023-04-11 12:30:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def upload_file(file):
|
|
|
|
|
|
if not os.path.exists("content"):
|
|
|
|
|
|
os.mkdir("content")
|
|
|
|
|
|
filename = os.path.basename(file.name)
|
2023-04-11 19:52:59 +08:00
|
|
|
|
shutil.move(file.name, "content/" + filename)
|
2023-04-11 12:30:36 +08:00
|
|
|
|
# file_list首位插入新上传的文件
|
|
|
|
|
|
file_list.insert(0, filename)
|
|
|
|
|
|
return gr.Dropdown.update(choices=file_list, value=filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-14 00:42:21 +08:00
|
|
|
|
def get_answer(query, vs_path, history):
|
2023-04-14 23:30:37 +08:00
|
|
|
|
if vs_path:
|
|
|
|
|
|
resp, history = local_doc_qa.get_knowledge_based_answer(
|
|
|
|
|
|
query=query, vs_path=vs_path, chat_history=history)
|
|
|
|
|
|
else:
|
|
|
|
|
|
history = history + [[None, "请先加载文件后,再进行提问。"]]
|
2023-04-14 23:42:28 +08:00
|
|
|
|
return history, ""
|
2023-04-11 12:30:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
2023-04-14 22:55:51 +08:00
|
|
|
|
def update_status(history, status):
|
|
|
|
|
|
history = history + [[None, status]]
|
|
|
|
|
|
print(status)
|
|
|
|
|
|
return history
|
2023-04-11 19:52:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
2023-04-14 00:42:21 +08:00
|
|
|
|
def init_model():
|
|
|
|
|
|
try:
|
|
|
|
|
|
local_doc_qa.init_cfg()
|
|
|
|
|
|
return """模型已成功加载,请选择文件后点击"加载文件"按钮"""
|
|
|
|
|
|
except:
|
|
|
|
|
|
return """模型未成功加载,请重新选择后点击"加载模型"按钮"""
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-15 14:43:12 +08:00
|
|
|
|
def reinit_model(llm_model, embedding_model, llm_history_len, use_ptuning_v2, top_k, history):
|
2023-04-14 22:55:51 +08:00
|
|
|
|
try:
|
|
|
|
|
|
local_doc_qa.init_cfg(llm_model=llm_model,
|
|
|
|
|
|
embedding_model=embedding_model,
|
|
|
|
|
|
llm_history_len=llm_history_len,
|
2023-04-15 14:43:12 +08:00
|
|
|
|
use_ptuning_v2=use_ptuning_v2,
|
2023-04-14 22:55:51 +08:00
|
|
|
|
top_k=top_k)
|
2023-04-14 23:30:37 +08:00
|
|
|
|
model_status = """模型已成功重新加载,请选择文件后点击"加载文件"按钮"""
|
2023-04-14 22:55:51 +08:00
|
|
|
|
except:
|
2023-04-14 23:30:37 +08:00
|
|
|
|
model_status = """模型未成功重新加载,请重新选择后点击"加载模型"按钮"""
|
|
|
|
|
|
return history + [[None, model_status]]
|
2023-04-14 22:55:51 +08:00
|
|
|
|
|
2023-04-14 00:42:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
2023-04-14 23:30:37 +08:00
|
|
|
|
def get_vector_store(filepath, history):
|
|
|
|
|
|
if local_doc_qa.llm and local_doc_qa.llm:
|
|
|
|
|
|
vs_path = local_doc_qa.init_knowledge_vector_store(["content/" + filepath])
|
|
|
|
|
|
if vs_path:
|
|
|
|
|
|
file_status = "文件已成功加载,请开始提问"
|
|
|
|
|
|
else:
|
|
|
|
|
|
file_status = "文件未成功加载,请重新上传文件"
|
2023-04-14 22:55:51 +08:00
|
|
|
|
else:
|
2023-04-14 23:30:37 +08:00
|
|
|
|
file_status = "模型未完成加载,请先在加载模型后再导入文件"
|
|
|
|
|
|
vs_path = None
|
|
|
|
|
|
return vs_path, history + [[None, file_status]]
|
2023-04-14 01:06:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
2023-04-14 22:55:51 +08:00
|
|
|
|
block_css = """.importantButton {
|
2023-04-11 12:30:36 +08:00
|
|
|
|
background: linear-gradient(45deg, #7e0570,#5d1c99, #6e00ff) !important;
|
|
|
|
|
|
border: none !important;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.importantButton:hover {
|
|
|
|
|
|
background: linear-gradient(45deg, #ff00e0,#8500ff, #6e00ff) !important;
|
|
|
|
|
|
border: none !important;
|
2023-04-14 22:55:51 +08:00
|
|
|
|
}"""
|
2023-04-11 12:30:36 +08:00
|
|
|
|
|
2023-04-14 22:55:51 +08:00
|
|
|
|
webui_title = """
|
2023-04-11 12:30:36 +08:00
|
|
|
|
# 🎉langchain-ChatGLM WebUI🎉
|
|
|
|
|
|
|
|
|
|
|
|
👍 [https://github.com/imClumsyPanda/langchain-ChatGLM](https://github.com/imClumsyPanda/langchain-ChatGLM)
|
|
|
|
|
|
|
2023-04-14 22:55:51 +08:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
init_message = """欢迎使用 langchain-ChatGLM Web UI,开始提问前,请依次如下 3 个步骤:
|
2023-04-15 14:43:12 +08:00
|
|
|
|
1. 选择语言模型、Embedding 模型及相关参数,如果使用ptuning-v2方式微调过模型,将PrefixEncoder模型放在ptuning-v2文件夹里并勾选相关选项,然后点击"重新加载模型",并等待加载完成提示
|
2023-04-14 00:42:21 +08:00
|
|
|
|
2. 上传或选择已有文件作为本地知识文档输入后点击"重新加载文档",并等待加载完成提示
|
2023-04-14 22:55:51 +08:00
|
|
|
|
3. 输入要提交的问题后,点击回车提交 """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_status = init_model()
|
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks(css=block_css) as demo:
|
2023-04-14 23:30:37 +08:00
|
|
|
|
vs_path, file_status, model_status = gr.State(""), gr.State(""), gr.State(model_status)
|
2023-04-14 22:55:51 +08:00
|
|
|
|
gr.Markdown(webui_title)
|
|
|
|
|
|
with gr.Row():
|
|
|
|
|
|
with gr.Column(scale=2):
|
|
|
|
|
|
chatbot = gr.Chatbot([[None, init_message], [None, model_status.value]],
|
2023-04-11 19:52:59 +08:00
|
|
|
|
elem_id="chat-box",
|
2023-04-14 22:55:51 +08:00
|
|
|
|
show_label=False).style(height=750)
|
2023-04-12 21:09:37 +08:00
|
|
|
|
query = gr.Textbox(show_label=False,
|
2023-04-14 23:30:37 +08:00
|
|
|
|
placeholder="请输入提问内容,按回车进行提交",
|
2023-04-12 21:09:37 +08:00
|
|
|
|
).style(container=False)
|
2023-04-14 00:42:21 +08:00
|
|
|
|
|
2023-04-11 12:30:36 +08:00
|
|
|
|
with gr.Column(scale=1):
|
2023-04-14 00:42:21 +08:00
|
|
|
|
llm_model = gr.Radio(llm_model_dict_list,
|
|
|
|
|
|
label="LLM 模型",
|
2023-04-14 22:55:51 +08:00
|
|
|
|
value=LLM_MODEL,
|
2023-04-14 00:42:21 +08:00
|
|
|
|
interactive=True)
|
|
|
|
|
|
llm_history_len = gr.Slider(0,
|
|
|
|
|
|
10,
|
2023-04-15 20:01:36 +08:00
|
|
|
|
value=LLM_HISTORY_LEN,
|
2023-04-14 00:42:21 +08:00
|
|
|
|
step=1,
|
|
|
|
|
|
label="LLM history len",
|
|
|
|
|
|
interactive=True)
|
2023-04-15 14:43:12 +08:00
|
|
|
|
use_ptuning_v2 = gr.Checkbox(USE_PTUNING_V2,
|
|
|
|
|
|
label="使用p-tuning-v2微调过的模型",
|
|
|
|
|
|
interactive=True)
|
2023-04-14 00:42:21 +08:00
|
|
|
|
embedding_model = gr.Radio(embedding_model_dict_list,
|
|
|
|
|
|
label="Embedding 模型",
|
2023-04-14 22:55:51 +08:00
|
|
|
|
value=EMBEDDING_MODEL,
|
2023-04-14 00:42:21 +08:00
|
|
|
|
interactive=True)
|
|
|
|
|
|
top_k = gr.Slider(1,
|
|
|
|
|
|
20,
|
2023-04-15 20:01:36 +08:00
|
|
|
|
value=VECTOR_SEARCH_TOP_K,
|
2023-04-14 00:42:21 +08:00
|
|
|
|
step=1,
|
|
|
|
|
|
label="向量匹配 top k",
|
|
|
|
|
|
interactive=True)
|
|
|
|
|
|
load_model_button = gr.Button("重新加载模型")
|
|
|
|
|
|
|
|
|
|
|
|
# with gr.Column():
|
|
|
|
|
|
with gr.Tab("select"):
|
|
|
|
|
|
selectFile = gr.Dropdown(file_list,
|
|
|
|
|
|
label="content file",
|
|
|
|
|
|
interactive=True,
|
|
|
|
|
|
value=file_list[0] if len(file_list) > 0 else None)
|
|
|
|
|
|
with gr.Tab("upload"):
|
|
|
|
|
|
file = gr.File(label="content file",
|
|
|
|
|
|
file_types=['.txt', '.md', '.docx', '.pdf']
|
|
|
|
|
|
) # .style(height=100)
|
2023-04-14 23:30:37 +08:00
|
|
|
|
load_file_button = gr.Button("加载文件")
|
2023-04-14 00:42:21 +08:00
|
|
|
|
load_model_button.click(reinit_model,
|
|
|
|
|
|
show_progress=True,
|
2023-04-15 14:43:12 +08:00
|
|
|
|
inputs=[llm_model, embedding_model, llm_history_len, use_ptuning_v2, top_k, chatbot],
|
2023-04-14 23:30:37 +08:00
|
|
|
|
outputs=chatbot
|
|
|
|
|
|
)
|
2023-04-14 00:42:21 +08:00
|
|
|
|
# 将上传的文件保存到content文件夹下,并更新下拉框
|
|
|
|
|
|
file.upload(upload_file,
|
|
|
|
|
|
inputs=file,
|
|
|
|
|
|
outputs=selectFile)
|
2023-04-14 22:55:51 +08:00
|
|
|
|
load_file_button.click(get_vector_store,
|
|
|
|
|
|
show_progress=True,
|
2023-04-14 23:30:37 +08:00
|
|
|
|
inputs=[selectFile, chatbot],
|
|
|
|
|
|
outputs=[vs_path, chatbot],
|
|
|
|
|
|
)
|
2023-04-14 22:55:51 +08:00
|
|
|
|
query.submit(get_answer,
|
|
|
|
|
|
[query, vs_path, chatbot],
|
2023-04-14 23:42:28 +08:00
|
|
|
|
[chatbot, query],
|
2023-04-14 22:55:51 +08:00
|
|
|
|
)
|
2023-04-11 12:30:36 +08:00
|
|
|
|
|
2023-04-15 20:01:36 +08:00
|
|
|
|
demo.queue(concurrency_count=3
|
|
|
|
|
|
).launch(server_name='0.0.0.0',
|
|
|
|
|
|
server_port=7860,
|
|
|
|
|
|
show_api=False,
|
|
|
|
|
|
share=False,
|
|
|
|
|
|
inbrowser=False)
|