Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
os.system('git clone https://github.com/facebookresearch/detectron2.git')
|
3 |
+
os.system('pip install -e detectron2')
|
4 |
+
os.system("git clone https://github.com/microsoft/unilm.git")
|
5 |
+
os.system("sed -i 's/from collections import Iterable/from collections.abc import Iterable/' unilm/dit/object_detection/ditod/table_evaluation/data_structure.py")
|
6 |
+
os.system("curl -LJ -o publaynet_dit-b_cascade.pth 'https://layoutlm.blob.core.windows.net/dit/dit-fts/publaynet_dit-b_cascade.pth?sv=2022-11-02&ss=b&srt=o&sp=r&se=2033-06-08T16:48:15Z&st=2023-06-08T08:48:15Z&spr=https&sig=a9VXrihTzbWyVfaIDlIT1Z0FoR1073VB0RLQUMuudD4%3D'")
|
7 |
+
|
8 |
+
import sys
|
9 |
+
sys.path.append("unilm")
|
10 |
+
sys.path.append("detectron2")
|
11 |
+
|
12 |
+
import uuid # Import the UUID library
|
13 |
+
from pdfextract_fun import *
|
14 |
+
from pdfsummary_fun import *
|
15 |
+
from imagesummary_fun import *
|
16 |
+
# Assuming all your defined functions are above and imported correctly into this script
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
def process_pdf(pdf_file,state):
|
21 |
+
#base_name = os.path.splitext(os.path.basename(pdf_file.name))[0]
|
22 |
+
unique_id = str(uuid.uuid4()) # Generate a unique identifier
|
23 |
+
|
24 |
+
output_folder = os.path.join("processed_files", unique_id) # Use UUID for the output folder name, within a parent directory
|
25 |
+
|
26 |
+
if not os.path.exists(output_folder):
|
27 |
+
os.makedirs(output_folder)
|
28 |
+
|
29 |
+
# Convert the uploaded PDF file to JPG images
|
30 |
+
convert_pdf_to_jpg(pdf_file.name, output_folder)
|
31 |
+
# Process the images to analyze and extract instances, then rename files sequentially and perform OCR
|
32 |
+
process_jpeg_images(output_folder)
|
33 |
+
#process_jpeg_images(output_folder)
|
34 |
+
rename_files_sequentially(output_folder)
|
35 |
+
ocr_folder(output_folder)
|
36 |
+
image_files = [os.path.join(output_folder, f) for f in os.listdir(output_folder)
|
37 |
+
if f.endswith('.jpg') and ('figure' in f or 'table' in f)]
|
38 |
+
|
39 |
+
#images = [Image.open(f) for f in image_files]
|
40 |
+
images = [(Image.open(f), os.path.basename(f).split('.')[0]) for f in image_files]
|
41 |
+
|
42 |
+
# For demonstration, let's just return the path to the output folder
|
43 |
+
# In a real app, you'd want to return images, texts, or links to download the results
|
44 |
+
return images, output_folder
|
45 |
+
|
46 |
+
def call_pdf_summary(state):
|
47 |
+
ocr_results_folder = os.path.join(state, "ocr_results")
|
48 |
+
summary = pdf_summary(ocr_results_folder) # Assuming pdf_summary accepts an output folder argument
|
49 |
+
return summary
|
50 |
+
|
51 |
+
|
52 |
+
def handle_summary_button_click(selected_images):
|
53 |
+
# Check if any image is selected
|
54 |
+
summary = get_image_summary(selected_images)
|
55 |
+
return summary
|
56 |
+
|
57 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as app:
|
58 |
+
gr.Markdown("# ChatPaper!")
|
59 |
+
state = gr.State() # Initialize state
|
60 |
+
|
61 |
+
with gr.Row():
|
62 |
+
file_input = gr.File(type="filepath", label="Upload a PDF")
|
63 |
+
|
64 |
+
with gr.Row():
|
65 |
+
gallery_output = gr.Gallery(label="Extracted Figures and Tables", show_label=True,columns=[3], rows=[1], object_fit="contain", height="auto")
|
66 |
+
with gr.Column():
|
67 |
+
summary_output = gr.Textbox(label="PDF Summary")
|
68 |
+
summary_button = gr.Button("Generate Summary")
|
69 |
+
with gr.Row():
|
70 |
+
# Initialize Dropdown without choices; they will be set dynamically
|
71 |
+
image_input = gr.Image(label="Select an Figure or Table for analysis",type='filepath',show_label=True, height="auto")
|
72 |
+
with gr.Column():
|
73 |
+
image_summary_output = gr.Textbox(label="Figure or Table analysis")
|
74 |
+
image_summary_button = gr.Button("Generate Figure or Table analysis")
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
file_input.change(process_pdf, inputs=[file_input, state], outputs=[gallery_output, state])
|
79 |
+
summary_button.click(call_pdf_summary, inputs=[state], outputs=[summary_output])
|
80 |
+
image_summary_button.click(handle_summary_button_click, inputs=image_input, outputs=image_summary_output)
|
81 |
+
|
82 |
+
# Note: Authentication details removed for security reasons
|
83 |
+
app.launch() # Launch the app with sharing enabled
|