Spaces:
Sleeping
Sleeping
import gradio as gr | |
import openai | |
# from gpt_reader.pdf_reader import PaperReader | |
# from gpt_reader.prompt import BASE_POINTS | |
from Document_QA import QA | |
from Document_QA import create_embeddings | |
from Document_QA import Paper | |
from PyPDF2 import PdfReader | |
class GUI: | |
def __init__(self): | |
self.api_key = "" | |
self.session = "" | |
self.all_embedding =None | |
self.tokens = 0 | |
#load pdf and create all embedings | |
def pdf_init(self, api_key, pdf_path): | |
openai.api_key = api_key | |
pdf_reader = PdfReader(pdf_path) | |
paper = Paper(pdf_reader) | |
all_texts = paper.get_texts() | |
self.all_embedding, self.tokens = create_embeddings(all_texts) | |
print("ε ¨ι¨ζζ¬ζΆθ {} tokens".format(self.tokens)) | |
def get_answer(self, question): | |
qa = QA(self.all_embedding) | |
answer,context = qa(question) | |
return answer.strip() | |
with gr.Blocks() as demo: | |
gr.Markdown( | |
""" | |
# CHATGPT-PAPER-READER | |
""") | |
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") | |
app = GUI() | |
upload_button.click(fn=app.pdf_init, inputs=[api_input, pdf_input]) | |
ask_button.click(app.get_answer, 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. | |