File size: 1,375 Bytes
15592c6 |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import gradio as gr
from conversation import create_conversation
qa = create_conversation()
def add_text(history, text):
history = history + [(text, None)]
return history, ""
def bot(history):
res = qa(
{
'question': history[-1][0],
'chat_history': history[:-1]
}
)
history[-1][1] = res['answer']
return history
with gr.Blocks() as demo:
chatbot = gr.Chatbot([], elem_id="chatbot",
label='Document GPT').style(height=750)
with gr.Row():
with gr.Column(scale=0.80):
txt = gr.Textbox(
show_label=False,
placeholder="Enter text and press enter",
).style(container=False)
with gr.Column(scale=0.10):
submit_btn = gr.Button(
'Submit',
variant='primary'
)
with gr.Column(scale=0.10):
clear_btn = gr.Button(
'Clear',
variant='stop'
)
txt.submit(add_text, [chatbot, txt], [chatbot, txt]).then(
bot, chatbot, chatbot
)
submit_btn.click(add_text, [chatbot, txt], [chatbot, txt]).then(
bot, chatbot, chatbot
)
clear_btn.click(lambda: None, None, chatbot, queue=False)
if __name__ == '__main__':
demo.queue(concurrency_count=3)
demo.launch()
|