ChenyuRabbitLove's picture
refactor/ enable mutiple usage
b95388b
raw
history blame
4.03 kB
import json
import time
import random
import os
import openai
import gradio as gr
import pandas as pd
import numpy as np
from openai.embeddings_utils import distances_from_embeddings
from utils.gpt_processor import QuestionAnswerer
from utils.work_flow_controller import WorkFlowController
from utils.chatbot import Chatbot
from utils.utils import *
def create_chatbot():
bot = Chatbot()
return bot
with gr.Blocks() as demo:
history = gr.State([])
user_question = gr.State("")
chatbot_utils = Chatbot()
user_chatbot = gr.State(Chatbot())
upload_state = gr.State("wating")
finished = gr.State("finished")
with gr.Row():
gr.HTML('Junyi Academy Chatbot')
with gr.Row(equal_height=True):
with gr.Column(scale=5):
with gr.Row():
chatbot = gr.Chatbot()
with gr.Row():
with gr.Column(scale=12):
user_input = gr.Textbox(
show_label=False,
placeholder="Enter text",
container=False,
)
with gr.Column(min_width=70, scale=1):
clear_btn = gr.Button("清除")
with gr.Column(min_width=70, scale=1):
submit_btn = gr.Button("傳送")
bot_args = dict(
fn=bot,
inputs=user_chatbot,
outputs=chatbot,
)
user_args = dict(
fn=user,
inputs=[user_chatbot, user_input],
outputs=[user_input, chatbot],
queue=False,
)
response = user_input.submit(**user_args).then(**bot_args)
response.then(lambda: gr.update(interactive=True), None, [user_input], queue=False)
submit_btn.click(user,
[user_input, chatbot],
[user_input, chatbot],
chatbot,
queue=False).then(**bot_args).then(lambda: gr.update(interactive=True), None, [user_input], queue=False)
with gr.Row():
index_file = gr.File(file_count="multiple", file_types=["pdf"], label="Upload PDF file")
with gr.Row():
instruction = gr.Markdown("""
## 使用說明
1. 上傳一個或多個 PDF 檔案,系統將自動進行摘要、翻譯等處理後建立知識庫
2. 在上方輸入欄輸入問題,系統將自動回覆
3. 可以根據下方的摘要內容來提問
4. 每次對話會根據第一個問題的內容來檢索所有文件,並挑選最能回答問題的文件來回覆
5. 要切換檢索的文件,請點選「清除對話記錄」按鈕後再重新提問
""")
with gr.Row():
describe = gr.Markdown('', visible=True)
clear_state_args = dict(
fn=clear_state,
inputs=user_chatbot,
outputs=None,
)
clear_btn.click(**clear_state_args)
send_system_nofification_args = dict(
fn=send_system_nofification,
inputs=user_chatbot,
outputs=chatbot,
)
bulid_knowledge_base_args = dict(
fn=build_knowledge_base,
inputs=[user_chatbot, index_file],
outputs=None,
)
change_md_args = dict(
fn=change_md,
inputs=[user_chatbot],
outputs=[describe],
)
index_file.upload(**send_system_nofification_args) \
.then(lambda: gr.update(interactive=True), None, None, queue=False) \
.then(**bulid_knowledge_base_args) \
.then(**send_system_nofification_args) \
.then(lambda: gr.update(interactive=True), None, None, queue=False) \
.then(**change_md_args)
if __name__ == "__main__":
demo.launch()