ysharma HF Staff commited on
Commit
6d6ec66
·
verified ·
1 Parent(s): 1d0caeb

Update app_v2.py

Browse files
Files changed (1) hide show
  1. app_v2.py +30 -11
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
- img = Image.open(image_path).convert("RGB")
124
- ocr = ocr_image(img)
125
- if not ocr["text"].strip():
126
- return json.dumps({"boxes": [], "text": "", "spans": []})
127
- _, spans = run_pii_analysis(ocr["text"])
128
- boxes = map_spans_to_boxes(ocr["words"], spans)
129
- return json.dumps({
130
- "width": img.width, "height": img.height,
131
- "boxes": boxes, "text": ocr["text"], "spans": spans,
132
- }, ensure_ascii=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)