File size: 2,111 Bytes
b4a95f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
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

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
        with open(pdf_path,'r',encoding='utf-8') as f:
            texts = f.readlines()
            #按照行对文章进行切割
            texts = [text.strip() for text in texts if text.strip()]
            self.all_embedding,self.tokens = create_embeddings(texts)
    def get_answer(self, question):
        qa = QA(self.all_embedding)
        answer,context = qa(question)
        return answer.strip()

    # 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 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.