File size: 1,045 Bytes
af8db98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import gradio as gr
from src.ui import UI
from src.chatbot import ChatBot

def clear_session():
    return "", []

def add_query(chat_history, input):
    if not input:
        raise gr.Error("Please enter a question.")
    chat_history.append((input, None))
    return chat_history

def response(chat_history, query):
    res_msg, ref_docs = chatbot.generate_response(query, chat_history[:-1])
    chat_history[-1] = (query, res_msg)
    return "", chat_history, ref_docs

if __name__ == "__main__":
    demo, chatspace, ref_docs, text_input, clear_btn = UI.create_demo()

    chatbot = ChatBot(is_debug=True)
    with demo:
        # Event handler for submitting text and generating response
        text_input.submit(add_query, inputs=[chatspace, text_input], outputs=[chatspace], concurrency_limit=1).\
            success(response, inputs=[chatspace, text_input], outputs=[text_input, chatspace, ref_docs])
        clear_btn.click(clear_session, inputs=[], outputs=[text_input, chatspace])

    demo.queue(api_open=False)
    demo.launch()