Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoProcessor,
|
3 |
from pdf2image import convert_from_path
|
4 |
import base64
|
5 |
import io
|
6 |
-
import spaces
|
7 |
from PIL import Image
|
8 |
|
9 |
# Load the OCR model and processor from Hugging Face
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
@spaces.GPU
|
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 |
return "<p>Please upload a PDF file.</p>"
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
3 |
from pdf2image import convert_from_path
|
4 |
import base64
|
5 |
import io
|
|
|
6 |
from PIL import Image
|
7 |
|
8 |
# Load the OCR model and processor from Hugging Face
|
9 |
+
try:
|
10 |
+
processor = AutoProcessor.from_pretrained("allenai/olmOCR-7B-0225-preview")
|
11 |
+
model = AutoModelForVision2Seq.from_pretrained("allenai/olmOCR-7B-0225-preview")
|
12 |
+
except ImportError as e:
|
13 |
+
processor = None
|
14 |
+
model = None
|
15 |
+
print(f"Error loading model: {str(e)}. Please ensure PyTorch is installed.")
|
16 |
+
except ValueError as e:
|
17 |
+
processor = None
|
18 |
+
model = None
|
19 |
+
print(f"Error with model configuration: {str(e)}")
|
20 |
|
|
|
21 |
def process_pdf(pdf_file):
|
22 |
"""
|
23 |
Process the uploaded PDF file, extract text from each page, and generate HTML
|
24 |
to display each page's image and text with copy buttons.
|
25 |
"""
|
26 |
+
if processor is None or model is None:
|
27 |
+
return "<p>Error: Model could not be loaded. Check environment setup (PyTorch may be missing) or model compatibility.</p>"
|
28 |
+
|
29 |
# Check if a PDF file was uploaded
|
30 |
if pdf_file is None:
|
31 |
return "<p>Please upload a PDF file.</p>"
|