Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,94 @@
|
|
1 |
import gradio as
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
demo.launch()
|
|
|
1 |
import gradio as
|
2 |
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
5 |
+
from pdf2image import convert_from_path
|
6 |
+
import base64
|
7 |
+
import io
|
8 |
+
from PIL import Image
|
9 |
|
10 |
+
# Load the OCR model and processor from Hugging Face
|
11 |
+
processor = AutoProcessor.from_pretrained("allenai/olmOCR-7B-0225-preview")
|
12 |
+
model = AutoModelForCausalLM.from_pretrained("allenai/olmOCR-7B-0225-preview")
|
13 |
+
|
14 |
+
def process_pdf(pdf_file):
|
15 |
+
"""
|
16 |
+
Process the uploaded PDF file, extract text from each page, and generate HTML
|
17 |
+
to display each page's image and text with copy buttons.
|
18 |
+
"""
|
19 |
+
# Check if a PDF file was uploaded
|
20 |
+
if pdf_file is None:
|
21 |
+
|
22 |
+
return "<p>Please upload a PDF file.</p>"
|
23 |
+
|
24 |
+
# Convert PDF to images
|
25 |
+
try:
|
26 |
+
|
27 |
+
pages = convert_from_path(pdf_file.name)
|
28 |
+
except Exception as e:
|
29 |
+
return f"<p>Error converting PDF to images: {str(e)}</p>"
|
30 |
+
|
31 |
+
# Start building the HTML output
|
32 |
+
html = '<div><button onclick="copyAll()" style="margin-bottom: 10px;">Copy All</button></div><div id="pages">'
|
33 |
+
|
34 |
+
# Process each page
|
35 |
+
for i, page in enumerate(pages):
|
36 |
+
# Convert the page image to base64 for embedding in HTML
|
37 |
+
buffered = io.BytesIO()
|
38 |
+
page.save(buffered, format="PNG")
|
39 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
40 |
+
img_data = f"data:image/png;base64,{img_str}"
|
41 |
+
|
42 |
+
# Extract text from the page using the OCR model
|
43 |
+
try:
|
44 |
+
inputs = processor(text="Extract the text from this image.", images=page, return_tensors="pt")
|
45 |
+
outputs = model.generate(**inputs)
|
46 |
+
text = processor.decode(outputs[0], skip_special_tokens=True)
|
47 |
+
except Exception as e:
|
48 |
+
text = f"Error extracting text: {str(e)}"
|
49 |
+
|
50 |
+
# Generate HTML for this page's section
|
51 |
+
textarea_id = f"text{i+1}"
|
52 |
+
html += f'''
|
53 |
+
<div class="page" style="margin-bottom: 20px; border-bottom: 1px solid #ccc; padding-bottom: 20px;">
|
54 |
+
<h3>Page {i+1}</h3>
|
55 |
+
<div style="display: flex; align-items: flex-start;">
|
56 |
+
<img src="{img_data}" alt="Page {i+1}" style="max-width: 300px; margin-right: 20px;">
|
57 |
+
<div style="flex-grow: 1;">
|
58 |
+
<textarea id="{textarea_id}" rows="10" style="width: 100%;">{text}</textarea>
|
59 |
+
<button onclick="copyText('{textarea_id}')" style="margin-top: 5px;">Copy</button>
|
60 |
+
</div>
|
61 |
+
</div>
|
62 |
+
</div>
|
63 |
+
'''
|
64 |
+
|
65 |
+
# Close the pages div and add JavaScript for copy functionality
|
66 |
+
html += '</div>'
|
67 |
+
html += '''
|
68 |
+
<script>
|
69 |
+
function copyText(id) {
|
70 |
+
var text = document.getElementById(id);
|
71 |
+
text.select();
|
72 |
+
document.execCommand("copy");
|
73 |
+
}
|
74 |
+
function copyAll() {
|
75 |
+
var texts = document.querySelectorAll("#pages textarea");
|
76 |
+
var allText = Array.from(texts).map(t => t.value).join("\\n\\n");
|
77 |
+
navigator.clipboard.writeText(allText);
|
78 |
+
}
|
79 |
+
</script>
|
80 |
+
'''
|
81 |
+
return html
|
82 |
+
|
83 |
+
# Define the Gradio interface
|
84 |
+
with gr.Blocks(title="PDF Text Extractor") as demo:
|
85 |
+
gr.Markdown("# PDF Text Extractor")
|
86 |
+
gr.Markdown("Upload a PDF file and click 'Extract Text' to see each page's image and extracted text.")
|
87 |
+
with gr.Row():
|
88 |
+
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
89 |
+
submit_btn = gr.Button("Extract Text")
|
90 |
+
output_html = gr.HTML()
|
91 |
+
submit_btn.click(fn=process_pdf, inputs=pdf_input, outputs=output_html)
|
92 |
+
|
93 |
+
# Launch the interface
|
94 |
demo.launch()
|