Last active
September 10, 2023 16:32
-
-
Save sukanyabag/25f03eff7ccd35945664921816f6ba51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from controller import Controller | |
import gradio as gr | |
os.environ["TOKENIZERS_PARALLELISM"] = "false" | |
controller = Controller() | |
def process_pdf(file): | |
if file is not None: | |
controller.embed_document(file) | |
return ( | |
gr.update(visible=True), | |
gr.update(visible=True), | |
gr.update(visible=True), | |
gr.update(visible=True), | |
) | |
def respond(message, history): | |
botmessage = controller.retrieve(message) | |
history.append((message, botmessage)) | |
return "", history | |
def clear_everything(): | |
return (None, None, None) | |
with gr.Blocks(css=CSS, title="") as demo: | |
gr.Markdown("# AskPDF ", elem_id="app-title") | |
gr.Markdown("## Upload a PDF and Ask Questions!", elem_id="select-a-file") | |
gr.Markdown( | |
"Drop an interesting PDF and ask questions about it!", | |
elem_id="select-a-file", | |
) | |
with gr.Row(): | |
with gr.Column(scale=3): | |
upload = gr.File(label="Upload PDF", type="file") | |
with gr.Row(): | |
clear_button = gr.Button("Clear", variant="secondary") | |
with gr.Column(scale=6): | |
chatbot = gr.Chatbot() | |
with gr.Row().style(equal_height=True): | |
with gr.Column(scale=8): | |
question = gr.Textbox( | |
show_label=False, | |
placeholder="e.g. What is the document about?", | |
lines=1, | |
max_lines=1, | |
).style(container=False) | |
with gr.Column(scale=1, min_width=60): | |
submit_button = gr.Button( | |
"Ask me 🤖", variant="primary", elem_id="submit-button" | |
) | |
upload.change( | |
fn=process_pdf, | |
inputs=[upload], | |
outputs=[ | |
question, | |
clear_button, | |
submit_button, | |
chatbot, | |
], | |
api_name="upload", | |
) | |
question.submit(respond, [question, chatbot], [question, chatbot]) | |
submit_button.click(respond, [question, chatbot], [question, chatbot]) | |
clear_button.click( | |
fn=clear_everything, | |
inputs=[], | |
outputs=[upload, question, chatbot], | |
api_name="clear", | |
) | |
if __name__ == "__main__": | |
demo.launch(enable_queue=False, share=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment