Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
updated minor warnings
Browse files
app.py
CHANGED
|
@@ -1,12 +1,18 @@
|
|
| 1 |
import os
|
| 2 |
import base64
|
| 3 |
from io import BytesIO
|
|
|
|
| 4 |
|
| 5 |
import torch
|
| 6 |
from PIL import Image
|
| 7 |
import gradio as gr
|
| 8 |
from transformers import AutoProcessor, AutoModelForVision2Seq
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# IMPORTANT: Load processor+model from the olmOCR checkpoint itself
|
| 11 |
MODEL_ID = "allenai/olmOCR-2-7B-1025"
|
| 12 |
|
|
@@ -19,9 +25,6 @@ def load_model():
|
|
| 19 |
if processor is not None and model is not None:
|
| 20 |
return
|
| 21 |
|
| 22 |
-
# Silence the libgomp warning in Spaces
|
| 23 |
-
os.environ["OMP_NUM_THREADS"] = "1"
|
| 24 |
-
|
| 25 |
# trust_remote_code is often required for VLM checkpoints
|
| 26 |
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 27 |
|
|
@@ -95,7 +98,9 @@ def ocr_image(img: Image.Image) -> str:
|
|
| 95 |
padding=True,
|
| 96 |
return_tensors="pt",
|
| 97 |
)
|
| 98 |
-
|
|
|
|
|
|
|
| 99 |
|
| 100 |
with torch.inference_mode():
|
| 101 |
output_ids = model.generate(
|
|
@@ -121,7 +126,7 @@ with gr.Blocks(title="BookReader OCR API (olmOCR2)") as demo:
|
|
| 121 |
with gr.Row():
|
| 122 |
with gr.Column():
|
| 123 |
image_input = gr.Image(type="pil", label="Upload image")
|
| 124 |
-
run_btn = gr.Button("Run OCR")
|
| 125 |
with gr.Column():
|
| 126 |
output = gr.Textbox(label="Extracted text", lines=20)
|
| 127 |
|
|
@@ -132,5 +137,5 @@ with gr.Blocks(title="BookReader OCR API (olmOCR2)") as demo:
|
|
| 132 |
api_name="/ocr",
|
| 133 |
)
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import base64
|
| 3 |
from io import BytesIO
|
| 4 |
+
import warnings
|
| 5 |
|
| 6 |
import torch
|
| 7 |
from PIL import Image
|
| 8 |
import gradio as gr
|
| 9 |
from transformers import AutoProcessor, AutoModelForVision2Seq
|
| 10 |
|
| 11 |
+
# Suppress warnings at startup
|
| 12 |
+
os.environ["OMP_NUM_THREADS"] = "1"
|
| 13 |
+
os.environ["TRANSFORMERS_VERBOSITY"] = "error"
|
| 14 |
+
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 15 |
+
|
| 16 |
# IMPORTANT: Load processor+model from the olmOCR checkpoint itself
|
| 17 |
MODEL_ID = "allenai/olmOCR-2-7B-1025"
|
| 18 |
|
|
|
|
| 25 |
if processor is not None and model is not None:
|
| 26 |
return
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
# trust_remote_code is often required for VLM checkpoints
|
| 29 |
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 30 |
|
|
|
|
| 98 |
padding=True,
|
| 99 |
return_tensors="pt",
|
| 100 |
)
|
| 101 |
+
|
| 102 |
+
# FIX: Move inputs to model's device (eliminates the warning)
|
| 103 |
+
inputs = {k: v.to(model.device) if torch.is_tensor(v) else v for k, v in inputs.items()}
|
| 104 |
|
| 105 |
with torch.inference_mode():
|
| 106 |
output_ids = model.generate(
|
|
|
|
| 126 |
with gr.Row():
|
| 127 |
with gr.Column():
|
| 128 |
image_input = gr.Image(type="pil", label="Upload image")
|
| 129 |
+
run_btn = gr.Button("Run OCR", variant="primary")
|
| 130 |
with gr.Column():
|
| 131 |
output = gr.Textbox(label="Extracted text", lines=20)
|
| 132 |
|
|
|
|
| 137 |
api_name="/ocr",
|
| 138 |
)
|
| 139 |
|
| 140 |
+
if __name__ == "__main__":
|
| 141 |
+
demo.queue().launch()
|