Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import base64
|
| 3 |
+
import os
|
| 4 |
+
api_key = os.getenv('API_KEY')
|
| 5 |
+
|
| 6 |
+
def predict(input, file_input):
|
| 7 |
+
print("input:", input)
|
| 8 |
+
print("file_input:", file_input.name)
|
| 9 |
+
from gradio_client import Client
|
| 10 |
+
|
| 11 |
+
client = Client(api_key)
|
| 12 |
+
extract_result = client.predict(
|
| 13 |
+
input,
|
| 14 |
+
file_input.name,
|
| 15 |
+
fn_index=1
|
| 16 |
+
)
|
| 17 |
+
if extract_result:
|
| 18 |
+
print(extract_result)
|
| 19 |
+
return extract_result
|
| 20 |
+
else:
|
| 21 |
+
return "Too many user, please wait a monument!"
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def view_pdf(pdf_file):
|
| 25 |
+
with open(pdf_file.name, 'rb') as f:
|
| 26 |
+
pdf_data = f.read()
|
| 27 |
+
# print("pdf_file", pdf_file)
|
| 28 |
+
# pdf_data = pdf_file
|
| 29 |
+
b64_data = base64.b64encode(pdf_data).decode()
|
| 30 |
+
# print("b64_data", b64_data)
|
| 31 |
+
return f"<embed src='data:application/pdf;base64,{b64_data}' type='application/pdf' width='100%' height='700px' />"
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
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?
|
| 35 |
+
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.
|
| 36 |
+
"""]
|
| 37 |
+
|
| 38 |
+
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?
|
| 39 |
+
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.
|
| 40 |
+
"""]
|
| 41 |
+
|
| 42 |
+
examples = [en_1, en_2]
|
| 43 |
+
|
| 44 |
+
with gr.Blocks(title="ChatPaperGPT") as demo:
|
| 45 |
+
gr.Markdown(
|
| 46 |
+
'''<p align="center" width="100%">
|
| 47 |
+
<img src="https://big-cheng.com/img/pdf.png" alt="pdf-logo" width="50"/>
|
| 48 |
+
<p>
|
| 49 |
+
|
| 50 |
+
<h1 align="center"> Paper Extract GPT </h1>
|
| 51 |
+
<p> How to use:
|
| 52 |
+
<br> <strong>#1</strong>: Upload your pdf.
|
| 53 |
+
<br> <strong>#2</strong>: Click the View PDF button to view it.
|
| 54 |
+
<br> <strong>#3</strong>: Enter your extraction prompt in the input box (of course, you can click example to test).
|
| 55 |
+
<br> <strong>#4</strong>: Click Generate to extract, and the extracted information will be displayed in markdown form.
|
| 56 |
+
</p>
|
| 57 |
+
'''
|
| 58 |
+
)
|
| 59 |
+
with gr.Row():
|
| 60 |
+
with gr.Column():
|
| 61 |
+
gr.Markdown('## Upload PDF')
|
| 62 |
+
file_input = gr.File(type="filepath")
|
| 63 |
+
viewer_button = gr.Button("View PDF")
|
| 64 |
+
file_out = gr.HTML()
|
| 65 |
+
with gr.Column():
|
| 66 |
+
with gr.Row():
|
| 67 |
+
model_input = gr.Textbox(lines=7, placeholder='Input prompt about extract information from paper',
|
| 68 |
+
label='Input')
|
| 69 |
+
with gr.Row():
|
| 70 |
+
gen = gr.Button("Generate")
|
| 71 |
+
clr = gr.Button("Clear")
|
| 72 |
+
example = gr.Examples(examples=examples, inputs=model_input)
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
outputs = gr.Markdown(label='Output', show_label=True, value="""| Title | Journal | Year | Author | Institution | Email |
|
| 76 |
+
|---------------------------------------------|--------------------|------|-----------------------------------------------|-------------------------------------------------------|-----------------------|
|
| 77 |
+
| 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 | "" |
|
| 78 |
+
""")
|
| 79 |
+
|
| 80 |
+
inputs = [model_input, file_input]
|
| 81 |
+
gen.click(fn=predict, inputs=inputs, outputs=outputs)
|
| 82 |
+
clr.click(fn=lambda value: [gr.update(value=""), gr.update(value="")], inputs=clr,
|
| 83 |
+
outputs=[model_input, outputs])
|
| 84 |
+
|
| 85 |
+
viewer_button.click(view_pdf, inputs=file_input, outputs=file_out)
|
| 86 |
+
# parser_button.click(extract_text, inputs=file_input, outputs=[xml_out, md_out, rich_md_out])
|
| 87 |
+
|
| 88 |
+
demo.launch()
|