Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# app.py
|
| 2 |
-
# FINAL
|
| 3 |
-
#
|
| 4 |
|
| 5 |
import os
|
| 6 |
import torch
|
|
@@ -8,43 +8,29 @@ import gradio as gr
|
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
import json
|
| 11 |
-
from io import BytesIO
|
| 12 |
|
| 13 |
-
# Hugging Face Hub utilities for downloading model files
|
| 14 |
from huggingface_hub import hf_hub_download
|
| 15 |
-
|
| 16 |
-
# Core libraries for the models
|
| 17 |
from ultralytics import YOLO
|
| 18 |
from transformers import AutoProcessor, AutoModelForCausalLM, set_seed
|
| 19 |
from paddleocr import PaddleOCR
|
| 20 |
-
|
| 21 |
-
# Computer vision utilities
|
| 22 |
import supervision as sv
|
| 23 |
-
|
| 24 |
-
# Suppress unnecessary warnings for a cleaner log
|
| 25 |
import warnings
|
|
|
|
| 26 |
warnings.filterwarnings("ignore")
|
| 27 |
|
| 28 |
# --- Global Configuration ---
|
| 29 |
REPO_ID = "microsoft/OmniParser-v2.0"
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 33 |
-
# This specific revision of the Florence-2 model code is known to work on CPU.
|
| 34 |
-
FLORENCE_REVISION = "e149e62e3c88b64e42138e12b9a04683b5d1e26e"
|
| 35 |
-
|
| 36 |
print(f"INFO: Using device: {DEVICE}")
|
| 37 |
set_seed(42)
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
# PART 2: AUTOMATED MODEL LOADING FROM THE HUB
|
| 41 |
-
# =====================================================================================
|
| 42 |
-
|
| 43 |
def load_detection_model():
|
| 44 |
print("INFO: Loading detection model...")
|
| 45 |
try:
|
| 46 |
-
# Note: The original repo structure might use a local path 'weights/detection/model.pt'
|
| 47 |
-
# hf_hub_download ensures it's available.
|
| 48 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=DETECTION_MODEL_FILENAME)
|
| 49 |
model = YOLO(model_path)
|
| 50 |
print("INFO: Detection model loaded successfully.")
|
|
@@ -56,44 +42,24 @@ def load_detection_model():
|
|
| 56 |
def load_caption_model():
|
| 57 |
print("INFO: Loading captioning model...")
|
| 58 |
try:
|
| 59 |
-
#
|
| 60 |
-
# We load the weights from the subfolder but pin the *remote code* to a specific stable revision.
|
| 61 |
model = AutoModelForCausalLM.from_pretrained(
|
| 62 |
-
|
|
|
|
| 63 |
torch_dtype=torch.float32,
|
| 64 |
trust_remote_code=True,
|
| 65 |
-
|
| 66 |
).to(DEVICE)
|
| 67 |
-
|
| 68 |
processor = AutoProcessor.from_pretrained(
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
)
|
| 73 |
print("INFO: Captioning model loaded successfully.")
|
| 74 |
return model, processor
|
| 75 |
except Exception as e:
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
try:
|
| 79 |
-
# Fallback to the main repo ID if the subfolder path fails
|
| 80 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 81 |
-
"microsoft/Florence-2-base-ft",
|
| 82 |
-
torch_dtype=torch.float32,
|
| 83 |
-
trust_remote_code=True,
|
| 84 |
-
revision=FLORENCE_REVISION
|
| 85 |
-
).to(DEVICE)
|
| 86 |
-
processor = AutoProcessor.from_pretrained(
|
| 87 |
-
"microsoft/Florence-2-base-ft",
|
| 88 |
-
trust_remote_code=True,
|
| 89 |
-
revision=FLORENCE_REVISION
|
| 90 |
-
)
|
| 91 |
-
print("INFO: Captioning model loaded successfully via fallback.")
|
| 92 |
-
return model, processor
|
| 93 |
-
except Exception as fallback_e:
|
| 94 |
-
print(f"ERROR: Fallback also failed. Main error: {e}, Fallback error: {fallback_e}")
|
| 95 |
-
return None, None
|
| 96 |
-
|
| 97 |
|
| 98 |
def load_ocr_model():
|
| 99 |
print("INFO: Loading OCR model...")
|
|
@@ -105,15 +71,11 @@ def load_ocr_model():
|
|
| 105 |
print(f"ERROR: Failed to load OCR model: {e}")
|
| 106 |
return None
|
| 107 |
|
| 108 |
-
# --- Initialize Models at Application Startup ---
|
| 109 |
detection_model = load_detection_model()
|
| 110 |
caption_model, caption_processor = load_caption_model()
|
| 111 |
ocr_model = load_ocr_model()
|
| 112 |
|
| 113 |
-
#
|
| 114 |
-
# PART 3: HELPER AND PREDICTION FUNCTIONS
|
| 115 |
-
# =====================================================================================
|
| 116 |
-
|
| 117 |
def run_captioning(image, text, model, processor):
|
| 118 |
prompt = f"<OD> <ref> {text} </ref>"
|
| 119 |
inputs = processor(text=prompt, images=image, return_tensors="pt").to(DEVICE)
|
|
@@ -129,14 +91,22 @@ def run_captioning(image, text, model, processor):
|
|
| 129 |
final_caption_list = parsed_text.get('<OD>', {}).get('labels', [])
|
| 130 |
return final_caption_list[0] if final_caption_list else "No description available"
|
| 131 |
|
| 132 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
if not all([detection_model, caption_model, ocr_model]):
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
image_np = np.array(input_image.convert("RGB"))
|
| 138 |
|
| 139 |
-
# OCR
|
| 140 |
ocr_results = ocr_model.ocr(image_np, cls=True)[0]
|
| 141 |
ocr_texts = []
|
| 142 |
if ocr_results:
|
|
@@ -144,42 +114,48 @@ def predict_func(input_image: Image.Image):
|
|
| 144 |
points, (text, conf) = line
|
| 145 |
x_coords = [p[0] for p in points]
|
| 146 |
y_coords = [p[1] for p in points]
|
| 147 |
-
ocr_texts.append({"box": [min(x_coords), min(y_coords), max(x_coords), max(y_coords)], "text": text})
|
| 148 |
|
| 149 |
-
# Object Detection
|
| 150 |
detection_results = detection_model(image_np, verbose=False)[0]
|
| 151 |
detections = sv.Detections.from_ultralytics(detection_results)
|
| 152 |
|
| 153 |
-
# Process and combine
|
| 154 |
parsed_elements = []
|
|
|
|
|
|
|
| 155 |
for i in range(len(detections)):
|
| 156 |
box = detections.xyxy[i].astype(int)
|
|
|
|
| 157 |
class_name = detection_model.model.names[detections.class_id[i]]
|
| 158 |
|
| 159 |
cropped_image = input_image.crop(tuple(box))
|
| 160 |
caption = run_captioning(cropped_image, f"Describe this UI element.", caption_model, caption_processor)
|
| 161 |
|
| 162 |
-
contained_text = " ".join([o["text"] for o in ocr_texts if (box
|
| 163 |
|
| 164 |
parsed_elements.append({
|
| 165 |
-
"box_2d": box.tolist(), "
|
| 166 |
-
"
|
| 167 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
return {"parsed_elements": parsed_elements}
|
| 170 |
|
| 171 |
-
#
|
| 172 |
-
|
| 173 |
-
#
|
| 174 |
-
|
| 175 |
-
with gr.Blocks() as demo:
|
| 176 |
-
gr.Markdown("# Microsoft OmniParser-v2 API Endpoint")
|
| 177 |
with gr.Row():
|
| 178 |
image_input = gr.Image(type="pil", label="Input UI Screenshot")
|
| 179 |
json_output = gr.JSON(label="Parsed UI Elements")
|
| 180 |
|
| 181 |
submit_button = gr.Button("Parse UI", variant="primary")
|
| 182 |
-
submit_button.click(fn=
|
| 183 |
|
| 184 |
-
|
| 185 |
-
demo.launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
# FINAL, DEFINITIVE VERSION
|
| 3 |
+
# Corrects all model loading paths and relies on the stable requirements.txt
|
| 4 |
|
| 5 |
import os
|
| 6 |
import torch
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
import json
|
|
|
|
| 11 |
|
|
|
|
| 12 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
| 13 |
from ultralytics import YOLO
|
| 14 |
from transformers import AutoProcessor, AutoModelForCausalLM, set_seed
|
| 15 |
from paddleocr import PaddleOCR
|
|
|
|
|
|
|
| 16 |
import supervision as sv
|
|
|
|
|
|
|
| 17 |
import warnings
|
| 18 |
+
|
| 19 |
warnings.filterwarnings("ignore")
|
| 20 |
|
| 21 |
# --- Global Configuration ---
|
| 22 |
REPO_ID = "microsoft/OmniParser-v2.0"
|
| 23 |
+
# CORRECTED file paths as they exist in the Hugging Face repository
|
| 24 |
+
DETECTION_MODEL_FILENAME = "icon_detect/model.pt"
|
| 25 |
+
CAPTION_MODEL_SUBFOLDER = "icon_caption"
|
| 26 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
| 27 |
print(f"INFO: Using device: {DEVICE}")
|
| 28 |
set_seed(42)
|
| 29 |
|
| 30 |
+
# --- Model Loading ---
|
|
|
|
|
|
|
|
|
|
| 31 |
def load_detection_model():
|
| 32 |
print("INFO: Loading detection model...")
|
| 33 |
try:
|
|
|
|
|
|
|
| 34 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=DETECTION_MODEL_FILENAME)
|
| 35 |
model = YOLO(model_path)
|
| 36 |
print("INFO: Detection model loaded successfully.")
|
|
|
|
| 42 |
def load_caption_model():
|
| 43 |
print("INFO: Loading captioning model...")
|
| 44 |
try:
|
| 45 |
+
# CORRECTED loading logic using repo_id and subfolder arguments
|
|
|
|
| 46 |
model = AutoModelForCausalLM.from_pretrained(
|
| 47 |
+
REPO_ID,
|
| 48 |
+
subfolder=CAPTION_MODEL_SUBFOLDER,
|
| 49 |
torch_dtype=torch.float32,
|
| 50 |
trust_remote_code=True,
|
| 51 |
+
attn_implementation="eager"
|
| 52 |
).to(DEVICE)
|
|
|
|
| 53 |
processor = AutoProcessor.from_pretrained(
|
| 54 |
+
REPO_ID,
|
| 55 |
+
subfolder=CAPTION_MODEL_SUBFOLDER,
|
| 56 |
+
trust_remote_code=True
|
| 57 |
)
|
| 58 |
print("INFO: Captioning model loaded successfully.")
|
| 59 |
return model, processor
|
| 60 |
except Exception as e:
|
| 61 |
+
print(f"ERROR: Failed to load captioning model: {e}")
|
| 62 |
+
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
def load_ocr_model():
|
| 65 |
print("INFO: Loading OCR model...")
|
|
|
|
| 71 |
print(f"ERROR: Failed to load OCR model: {e}")
|
| 72 |
return None
|
| 73 |
|
|
|
|
| 74 |
detection_model = load_detection_model()
|
| 75 |
caption_model, caption_processor = load_caption_model()
|
| 76 |
ocr_model = load_ocr_model()
|
| 77 |
|
| 78 |
+
# --- Inference Pipeline ---
|
|
|
|
|
|
|
|
|
|
| 79 |
def run_captioning(image, text, model, processor):
|
| 80 |
prompt = f"<OD> <ref> {text} </ref>"
|
| 81 |
inputs = processor(text=prompt, images=image, return_tensors="pt").to(DEVICE)
|
|
|
|
| 91 |
final_caption_list = parsed_text.get('<OD>', {}).get('labels', [])
|
| 92 |
return final_caption_list[0] if final_caption_list else "No description available"
|
| 93 |
|
| 94 |
+
def is_box_contained(outer_box, inner_box):
|
| 95 |
+
return (outer_box[0] <= inner_box[0] and
|
| 96 |
+
outer_box[1] <= inner_box[1] and
|
| 97 |
+
outer_box[2] >= inner_box[2] and
|
| 98 |
+
outer_box[3] >= inner_box[3])
|
| 99 |
+
|
| 100 |
+
def predict(input_image: Image.Image):
|
| 101 |
if not all([detection_model, caption_model, ocr_model]):
|
| 102 |
+
error_messages = []
|
| 103 |
+
if not detection_model: error_messages.append("Detection model failed.")
|
| 104 |
+
if not caption_model: error_messages.append("Captioning model failed.")
|
| 105 |
+
if not ocr_model: error_messages.append("OCR model failed.")
|
| 106 |
+
return {"error": " ".join(error_messages) + " Check container logs for details."}
|
| 107 |
|
| 108 |
image_np = np.array(input_image.convert("RGB"))
|
| 109 |
|
|
|
|
| 110 |
ocr_results = ocr_model.ocr(image_np, cls=True)[0]
|
| 111 |
ocr_texts = []
|
| 112 |
if ocr_results:
|
|
|
|
| 114 |
points, (text, conf) = line
|
| 115 |
x_coords = [p[0] for p in points]
|
| 116 |
y_coords = [p[1] for p in points]
|
| 117 |
+
ocr_texts.append({"box": [min(x_coords), min(y_coords), max(x_coords), max(y_coords)], "text": text, "conf": conf})
|
| 118 |
|
|
|
|
| 119 |
detection_results = detection_model(image_np, verbose=False)[0]
|
| 120 |
detections = sv.Detections.from_ultralytics(detection_results)
|
| 121 |
|
|
|
|
| 122 |
parsed_elements = []
|
| 123 |
+
element_id_counter = 0
|
| 124 |
+
|
| 125 |
for i in range(len(detections)):
|
| 126 |
box = detections.xyxy[i].astype(int)
|
| 127 |
+
confidence = detections.confidence[i]
|
| 128 |
class_name = detection_model.model.names[detections.class_id[i]]
|
| 129 |
|
| 130 |
cropped_image = input_image.crop(tuple(box))
|
| 131 |
caption = run_captioning(cropped_image, f"Describe this UI element.", caption_model, caption_processor)
|
| 132 |
|
| 133 |
+
contained_text = " ".join([o["text"] for o in ocr_texts if is_box_contained(box.tolist(), o["box"])])
|
| 134 |
|
| 135 |
parsed_elements.append({
|
| 136 |
+
"id": element_id_counter, "box_2d": box.tolist(), "type": class_name,
|
| 137 |
+
"text": contained_text.strip(), "description": caption, "confidence": float(confidence)
|
| 138 |
})
|
| 139 |
+
element_id_counter += 1
|
| 140 |
+
|
| 141 |
+
for ocr in ocr_texts:
|
| 142 |
+
if not any(is_box_contained(el["box_2d"], ocr["box"]) for el in parsed_elements):
|
| 143 |
+
parsed_elements.append({
|
| 144 |
+
"id": element_id_counter, "box_2d": [int(p) for p in ocr["box"]], "type": "text_label",
|
| 145 |
+
"text": ocr["text"], "description": "A text label.", "confidence": float(ocr["conf"])
|
| 146 |
+
})
|
| 147 |
+
element_id_counter += 1
|
| 148 |
|
| 149 |
return {"parsed_elements": parsed_elements}
|
| 150 |
|
| 151 |
+
# --- Gradio Interface ---
|
| 152 |
+
with gr.Blocks(css="footer {display: none!important}") as demo:
|
| 153 |
+
gr.Markdown("# Microsoft OmniParser-v2 API Endpoint\nUpload a UI screenshot to get a parsed JSON output.")
|
|
|
|
|
|
|
|
|
|
| 154 |
with gr.Row():
|
| 155 |
image_input = gr.Image(type="pil", label="Input UI Screenshot")
|
| 156 |
json_output = gr.JSON(label="Parsed UI Elements")
|
| 157 |
|
| 158 |
submit_button = gr.Button("Parse UI", variant="primary")
|
| 159 |
+
submit_button.click(fn=predict, inputs=[image_input], outputs=[json_output], api_name="predict")
|
| 160 |
|
| 161 |
+
demo.launch()
|
|
|