Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -1,91 +1,89 @@
|
|
1 |
-
import subprocess
|
2 |
-
import json
|
3 |
-
import os
|
4 |
-
import gradio as gr
|
5 |
-
from PyPDF2 import PdfReader, PdfWriter
|
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 |
-
return
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
#
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
#
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
# Launch the app with a specified port for Docker
|
91 |
-
app.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
1 |
+
import subprocess
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import gradio as gr
|
5 |
+
from PyPDF2 import PdfReader, PdfWriter
|
6 |
+
|
7 |
+
# Function to split PDF into batches of 3 pages
|
8 |
+
def split_pdf(file_path, batch_size=3):
|
9 |
+
pdf_reader = PdfReader(open(file_path, "rb"))
|
10 |
+
total_pages = len(pdf_reader.pages)
|
11 |
+
pdf_batches = []
|
12 |
+
|
13 |
+
# Split the PDF into batches of 3 pages
|
14 |
+
for i in range(0, total_pages, batch_size):
|
15 |
+
pdf_writer = PdfWriter()
|
16 |
+
for j in range(i, min(i + batch_size, total_pages)):
|
17 |
+
pdf_writer.add_page(pdf_reader.pages[j])
|
18 |
+
|
19 |
+
batch_path = f"./temp_batch_{i // batch_size}.pdf"
|
20 |
+
with open(batch_path, "wb") as batch_file:
|
21 |
+
pdf_writer.write(batch_file)
|
22 |
+
|
23 |
+
pdf_batches.append(batch_path)
|
24 |
+
|
25 |
+
return pdf_batches
|
26 |
+
|
27 |
+
# Function to process the PDF batch using subprocess
|
28 |
+
def process_pdf_batch(batch_path, output_dir):
|
29 |
+
# Extract the base name of the batch file
|
30 |
+
pdf_name = os.path.basename(batch_path).split('.')[0]
|
31 |
+
result_path = os.path.join(output_dir, pdf_name, "results.json")
|
32 |
+
|
33 |
+
# Build the OCR command
|
34 |
+
ocr_command = ["surya_ocr", batch_path, "--results_dir", output_dir]
|
35 |
+
|
36 |
+
# Run the command using subprocess
|
37 |
+
try:
|
38 |
+
result = subprocess.run(ocr_command, check=True, text=True, capture_output=True,encoding="utf-8")
|
39 |
+
print("OCR Command Output:", result.stdout)
|
40 |
+
except subprocess.CalledProcessError as e:
|
41 |
+
return f"OCR processing failed: {e.stderr}"
|
42 |
+
|
43 |
+
# After OCR processing, read the results from the JSON file
|
44 |
+
if os.path.exists(result_path):
|
45 |
+
with open(result_path, 'r', encoding="utf-8") as f:
|
46 |
+
data = json.load(f)
|
47 |
+
|
48 |
+
# Extract text from the JSON
|
49 |
+
result_text = ''
|
50 |
+
for page_data in data[pdf_name]:
|
51 |
+
for line in page_data['text_lines']:
|
52 |
+
result_text += line['text'] + '\n'
|
53 |
+
|
54 |
+
return result_text
|
55 |
+
else:
|
56 |
+
return "OCR processing completed, but result file not found."
|
57 |
+
|
58 |
+
# Main function to process the entire PDF in batches
|
59 |
+
def process_pdf(file):
|
60 |
+
# Define output directory
|
61 |
+
output_dir = "./result"
|
62 |
+
|
63 |
+
# Split the uploaded PDF into batches of 3 pages
|
64 |
+
pdf_batches = split_pdf(file.name, batch_size=3)
|
65 |
+
|
66 |
+
# Process each batch and accumulate results
|
67 |
+
final_text = ""
|
68 |
+
for batch_path in pdf_batches:
|
69 |
+
batch_result = process_pdf_batch(batch_path, output_dir)
|
70 |
+
final_text += batch_result + "\n"
|
71 |
+
|
72 |
+
return final_text
|
73 |
+
|
74 |
+
# Define Gradio interface
|
75 |
+
def process_pdf_gradio(file):
|
76 |
+
# Gradio handles the file upload differently, so process accordingly
|
77 |
+
result = process_pdf(file)
|
78 |
+
return result
|
79 |
+
|
80 |
+
# Gradio app
|
81 |
+
app = gr.Interface(
|
82 |
+
fn=process_pdf_gradio,
|
83 |
+
inputs=gr.File(label="Upload PDF"),
|
84 |
+
outputs=gr.Textbox(label="Extracted Text"),
|
85 |
+
title="PDF OCR Extractor"
|
86 |
+
)
|
87 |
+
|
88 |
+
# Launch the app with a specified port for Docker
|
89 |
+
app.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|