Spaces:
Running on Zero
Running on Zero
Add workspace scale control to avoid in-canvas zoom draw lag
Browse files
app.py
CHANGED
|
@@ -1346,11 +1346,27 @@ def load_image(file_path, page_num=1):
|
|
| 1346 |
else:
|
| 1347 |
return Image.open(file_path)
|
| 1348 |
|
| 1349 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1350 |
img = load_image(file_path, page_num)
|
| 1351 |
if img is None:
|
| 1352 |
return None, None, None
|
| 1353 |
-
|
|
|
|
| 1354 |
|
| 1355 |
def sync_workspace_state(editor_value, current_base_image):
|
| 1356 |
background = _extract_editor_background(editor_value)
|
|
@@ -1467,6 +1483,14 @@ with gr.Blocks(**blocks_kwargs) as demo:
|
|
| 1467 |
gr.Markdown("### OCR Workflow")
|
| 1468 |
task = gr.Dropdown(list(TASK_PROMPTS.keys()), value="📋 Markdown", label="Task")
|
| 1469 |
input_scope = gr.Radio(["Entire Page", "Selected Region"], value="Entire Page", label="Input Scope")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1470 |
|
| 1471 |
selection_controls = gr.Row(visible=False)
|
| 1472 |
with selection_controls:
|
|
@@ -1543,6 +1567,7 @@ with gr.Blocks(**blocks_kwargs) as demo:
|
|
| 1543 |
2. Choose **Input Scope**:
|
| 1544 |
- `Entire Page` for the full page.
|
| 1545 |
- `Selected Region` for a specific area.
|
|
|
|
| 1546 |
3. For `Selected Region`, use the **Image Workspace**:
|
| 1547 |
- Recommended: freehand selection (draw/highlight target); app uses an automatic bounding box around your marks.
|
| 1548 |
- Optional rectangle selection: use the **Crop** tool.
|
|
@@ -1583,13 +1608,16 @@ with gr.Blocks(**blocks_kwargs) as demo:
|
|
| 1583 |
task.change(select_boxes, [task], [tabs])
|
| 1584 |
input_scope.change(toggle_scope_ui, [input_scope], [workspace_hint, selection_controls, selection_status, selected_regions_gallery])
|
| 1585 |
if HAS_REGION_WORKSPACE and region_editor is not None:
|
| 1586 |
-
file_in.change(load_image_with_size, [file_in, page_selector], [region_editor, workspace_base_size, workspace_base_image])
|
| 1587 |
-
page_selector.change(load_image_with_size, [file_in, page_selector], [region_editor, workspace_base_size, workspace_base_image])
|
|
|
|
| 1588 |
region_editor.change(sync_workspace_state, [region_editor, workspace_base_image], [workspace_base_size, workspace_base_image])
|
| 1589 |
file_in.change(_reset_selected_regions, outputs=[selected_regions_state, selected_regions_gallery, selection_status])
|
| 1590 |
page_selector.change(_reset_selected_regions, outputs=[selected_regions_state, selected_regions_gallery, selection_status])
|
|
|
|
| 1591 |
file_in.change(_reset_drawn_mask, outputs=[drawn_mask_state])
|
| 1592 |
page_selector.change(_reset_drawn_mask, outputs=[drawn_mask_state])
|
|
|
|
| 1593 |
|
| 1594 |
add_region_btn.click(
|
| 1595 |
add_selected_region,
|
|
@@ -1643,20 +1671,20 @@ with gr.Blocks(**blocks_kwargs) as demo:
|
|
| 1643 |
full_img = base_image if isinstance(base_image, Image.Image) else _extract_editor_background(region_value)
|
| 1644 |
region_boxes = [r["bbox"] for r in regions if r.get("bbox") is not None]
|
| 1645 |
img_out = _draw_selected_region_boxes(full_img, region_boxes)
|
| 1646 |
-
elif
|
| 1647 |
-
cleaned, markdown, raw, img_out, crops =
|
| 1648 |
-
|
| 1649 |
task,
|
| 1650 |
custom_prompt,
|
|
|
|
| 1651 |
enable_equation_zoom=enable_equation_zoom,
|
| 1652 |
separate_equation_lines=detect_eq_lines,
|
| 1653 |
)
|
| 1654 |
-
elif
|
| 1655 |
-
cleaned, markdown, raw, img_out, crops =
|
| 1656 |
-
|
| 1657 |
task,
|
| 1658 |
custom_prompt,
|
| 1659 |
-
int(page_num),
|
| 1660 |
enable_equation_zoom=enable_equation_zoom,
|
| 1661 |
separate_equation_lines=detect_eq_lines,
|
| 1662 |
)
|
|
|
|
| 1346 |
else:
|
| 1347 |
return Image.open(file_path)
|
| 1348 |
|
| 1349 |
+
def _scale_workspace_image(img, workspace_scale):
|
| 1350 |
+
if img is None:
|
| 1351 |
+
return None
|
| 1352 |
+
try:
|
| 1353 |
+
scale = max(100, min(220, int(workspace_scale)))
|
| 1354 |
+
except Exception:
|
| 1355 |
+
scale = 100
|
| 1356 |
+
if scale == 100:
|
| 1357 |
+
return img
|
| 1358 |
+
ratio = scale / 100.0
|
| 1359 |
+
new_w = max(1, int(round(img.width * ratio)))
|
| 1360 |
+
new_h = max(1, int(round(img.height * ratio)))
|
| 1361 |
+
resample = Image.Resampling.BILINEAR if hasattr(Image, "Resampling") else Image.BILINEAR
|
| 1362 |
+
return img.resize((new_w, new_h), resample)
|
| 1363 |
+
|
| 1364 |
+
def load_image_with_size(file_path, page_num=1, workspace_scale=100):
|
| 1365 |
img = load_image(file_path, page_num)
|
| 1366 |
if img is None:
|
| 1367 |
return None, None, None
|
| 1368 |
+
display_img = _scale_workspace_image(img, workspace_scale)
|
| 1369 |
+
return display_img, (int(display_img.width), int(display_img.height)), display_img
|
| 1370 |
|
| 1371 |
def sync_workspace_state(editor_value, current_base_image):
|
| 1372 |
background = _extract_editor_background(editor_value)
|
|
|
|
| 1483 |
gr.Markdown("### OCR Workflow")
|
| 1484 |
task = gr.Dropdown(list(TASK_PROMPTS.keys()), value="📋 Markdown", label="Task")
|
| 1485 |
input_scope = gr.Radio(["Entire Page", "Selected Region"], value="Entire Page", label="Input Scope")
|
| 1486 |
+
workspace_scale = gr.Slider(
|
| 1487 |
+
minimum=100,
|
| 1488 |
+
maximum=220,
|
| 1489 |
+
step=10,
|
| 1490 |
+
value=140,
|
| 1491 |
+
label="Workspace Scale (%)",
|
| 1492 |
+
info="Use this instead of in-canvas zoom for smoother live drawing.",
|
| 1493 |
+
)
|
| 1494 |
|
| 1495 |
selection_controls = gr.Row(visible=False)
|
| 1496 |
with selection_controls:
|
|
|
|
| 1567 |
2. Choose **Input Scope**:
|
| 1568 |
- `Entire Page` for the full page.
|
| 1569 |
- `Selected Region` for a specific area.
|
| 1570 |
+
2a. For small math, increase **Workspace Scale** instead of using in-canvas zoom (better live drawing feedback).
|
| 1571 |
3. For `Selected Region`, use the **Image Workspace**:
|
| 1572 |
- Recommended: freehand selection (draw/highlight target); app uses an automatic bounding box around your marks.
|
| 1573 |
- Optional rectangle selection: use the **Crop** tool.
|
|
|
|
| 1608 |
task.change(select_boxes, [task], [tabs])
|
| 1609 |
input_scope.change(toggle_scope_ui, [input_scope], [workspace_hint, selection_controls, selection_status, selected_regions_gallery])
|
| 1610 |
if HAS_REGION_WORKSPACE and region_editor is not None:
|
| 1611 |
+
file_in.change(load_image_with_size, [file_in, page_selector, workspace_scale], [region_editor, workspace_base_size, workspace_base_image])
|
| 1612 |
+
page_selector.change(load_image_with_size, [file_in, page_selector, workspace_scale], [region_editor, workspace_base_size, workspace_base_image])
|
| 1613 |
+
workspace_scale.change(load_image_with_size, [file_in, page_selector, workspace_scale], [region_editor, workspace_base_size, workspace_base_image])
|
| 1614 |
region_editor.change(sync_workspace_state, [region_editor, workspace_base_image], [workspace_base_size, workspace_base_image])
|
| 1615 |
file_in.change(_reset_selected_regions, outputs=[selected_regions_state, selected_regions_gallery, selection_status])
|
| 1616 |
page_selector.change(_reset_selected_regions, outputs=[selected_regions_state, selected_regions_gallery, selection_status])
|
| 1617 |
+
workspace_scale.change(_reset_selected_regions, outputs=[selected_regions_state, selected_regions_gallery, selection_status])
|
| 1618 |
file_in.change(_reset_drawn_mask, outputs=[drawn_mask_state])
|
| 1619 |
page_selector.change(_reset_drawn_mask, outputs=[drawn_mask_state])
|
| 1620 |
+
workspace_scale.change(_reset_drawn_mask, outputs=[drawn_mask_state])
|
| 1621 |
|
| 1622 |
add_region_btn.click(
|
| 1623 |
add_selected_region,
|
|
|
|
| 1671 |
full_img = base_image if isinstance(base_image, Image.Image) else _extract_editor_background(region_value)
|
| 1672 |
region_boxes = [r["bbox"] for r in regions if r.get("bbox") is not None]
|
| 1673 |
img_out = _draw_selected_region_boxes(full_img, region_boxes)
|
| 1674 |
+
elif file_path:
|
| 1675 |
+
cleaned, markdown, raw, img_out, crops = process_file(
|
| 1676 |
+
file_path,
|
| 1677 |
task,
|
| 1678 |
custom_prompt,
|
| 1679 |
+
int(page_num),
|
| 1680 |
enable_equation_zoom=enable_equation_zoom,
|
| 1681 |
separate_equation_lines=detect_eq_lines,
|
| 1682 |
)
|
| 1683 |
+
elif (full_image := _extract_editor_background(region_value)) is not None:
|
| 1684 |
+
cleaned, markdown, raw, img_out, crops = process_image(
|
| 1685 |
+
full_image,
|
| 1686 |
task,
|
| 1687 |
custom_prompt,
|
|
|
|
| 1688 |
enable_equation_zoom=enable_equation_zoom,
|
| 1689 |
separate_equation_lines=detect_eq_lines,
|
| 1690 |
)
|