ChatPDF-GUI / app.py
epochs-demos's picture
Update app.py
825073b
import gradio as gr
import base64
from gpt_reader.pdf_reader import PaperReader
from gpt_reader.prompt import BASE_POINTS
class GUI:
def __init__(self):
self.api_key = ""
self.session = ""
def analyse(self, api_key, pdf_file):
self.session = PaperReader(api_key, points_to_focus=BASE_POINTS)
return self.session.read_pdf_and_summarize(pdf_file)
def ask_question(self, question):
if self.session == "":
return "Please upload PDF file first!"
return self.session.question(question)
with open("./logo.png", "rb") as f:
image_data = f.read()
image_base64 = base64.b64encode(image_data).decode("utf-8")
title = f"""
<h2 style="background-image: linear-gradient(to right, #3A5FCD, #87CEFA); -webkit-background-clip: text;
-webkit-text-fill-color: transparent; text-align: center;">
ChatPDF-GUI
</h2>
"""
description = f"""
<div style="display: flex; align-items: center; justify-content: center; flex-direction: column;">
<p style="font-size: 18px; color: #4AAAFF; text-align: center;">
Upload your pdf and let our model read it to you!"
</p>
<div style="display: flex; align-items: center; margin-bottom: 0px;">
<img src='data:image/jpeg;base64,{image_base64}' width='50' height='30' style="margin-right: 5px;"/>
<p style="font-size: 14px; color: #555;">
Disclaimer: The purpose of this application is solely for demonstration. 1001epochs does not claim ownership for the results. Contact: [email protected] for full solution.
</p>
</div>
</div>
"""
with gr.Blocks() as demo:
gr.HTML(title)
gr.HTML(description)
with gr.Tab("Upload PDF File"):
pdf_input = gr.File(label="PDF File")
api_input = gr.Textbox(label="OpenAI API Key")
result = gr.Textbox(label="PDF Summary")
upload_button = gr.Button("Start Analyse")
with gr.Tab("Ask question about your PDF"):
question_input = gr.Textbox(label="Your Question", placeholder="Authors of this paper?")
answer = gr.Textbox(label="Answer")
ask_button = gr.Button("Ask")
with gr.Accordion("About this project"):
gr.Markdown(
"""Simply upload your document and let our model read it within seconds!""")
app = GUI()
upload_button.click(fn=app.analyse, inputs=[api_input, pdf_input], outputs=result)
ask_button.click(app.ask_question, inputs=question_input, outputs=answer)
if __name__ == "__main__":
demo.title = "CHATGPT-PAPER-READER"
demo.launch() # add "share=True" to share CHATGPT-PAPER-READER app on Internet.