Spaces:
Runtime error
Runtime error
import json | |
import time | |
import random | |
import gradio as gr | |
import pandas as pd | |
from utils.gpt_processor import QuestionAnswerer | |
qa_processor = QuestionAnswerer() | |
current_file = None | |
context = None | |
with open("final_result.json", 'r', encoding='UTF-8') as fp: | |
db = json.load(fp) | |
def read_examples(): | |
df = pd.read_csv(r'examples.csv') | |
return [f"{keyword}" for keyword in df['word'].tolist()] | |
def user(message, history): | |
#return gr.update(value="", interactive=False), history + [[message, None]] | |
return "", history + [[message, None]] | |
def bot(history): | |
user_message = history[-1][0] | |
global current_file | |
global context | |
#check if user input has "我想了解" | |
if "我想了解" in user_message: | |
# get keyword from "「」" | |
keyword = user_message.split("「")[1].split("」")[0] | |
# check if keyword is in db | |
file_list = [] | |
for key in db.keys(): | |
if keyword in db[key]['keywords']: | |
file_list.append(key) | |
if len(file_list) == 0: | |
response = [ | |
[user_message, "Sorry, I can't find any documents about this topic. Please try again."], | |
] | |
else: | |
bot_message = "以下是我所找到的文件:" | |
for file in file_list: | |
bot_message += "\n" + file | |
bot_message += "\n\n" + "請複製貼上想要了解的文件,我會給你該文件的摘要" | |
response = [ | |
[user_message, bot_message], | |
] | |
history = response | |
# history[-1][1] = "" | |
# for character in bot_message: | |
# history[-1][1] += character | |
# time.sleep(random.uniform(0.01, 0.05)) | |
# yield history | |
return history | |
# check if user input has a pdf file name | |
if ".pdf" in user_message or ".docx" in user_message: | |
current_file = user_message | |
context = db[current_file]['file_full_content'] | |
# check if file name is in db | |
if user_message in db.keys(): | |
bot_message = f"文件 {user_message} 的摘要如下:" | |
bot_message += "\n\n" + db[user_message]['summarized_content'] | |
bot_message += "\n\n" + "可以透過詢問來了解更多這個文件的內容" | |
response = [ | |
[user_message, bot_message], | |
] | |
else: | |
response = [ | |
[user_message, "Sorry, I can't find this file. Please try again."], | |
] | |
history[-1] = response[0] | |
# history[-1][1] = "" | |
# for character in bot_message: | |
# history[-1][1] += character | |
# time.sleep(random.uniform(0.01, 0.05)) | |
# yield history | |
return history | |
if context is None: | |
response = [ | |
[user_message, "請輸入一個文件名稱或是點選下方的範例"], | |
] | |
history[-1] = response[0] | |
return history | |
if context is not None: | |
bot_message = qa_processor.answer_question(context, user_message) | |
response = [ | |
[user_message, bot_message], | |
] | |
history[-1] = response[0] | |
return history | |
with gr.Blocks() as demo: | |
history = gr.State([]) | |
user_question = gr.State("") | |
with gr.Row(): | |
gr.HTML('Junyi Academy Chatbot') | |
#status_display = gr.Markdown("Success", elem_id="status_display") | |
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): | |
# submit_btn = gr.Button("Send") | |
with gr.Column(min_width=70, scale=1): | |
clear_btn = gr.Button("Clear") | |
response = user_input.submit(user, | |
[user_input, chatbot], | |
[user_input, chatbot], | |
queue=False, | |
).then(bot, chatbot, chatbot) | |
response.then(lambda: gr.update(interactive=True), None, [user_input], queue=False) | |
clear_btn.click(lambda: None, None, chatbot, queue=False) | |
examples = gr.Examples(examples=read_examples(), | |
inputs=[user_input]) | |
if __name__ == "__main__": | |
demo.launch() | |