Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- README.md +1 -1
- app.py +133 -9
- app_launcher.py +43 -0
- app_simplified.py +3 -1
README.md
CHANGED
|
@@ -5,7 +5,7 @@ colorFrom: blue
|
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.0.0
|
| 8 |
-
app_file:
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
|
|
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.0.0
|
| 8 |
+
app_file: app_launcher.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
app.py
CHANGED
|
@@ -57,16 +57,140 @@ def setup_omniparser():
|
|
| 57 |
# Patch PaddleOCR initialization in utils.py to fix compatibility issue
|
| 58 |
utils_path = os.path.join(omniparser_path, "util", "utils.py")
|
| 59 |
if os.path.exists(utils_path):
|
| 60 |
-
print("Patching utils.py to fix
|
| 61 |
-
with open(utils_path, 'r') as f:
|
| 62 |
-
content = f.read()
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
print("OmniParser setup completed successfully!")
|
| 72 |
return True
|
|
|
|
| 57 |
# Patch PaddleOCR initialization in utils.py to fix compatibility issue
|
| 58 |
utils_path = os.path.join(omniparser_path, "util", "utils.py")
|
| 59 |
if os.path.exists(utils_path):
|
| 60 |
+
print("Patching utils.py to fix compatibility issues...")
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# Create a simplified version of utils.py with essential functions
|
| 63 |
+
simplified_utils = """import os
|
| 64 |
+
import io
|
| 65 |
+
import cv2
|
| 66 |
+
import base64
|
| 67 |
+
import numpy as np
|
| 68 |
+
import torch
|
| 69 |
+
from PIL import Image, ImageDraw
|
| 70 |
+
|
| 71 |
+
def check_ocr_box(image, display_img=False, output_bb_format='xyxy', goal_filtering=None,
|
| 72 |
+
easyocr_args=None, use_paddleocr=True):
|
| 73 |
+
"""
|
| 74 |
+
Custom implementation of check_ocr_box that uses EasyOCR
|
| 75 |
+
"""
|
| 76 |
+
try:
|
| 77 |
+
import easyocr
|
| 78 |
+
|
| 79 |
+
# Convert PIL Image to numpy array
|
| 80 |
+
img_np = np.array(image)
|
| 81 |
+
|
| 82 |
+
# Initialize EasyOCR
|
| 83 |
+
reader = easyocr.Reader(['en'])
|
| 84 |
+
|
| 85 |
+
# Run OCR
|
| 86 |
+
results = reader.readtext(img_np)
|
| 87 |
+
|
| 88 |
+
# Extract text and bounding boxes
|
| 89 |
+
texts = []
|
| 90 |
+
boxes = []
|
| 91 |
+
|
| 92 |
+
for result in results:
|
| 93 |
+
box, text, _ = result
|
| 94 |
+
texts.append(text)
|
| 95 |
+
|
| 96 |
+
# Convert box format if needed
|
| 97 |
+
if output_bb_format == 'xyxy':
|
| 98 |
+
# Convert from [[x1,y1],[x2,y2],[x3,y3],[x4,y4]] to [x1,y1,x3,y3]
|
| 99 |
+
x1, y1 = box[0]
|
| 100 |
+
x3, y3 = box[2]
|
| 101 |
+
boxes.append([x1, y1, x3, y3])
|
| 102 |
+
else:
|
| 103 |
+
boxes.append(box)
|
| 104 |
+
|
| 105 |
+
return (texts, boxes), False
|
| 106 |
+
except Exception as e:
|
| 107 |
+
print(f"Error in OCR: {str(e)}")
|
| 108 |
+
return ([], []), False
|
| 109 |
+
|
| 110 |
+
def get_yolo_model(model_path):
|
| 111 |
+
"""
|
| 112 |
+
Load YOLO model for icon detection
|
| 113 |
+
"""
|
| 114 |
+
try:
|
| 115 |
+
from ultralytics import YOLO
|
| 116 |
+
model = YOLO(model_path)
|
| 117 |
+
return model
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"Error loading YOLO model: {str(e)}")
|
| 120 |
+
return None
|
| 121 |
+
|
| 122 |
+
def get_caption_model_processor(model_name, model_name_or_path):
|
| 123 |
+
"""
|
| 124 |
+
Load caption model and processor
|
| 125 |
+
"""
|
| 126 |
+
try:
|
| 127 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 128 |
+
|
| 129 |
+
processor = AutoProcessor.from_pretrained(model_name_or_path)
|
| 130 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 131 |
+
model_name_or_path,
|
| 132 |
+
torch_dtype=torch.float16,
|
| 133 |
+
device_map="auto"
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
return (model, processor)
|
| 137 |
+
except Exception as e:
|
| 138 |
+
print(f"Error loading caption model: {str(e)}")
|
| 139 |
+
return None
|
| 140 |
+
|
| 141 |
+
def get_som_labeled_img(image, yolo_model, BOX_TRESHOLD=0.05, output_coord_in_ratio=True,
|
| 142 |
+
ocr_bbox=None, draw_bbox_config=None, caption_model_processor=None,
|
| 143 |
+
ocr_text=None, iou_threshold=0.1, imgsz=640):
|
| 144 |
+
"""
|
| 145 |
+
Simplified implementation of get_som_labeled_img
|
| 146 |
+
"""
|
| 147 |
+
try:
|
| 148 |
+
# Create a copy of the image for visualization
|
| 149 |
+
vis_img = image.copy()
|
| 150 |
+
draw = ImageDraw.Draw(vis_img)
|
| 151 |
+
|
| 152 |
+
# Run YOLO detection
|
| 153 |
+
results = yolo_model(image, imgsz=imgsz)
|
| 154 |
+
|
| 155 |
+
# Process results
|
| 156 |
+
elements = []
|
| 157 |
+
for i, det in enumerate(results[0].boxes.data):
|
| 158 |
+
x1, y1, x2, y2, conf, cls = det
|
| 159 |
+
|
| 160 |
+
if conf < BOX_TRESHOLD:
|
| 161 |
+
continue
|
| 162 |
+
|
| 163 |
+
# Draw bounding box
|
| 164 |
+
draw.rectangle([x1, y1, x2, y2], outline="red", width=2)
|
| 165 |
+
|
| 166 |
+
# Generate caption
|
| 167 |
+
caption = f"UI Element {i}"
|
| 168 |
+
|
| 169 |
+
# Add to elements list
|
| 170 |
+
elements.append({
|
| 171 |
+
"id": i,
|
| 172 |
+
"text": "",
|
| 173 |
+
"caption": caption,
|
| 174 |
+
"coordinates": [x1/image.width, y1/image.height, x2/image.width, y2/image.height],
|
| 175 |
+
"is_interactable": True,
|
| 176 |
+
"confidence": float(conf)
|
| 177 |
+
})
|
| 178 |
+
|
| 179 |
+
# Convert to base64
|
| 180 |
+
buffered = io.BytesIO()
|
| 181 |
+
vis_img.save(buffered, format="PNG")
|
| 182 |
+
img_str = "data:image/png;base64," + base64.b64encode(buffered.getvalue()).decode()
|
| 183 |
+
|
| 184 |
+
return img_str, [], elements
|
| 185 |
+
except Exception as e:
|
| 186 |
+
print(f"Error in get_som_labeled_img: {str(e)}")
|
| 187 |
+
return "Error processing image", [], []
|
| 188 |
+
"""
|
| 189 |
+
|
| 190 |
+
# Write the simplified utils.py
|
| 191 |
+
with open(utils_path, 'w') as f:
|
| 192 |
+
f.write(simplified_utils)
|
| 193 |
+
print("Created simplified utils.py with essential functions")
|
| 194 |
|
| 195 |
print("OmniParser setup completed successfully!")
|
| 196 |
return True
|
app_launcher.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import importlib.util
|
| 4 |
+
|
| 5 |
+
def check_module(module_name):
|
| 6 |
+
"""Check if a module can be imported"""
|
| 7 |
+
try:
|
| 8 |
+
importlib.import_module(module_name)
|
| 9 |
+
return True
|
| 10 |
+
except ImportError:
|
| 11 |
+
return False
|
| 12 |
+
|
| 13 |
+
def main():
|
| 14 |
+
"""
|
| 15 |
+
Determine which app version to launch based on available dependencies
|
| 16 |
+
"""
|
| 17 |
+
print("Checking environment for OmniParser compatibility...")
|
| 18 |
+
|
| 19 |
+
# Check for required dependencies
|
| 20 |
+
dependencies = {
|
| 21 |
+
"torch": check_module("torch"),
|
| 22 |
+
"transformers": check_module("transformers"),
|
| 23 |
+
"ultralytics": check_module("ultralytics"),
|
| 24 |
+
"easyocr": check_module("easyocr"),
|
| 25 |
+
"paddleocr": check_module("paddleocr")
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
# Print dependency status
|
| 29 |
+
for dep, available in dependencies.items():
|
| 30 |
+
print(f" {dep}: {'✓' if available else '✗'}")
|
| 31 |
+
|
| 32 |
+
# Decide which version to use
|
| 33 |
+
if all([dependencies["torch"], dependencies["transformers"], dependencies["ultralytics"]]):
|
| 34 |
+
print("\nUsing full version (app.py)")
|
| 35 |
+
# Import and run the full version
|
| 36 |
+
import app
|
| 37 |
+
else:
|
| 38 |
+
print("\nUsing simplified version (app_simplified.py)")
|
| 39 |
+
# Import and run the simplified version
|
| 40 |
+
import app_simplified
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
main()
|
app_simplified.py
CHANGED
|
@@ -5,10 +5,12 @@ import base64
|
|
| 5 |
import random
|
| 6 |
import gradio as gr
|
| 7 |
import numpy as np
|
| 8 |
-
import torch
|
| 9 |
from PIL import Image, ImageDraw, ImageFont
|
| 10 |
from typing import Dict, Any, List
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
# Simplified OmniParser API that doesn't rely on the actual OmniParser repository
|
| 13 |
# This is a fallback in case the main app.py has issues with dependencies
|
| 14 |
|
|
|
|
| 5 |
import random
|
| 6 |
import gradio as gr
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
from PIL import Image, ImageDraw, ImageFont
|
| 9 |
from typing import Dict, Any, List
|
| 10 |
|
| 11 |
+
# Don't import torch to avoid potential issues
|
| 12 |
+
# import torch
|
| 13 |
+
|
| 14 |
# Simplified OmniParser API that doesn't rely on the actual OmniParser repository
|
| 15 |
# This is a fallback in case the main app.py has issues with dependencies
|
| 16 |
|