ibsocr1 commited on
Commit
bb321c3
·
verified ·
1 Parent(s): f8929eb

Upload 9 files

Browse files
Files changed (2) hide show
  1. __pycache__/app.cpython-313.pyc +0 -0
  2. app.py +46 -12
__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
@@ -75,8 +75,47 @@ def image_record(filename):
75
  )
76
 
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  def upload_training_images(files):
79
- """Copy uploaded files into persistent dataset storage and register them."""
80
  DATA.mkdir(parents=True, exist_ok=True)
81
  IMAGE_DIR.mkdir(parents=True, exist_ok=True)
82
  db = load_db()
@@ -85,7 +124,7 @@ def upload_training_images(files):
85
  return (
86
  gr.update(choices=choices, value=choices[-1] if choices else None),
87
  "⚠️ No images selected.",
88
- [x["path"] for x in db["images"] if Path(x.get("path", "")).exists()],
89
  gr.update(choices=choices, value=choices[-1] if choices else None),
90
  annotation_html(choices[-1] if choices else None),
91
  )
@@ -99,14 +138,9 @@ def upload_training_images(files):
99
 
100
  for file in files:
101
  try:
102
- # Gradio normally supplies a filepath string; also support objects
103
- # exposing a .name attribute.
104
- raw = getattr(file, "name", file)
105
- if not raw:
106
- raise ValueError("Upload has no file path")
107
- src = Path(str(raw))
108
- if not src.exists() or not src.is_file():
109
- raise FileNotFoundError(f"Uploaded file not found: {src}")
110
 
111
  ext = src.suffix.lower() or ".jpg"
112
  if ext not in {".jpg", ".jpeg", ".png", ".webp", ".bmp"}:
@@ -150,7 +184,7 @@ def upload_training_images(files):
150
  else:
151
  msg = "⚠️ No images were saved."
152
  latest = choices[-1] if choices else None
153
- gallery_value = [x["path"] for x in db["images"] if Path(x.get("path", "")).exists()]
154
  annotator_value = (
155
  f'<div class="annotator-shell" data-filename="{html.escape(latest or "", quote=True)}">'
156
  f'{annotation_html(latest)}</div>'
@@ -774,7 +808,7 @@ with gr.Blocks(title="Anything Detector", js=ANNOTATOR_JS) as demo:
774
  )
775
  upload_status = gr.Markdown()
776
  saved_images = gr.Gallery(
777
- value=[x["path"] for x in load_db()["images"] if Path(x.get("path", "")).exists()],
778
  label="Saved training images",
779
  columns=5,
780
  rows=2,
 
75
  )
76
 
77
 
78
+ def gallery_items(db=None):
79
+ """Return real PIL images for Gradio Gallery so saved files are visibly rendered."""
80
+ if db is None:
81
+ db = load_db()
82
+ items = []
83
+ for item in db.get("images", []):
84
+ path = Path(item.get("path", ""))
85
+ try:
86
+ if path.exists() and path.is_file():
87
+ with Image.open(path) as im:
88
+ items.append((im.convert("RGB").copy(), item.get("filename", path.name)))
89
+ except Exception:
90
+ continue
91
+ return items
92
+
93
+
94
+ def uploaded_path(file):
95
+ """Get the actual temporary upload path across Gradio 4/5 FileData formats."""
96
+ if file is None:
97
+ return None
98
+ if isinstance(file, (str, os.PathLike)):
99
+ return Path(file)
100
+ if isinstance(file, dict):
101
+ for key in ("path", "name"):
102
+ value = file.get(key)
103
+ if value:
104
+ candidate = Path(str(value))
105
+ if candidate.exists():
106
+ return candidate
107
+ return None
108
+ for attr in ("path", "name"):
109
+ value = getattr(file, attr, None)
110
+ if value:
111
+ candidate = Path(str(value))
112
+ if candidate.exists():
113
+ return candidate
114
+ return None
115
+
116
+
117
  def upload_training_images(files):
118
+ """Copy uploaded files into dataset storage and immediately refresh every image UI."""
119
  DATA.mkdir(parents=True, exist_ok=True)
120
  IMAGE_DIR.mkdir(parents=True, exist_ok=True)
121
  db = load_db()
 
124
  return (
125
  gr.update(choices=choices, value=choices[-1] if choices else None),
126
  "⚠️ No images selected.",
127
+ gallery_items(db),
128
  gr.update(choices=choices, value=choices[-1] if choices else None),
129
  annotation_html(choices[-1] if choices else None),
130
  )
 
138
 
139
  for file in files:
140
  try:
141
+ src = uploaded_path(file)
142
+ if src is None or not src.is_file():
143
+ raise FileNotFoundError("Could not resolve the uploaded image's temporary file path")
 
 
 
 
 
144
 
145
  ext = src.suffix.lower() or ".jpg"
146
  if ext not in {".jpg", ".jpeg", ".png", ".webp", ".bmp"}:
 
184
  else:
185
  msg = "⚠️ No images were saved."
186
  latest = choices[-1] if choices else None
187
+ gallery_value = gallery_items(db)
188
  annotator_value = (
189
  f'<div class="annotator-shell" data-filename="{html.escape(latest or "", quote=True)}">'
190
  f'{annotation_html(latest)}</div>'
 
808
  )
809
  upload_status = gr.Markdown()
810
  saved_images = gr.Gallery(
811
+ value=gallery_items(),
812
  label="Saved training images",
813
  columns=5,
814
  rows=2,