Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,10 @@ from transformers import AutoProcessor, AutoModelForVision2Seq
|
|
| 8 |
from PIL import Image
|
| 9 |
import gradio as gr
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Normalization boundary for ScreenVLM
|
| 12 |
NORM_SIZE = 500
|
| 13 |
MODEL_ID = "docling-project/ScreenVLM"
|
|
@@ -19,6 +23,7 @@ class ScreenVLMEngine:
|
|
| 19 |
|
| 20 |
# --- THE CORRECT PROCESSOR OVERRIDE (MATCHED EXACTLY) ---
|
| 21 |
if hasattr(self.processor, "image_processor"):
|
|
|
|
| 22 |
self.processor.image_processor.max_image_size = {"longest_edge": 99999}
|
| 23 |
if hasattr(self.processor.image_processor, "size"):
|
| 24 |
self.processor.image_processor.size = {"longest_edge": 512}
|
|
@@ -44,7 +49,7 @@ class ScreenVLMEngine:
|
|
| 44 |
model = AutoModelForVision2Seq.from_pretrained(
|
| 45 |
MODEL_ID,
|
| 46 |
torch_dtype=torch.float32,
|
| 47 |
-
attn_implementation="eager",
|
| 48 |
low_cpu_mem_usage=True
|
| 49 |
)
|
| 50 |
|
|
@@ -54,6 +59,7 @@ class ScreenVLMEngine:
|
|
| 54 |
)
|
| 55 |
|
| 56 |
# Free unquantized weights from RAM immediately
|
|
|
|
| 57 |
del model
|
| 58 |
gc.collect()
|
| 59 |
print("Model is AWAKE and ready in RAM.")
|
|
@@ -83,6 +89,7 @@ class ScreenVLMEngine:
|
|
| 83 |
return elements
|
| 84 |
|
| 85 |
def analyze(self, image: Image.Image):
|
|
|
|
| 86 |
orig_width, orig_height = image.size
|
| 87 |
|
| 88 |
# Keep our CPU-friendly manual resizing, but scale to 512 max edge
|
|
@@ -106,7 +113,7 @@ class ScreenVLMEngine:
|
|
| 106 |
generated_ids = self.model.generate(
|
| 107 |
**inputs,
|
| 108 |
max_new_tokens=1024,
|
| 109 |
-
use_cache=True,
|
| 110 |
eos_token_id=100257,
|
| 111 |
pad_token_id=100257
|
| 112 |
)
|
|
@@ -127,7 +134,6 @@ engine = ScreenVLMEngine()
|
|
| 127 |
def gradio_interface_fn(image):
|
| 128 |
try:
|
| 129 |
elements = engine.analyze(image)
|
| 130 |
-
# FIX: Serialize to string to prevent Pydantic/Gradio 4 validation clashes
|
| 131 |
return json.dumps({"status": "success", "elements": elements})
|
| 132 |
except Exception as e:
|
| 133 |
return json.dumps({"status": "error", "message": str(e)})
|
|
@@ -136,10 +142,10 @@ def gradio_interface_fn(image):
|
|
| 136 |
demo = gr.Interface(
|
| 137 |
fn=gradio_interface_fn,
|
| 138 |
inputs=gr.Image(type="pil", label="Upload Screenshot"),
|
| 139 |
-
outputs="text",
|
| 140 |
title="ScreenVLM Production Parser"
|
| 141 |
)
|
| 142 |
|
| 143 |
if __name__ == "__main__":
|
| 144 |
-
#
|
| 145 |
-
demo.queue().launch(
|
|
|
|
| 8 |
from PIL import Image
|
| 9 |
import gradio as gr
|
| 10 |
|
| 11 |
+
# Force Hugging Face offline globally
|
| 12 |
+
os.environ["HF_HUB_OFFLINE"] = "1"
|
| 13 |
+
os.environ["HF_DATASETS_OFFLINE"] = "1"
|
| 14 |
+
|
| 15 |
# Normalization boundary for ScreenVLM
|
| 16 |
NORM_SIZE = 500
|
| 17 |
MODEL_ID = "docling-project/ScreenVLM"
|
|
|
|
| 23 |
|
| 24 |
# --- THE CORRECT PROCESSOR OVERRIDE (MATCHED EXACTLY) ---
|
| 25 |
if hasattr(self.processor, "image_processor"):
|
| 26 |
+
# Set dictionary structure correctly to prevent subscriptable errors
|
| 27 |
self.processor.image_processor.max_image_size = {"longest_edge": 99999}
|
| 28 |
if hasattr(self.processor.image_processor, "size"):
|
| 29 |
self.processor.image_processor.size = {"longest_edge": 512}
|
|
|
|
| 49 |
model = AutoModelForVision2Seq.from_pretrained(
|
| 50 |
MODEL_ID,
|
| 51 |
torch_dtype=torch.float32,
|
| 52 |
+
attn_implementation="eager", # Matched exactly to prevent the SDPA crash
|
| 53 |
low_cpu_mem_usage=True
|
| 54 |
)
|
| 55 |
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
# Free unquantized weights from RAM immediately
|
| 62 |
+
import gc
|
| 63 |
del model
|
| 64 |
gc.collect()
|
| 65 |
print("Model is AWAKE and ready in RAM.")
|
|
|
|
| 89 |
return elements
|
| 90 |
|
| 91 |
def analyze(self, image: Image.Image):
|
| 92 |
+
# Gradio passes image directly as a PIL Image object
|
| 93 |
orig_width, orig_height = image.size
|
| 94 |
|
| 95 |
# Keep our CPU-friendly manual resizing, but scale to 512 max edge
|
|
|
|
| 113 |
generated_ids = self.model.generate(
|
| 114 |
**inputs,
|
| 115 |
max_new_tokens=1024,
|
| 116 |
+
use_cache=True, # Speeds up CPU generation by 10x-20x
|
| 117 |
eos_token_id=100257,
|
| 118 |
pad_token_id=100257
|
| 119 |
)
|
|
|
|
| 134 |
def gradio_interface_fn(image):
|
| 135 |
try:
|
| 136 |
elements = engine.analyze(image)
|
|
|
|
| 137 |
return json.dumps({"status": "success", "elements": elements})
|
| 138 |
except Exception as e:
|
| 139 |
return json.dumps({"status": "error", "message": str(e)})
|
|
|
|
| 142 |
demo = gr.Interface(
|
| 143 |
fn=gradio_interface_fn,
|
| 144 |
inputs=gr.Image(type="pil", label="Upload Screenshot"),
|
| 145 |
+
outputs="text",
|
| 146 |
title="ScreenVLM Production Parser"
|
| 147 |
)
|
| 148 |
|
| 149 |
if __name__ == "__main__":
|
| 150 |
+
# Let Hugging Face manage host and port dynamically (safer for default Space configs)
|
| 151 |
+
demo.queue().launch()
|