Spaces:
Running on Zero
Running on Zero
Update app_v2.py
Browse files
app_v2.py
CHANGED
|
@@ -120,16 +120,35 @@ async def get_example(name: str, thumb: int = 0):
|
|
| 120 |
|
| 121 |
@server.api(name="anonymize_screenshot")
|
| 122 |
def anonymize_screenshot_api(image_path: str) -> str:
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
|
| 135 |
# =====================================================================
|
|
@@ -1278,4 +1297,4 @@ function toast(msg) {
|
|
| 1278 |
|
| 1279 |
|
| 1280 |
if __name__ == "__main__":
|
| 1281 |
-
server.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 120 |
|
| 121 |
@server.api(name="anonymize_screenshot")
|
| 122 |
def anonymize_screenshot_api(image_path: str) -> str:
|
| 123 |
+
"""Gradio API: accepts an image (via gradio_client.handle_file) and
|
| 124 |
+
returns detected redaction boxes as JSON.
|
| 125 |
+
|
| 126 |
+
Tolerant of both a plain path string and a FileData dict, since
|
| 127 |
+
different gradio_client versions pass each.
|
| 128 |
+
"""
|
| 129 |
+
import traceback
|
| 130 |
+
try:
|
| 131 |
+
if isinstance(image_path, dict):
|
| 132 |
+
image_path = image_path.get("path") or image_path.get("url") or ""
|
| 133 |
+
if not image_path or not isinstance(image_path, str):
|
| 134 |
+
return json.dumps({"error": f"expected image path, got {type(image_path).__name__}"})
|
| 135 |
+
|
| 136 |
+
img = Image.open(image_path).convert("RGB")
|
| 137 |
+
ocr = ocr_image(img)
|
| 138 |
+
if not ocr["text"].strip():
|
| 139 |
+
return json.dumps({
|
| 140 |
+
"width": img.width, "height": img.height,
|
| 141 |
+
"boxes": [], "text": "", "spans": [],
|
| 142 |
+
})
|
| 143 |
+
_, spans = run_pii_analysis(ocr["text"])
|
| 144 |
+
boxes = map_spans_to_boxes(ocr["words"], spans)
|
| 145 |
+
return json.dumps({
|
| 146 |
+
"width": img.width, "height": img.height,
|
| 147 |
+
"boxes": boxes, "text": ocr["text"], "spans": spans,
|
| 148 |
+
}, ensure_ascii=False)
|
| 149 |
+
except Exception as e:
|
| 150 |
+
traceback.print_exc()
|
| 151 |
+
return json.dumps({"error": f"{type(e).__name__}: {e}"})
|
| 152 |
|
| 153 |
|
| 154 |
# =====================================================================
|
|
|
|
| 1297 |
|
| 1298 |
|
| 1299 |
if __name__ == "__main__":
|
| 1300 |
+
server.launch(server_name="0.0.0.0", server_port=7860, show_error=True)
|