ibsocr1 commited on
Commit
7acbc8d
·
verified ·
1 Parent(s): 4c1f362

Upload 9 files

Browse files
Files changed (3) hide show
  1. README.md +3 -0
  2. __pycache__/app.cpython-313.pyc +0 -0
  3. app.py +10 -3
README.md CHANGED
@@ -49,3 +49,6 @@ cat
49
  ## Important
50
 
51
  For production, attach persistent storage to the Space so the dataset and trained model survive rebuilds.
 
 
 
 
49
  ## Important
50
 
51
  For production, attach persistent storage to the Space so the dataset and trained model survive rebuilds.
52
+
53
+ ## Startup fix
54
+ The annotation JavaScript is registered at the Gradio Blocks level rather than using the unsupported `js_on_load` argument on `gr.HTML`.
__pycache__/app.cpython-313.pyc CHANGED
Binary files a/__pycache__/app.cpython-313.pyc and b/__pycache__/app.cpython-313.pyc differ
 
app.py CHANGED
@@ -168,8 +168,9 @@ def annotation_html(filename):
168
 
169
  ANNOTATOR_JS = r"""
170
  function setupAnythingAnnotator() {
171
- const root = element.querySelector(".annotator-root");
172
  if (!root) return;
 
173
 
174
  const canvas = root.querySelector(".ann-canvas");
175
  const ctx = canvas.getContext("2d");
@@ -367,6 +368,13 @@ function setupAnythingAnnotator() {
367
  }
368
 
369
  setupAnythingAnnotator();
 
 
 
 
 
 
 
370
  """
371
 
372
 
@@ -675,7 +683,7 @@ def detect(image, threshold):
675
  return out, json.dumps(response, indent=2)
676
 
677
 
678
- with gr.Blocks(title="Anything Detector") as demo:
679
  gr.Markdown(
680
  "# 🔍 Anything Detector\n"
681
  "Train your own object detector for any classes you define. **No YOLO.**"
@@ -742,7 +750,6 @@ This is a native HTML5 canvas — there is no `gradio_image_annotation` package.
742
  )
743
  ),
744
  elem_id="annotator_html",
745
- js_on_load=ANNOTATOR_JS,
746
  )
747
 
748
  # Hidden data channel used by the canvas Save button.
 
168
 
169
  ANNOTATOR_JS = r"""
170
  function setupAnythingAnnotator() {
171
+ const root = document.querySelector(".annotator-root:not([data-ann-initialized])");
172
  if (!root) return;
173
+ root.dataset.annInitialized = "1";
174
 
175
  const canvas = root.querySelector(".ann-canvas");
176
  const ctx = canvas.getContext("2d");
 
368
  }
369
 
370
  setupAnythingAnnotator();
371
+
372
+ // Gradio replaces the HTML component's DOM when an image is selected.
373
+ // Re-initialize the canvas whenever a new annotator root appears.
374
+ const anythingAnnotatorObserver = new MutationObserver(() => {
375
+ setupAnythingAnnotator();
376
+ });
377
+ anythingAnnotatorObserver.observe(document.body, { childList: true, subtree: true });
378
  """
379
 
380
 
 
683
  return out, json.dumps(response, indent=2)
684
 
685
 
686
+ with gr.Blocks(title="Anything Detector", js=ANNOTATOR_JS) as demo:
687
  gr.Markdown(
688
  "# 🔍 Anything Detector\n"
689
  "Train your own object detector for any classes you define. **No YOLO.**"
 
750
  )
751
  ),
752
  elem_id="annotator_html",
 
753
  )
754
 
755
  # Hidden data channel used by the canvas Save button.