import gradio as gr import time from utils import Bot from utils import make_documens, make_descriptions def init_bot(table=None,tittle=None,pdf=None,key=None): if key is None: return 'You must submit OpenAI key' if pdf is None: return 'You must submit pdf file' if table is None: return 'You must submit table file' if tittle is None: return 'You must submit tittle of table' table = table.name pdf = pdf.name table_descriptions = make_descriptions(tittle=tittle,table=table) documents = make_documens(pdf) print(documents[0]) global bot bot = Bot( openai_api_key=key, table_descriptions=table_descriptions, text_documents=documents, verbose=False ) return 'Chat bot successfully initialized' def msg_bot(history): message = history[-1][0] bot_message = bot(message)['output'] history[-1][1] = "" for character in bot_message: history[-1][1] += character time.sleep(0.05) yield history def user(user_message, history): return "", history + [[user_message, None]] with gr.Blocks() as demo: with gr.Tab("Chat bot initialization"): key = gr.Textbox(label='OpenAI key') with gr.Row(variant='panel'): with gr.Column(): with gr.Row(): title = gr.Textbox(label='Table tittle') with gr.Row(): table = gr.File(label='csv table') pdf = gr.File(label='pdf') with gr.Row(variant='panel'): init_button = gr.Button('submit') init_output = gr.Textbox(label="Initialization status") init_button.click(fn=init_bot,inputs=[table,title,pdf,key],outputs=init_output,api_name='init') with gr.Tab("Chat bot"): chatbot = gr.Chatbot() msg = gr.Textbox(label='Ask the bot') clear = gr.Button('Clear') msg.submit(user,[msg,chatbot],[msg,chatbot],queue=False).then( msg_bot, chatbot, chatbot ) clear.click(lambda: None, None, chatbot, queue=False) demo.queue() demo.launch()