Spaces:
Sleeping
Sleeping
import gradio as gr | |
from ChatErector import conversation, initializer | |
def ui(): | |
with gr.Blocks(theme=gr.themes.Default(primary_hue="red", secondary_hue="pink", neutral_hue = "purple")) as ui: | |
qa_chain = gr.State() | |
gr.HTML("<center><h1>Ask your Manuscript</h1><center>") | |
gr.Markdown("""<center><b>Simple Chatbot demo with RAG</b><center>""") | |
gr.Markdown("""Upload your documents to initilize conversation system. | |
It could take some time to preprocess documents if there are many of them. """) | |
with gr.Row(): | |
with gr.Column(scale = 86): | |
gr.Markdown("""<b>Important: The demo only works with pdf. It must be initialized by creating database.</b>""") | |
with gr.Row(): | |
document = gr.Files(height=300, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload PDF documents") | |
with gr.Row(): | |
with gr.Accordion("Advanced settings", open=False): | |
with gr.Row(): | |
slider_temperature = gr.Slider(minimum = 0.01, maximum = 1.0, value=0.2, step=0.1, label="Temperature", info="Controls randomness in token generation (not recommended to set up higher than 0.5)", interactive=True) | |
with gr.Row(): | |
slider_maxtokens = gr.Slider(minimum = 128, maximum = 9192, value=4096, step=128, label="Max New Tokens", info="Maximum number of tokens to be generated",interactive=True) | |
with gr.Row(): | |
slider_topk = gr.Slider(minimum = 1, maximum = 10, value=3, step=1, label="top-k", info="Number of tokens to select the next token from", interactive=True) | |
with gr.Row(): | |
thold = gr.Slider(minimum = 0.01, maximum = 1.0, value=0.8, step=0.1, label="Treshold", info="Retrieved information relevance level (not recommended to set up higher than 0.8)", interactive=True) | |
with gr.Row(): | |
qachain_btn = gr.Button("Create database") | |
with gr.Row(): | |
llm_progress = gr.Textbox(value="Not initialized", label="Database creating status") # label="Chatbot status", | |
with gr.Column(scale = 200): | |
chatbot = gr.Chatbot(height=505) | |
with gr.Row(): | |
msg = gr.Textbox(placeholder="Ask a question", container=True) | |
gr.Examples([["Chicago"], ["Little Rock"], ["San Francisco"]], msg) | |
with gr.Row(): | |
submit_btn = gr.Button("Submit") | |
clear_btn = gr.ClearButton([msg, chatbot], value="Clear") | |
# Preprocessing events | |
qachain_btn.click(initializer, | |
inputs=[document, slider_temperature, slider_maxtokens, slider_topk, thold], | |
outputs=[qa_chain, llm_progress], | |
queue=False) | |
# Chatbot events | |
msg.submit(conversation, | |
inputs=[qa_chain, msg, chatbot], | |
outputs=[qa_chain, msg, chatbot], | |
queue=False) | |
submit_btn.click(conversation, | |
inputs=[qa_chain, msg, chatbot], | |
outputs=[qa_chain, msg, chatbot], | |
queue=False) | |
clear_btn.click(lambda:[None,"",0,"",0,"",0], | |
inputs=None, | |
outputs=[chatbot], | |
queue=False) | |
ui.queue().launch(debug=True) |