import gradio as gr import base64 import os api_key = os.getenv('API_KEY') def predict(input, file_input): print("input:", input) print("file_input:", file_input.name) from gradio_client import Client client = Client(api_key) extract_result = client.predict( input, file_input.name, fn_index=1 ) if extract_result: print(extract_result) return extract_result else: return "Too many user, please wait a monument!" def view_pdf(pdf_file): with open(pdf_file.name, 'rb') as f: pdf_data = f.read() # print("pdf_file", pdf_file) # pdf_data = pdf_file b64_data = base64.b64encode(pdf_data).decode() # print("b64_data", b64_data) return f"" en_1 = ["""could you please help me extract the information of 'title'/'journal'/'year'/'author'/'institution'/'email' from the previous content in a markdown table format? If any of this information was not available in the paper, please replaced it with the string `""`, If the property contains multiple entities, please use a list to contain. """] en_2 = ["""could you please help me extract the information of 'title'/'journal'/'year'/'author'/'institution'/'email' from the previous content in a json format? If any of this information was not available in the paper, please replaced it with the string `""`, If the property contains multiple entities, please use a list to contain. """] examples = [en_1, en_2] with gr.Blocks(title="ChatPaperGPT") as demo: gr.Markdown( '''

pdf-logo

Paper Extract GPT

How to use:
#1: Upload your pdf.
#2: Click the View PDF button to view it.
#3: Enter your extraction prompt in the input box (of course, you can click example to test).
#4: Click Generate to extract, and the extracted information will be displayed in markdown form.

''' ) with gr.Row(): with gr.Column(): gr.Markdown('## Upload PDF') file_input = gr.File(type="filepath") viewer_button = gr.Button("View PDF") file_out = gr.HTML() with gr.Column(): with gr.Row(): model_input = gr.Textbox(lines=7, placeholder='Input prompt about extract information from paper', label='Input') with gr.Row(): gen = gr.Button("Generate") clr = gr.Button("Clear") example = gr.Examples(examples=examples, inputs=model_input) with gr.Row(): outputs = gr.Markdown(label='Output', show_label=True, value="""| Title | Journal | Year | Author | Institution | Email | |---------------------------------------------|--------------------|------|-----------------------------------------------|-------------------------------------------------------|-----------------------| | Paleomagnetic Study of Deccan Traps from Jabalpur to Amarkantak, Central India | J. Geomag. Geoelectr. | 1973 | R. K. VERMA, G. PULLAIAH, G.R. ANJANEYULU, P. K. MALLIK | National Geophysical Research Institute, Hyderabad, and Indian School o f Mines, Dhanbad | "" | """) inputs = [model_input, file_input] gen.click(fn=predict, inputs=inputs, outputs=outputs) clr.click(fn=lambda value: [gr.update(value=""), gr.update(value="")], inputs=clr, outputs=[model_input, outputs]) viewer_button.click(view_pdf, inputs=file_input, outputs=file_out) # parser_button.click(extract_text, inputs=file_input, outputs=[xml_out, md_out, rich_md_out]) demo.launch()