Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -153,7 +153,7 @@ def analyze_and_visualize_layout(input_image: Image.Image, selected_model_name:
|
|
| 153 |
|
| 154 |
progress(0.5, desc=f"Generating layout data with {model_name_desc}...")
|
| 155 |
with torch.no_grad():
|
| 156 |
-
output_ids = model_to_run.generate(**inputs, max_new_tokens=4096, do_sample=False)
|
| 157 |
|
| 158 |
raw_text = processor.batch_decode(output_ids[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)[0]
|
| 159 |
|
|
@@ -210,7 +210,18 @@ def analyze_and_visualize_layout(input_image: Image.Image, selected_model_name:
|
|
| 210 |
except IOError:
|
| 211 |
font = ImageFont.load_default()
|
| 212 |
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
bbox, label, order = item.get("bbox_2d"), item.get("label", "other"), item.get("order", "")
|
| 215 |
if not bbox or len(bbox) != 4: continue
|
| 216 |
|
|
|
|
| 153 |
|
| 154 |
progress(0.5, desc=f"Generating layout data with {model_name_desc}...")
|
| 155 |
with torch.no_grad():
|
| 156 |
+
output_ids = model_to_run.generate(**inputs, max_new_tokens=4096*2, do_sample=False)
|
| 157 |
|
| 158 |
raw_text = processor.batch_decode(output_ids[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)[0]
|
| 159 |
|
|
|
|
| 210 |
except IOError:
|
| 211 |
font = ImageFont.load_default()
|
| 212 |
|
| 213 |
+
# 推荐的修改
|
| 214 |
+
def get_safe_order(item):
|
| 215 |
+
"""一个安全的函数,用于获取并转换order值为整数,处理潜在的错误。"""
|
| 216 |
+
try:
|
| 217 |
+
# 尝试将获取到的值转换为整数
|
| 218 |
+
return int(item.get("order", 999))
|
| 219 |
+
except (ValueError, TypeError):
|
| 220 |
+
# 如果转换失败(例如,值是"abc"这样的非数字字符串),则返回默认值
|
| 221 |
+
return 999
|
| 222 |
+
|
| 223 |
+
# 在排序时使用这个新函数
|
| 224 |
+
for item in sorted(results, key=get_safe_order):
|
| 225 |
bbox, label, order = item.get("bbox_2d"), item.get("label", "other"), item.get("order", "")
|
| 226 |
if not bbox or len(bbox) != 4: continue
|
| 227 |
|