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

Upload 8 files

Browse files
Files changed (1) hide show
  1. app.py +19 -33
app.py CHANGED
@@ -75,47 +75,32 @@ def image_record(filename):
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()
@@ -138,9 +123,9 @@ def upload_training_images(files):
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"}:
@@ -808,7 +793,7 @@ with gr.Blocks(title="Anything Detector", js=ANNOTATOR_JS) as demo:
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,
@@ -970,4 +955,5 @@ if __name__ == "__main__":
970
  server_port=7860,
971
  ssr_mode=False,
972
  max_file_size="100mb",
 
973
  )
 
75
  )
76
 
77
 
78
+ def gallery_items(db):
79
+ """Return Gradio Gallery items using absolute paths plus captions."""
 
 
80
  items = []
81
  for item in db.get("images", []):
82
  path = Path(item.get("path", ""))
83
+ if path.is_file():
84
+ items.append((str(path), item.get("filename", path.name)))
 
 
 
 
85
  return items
86
 
87
 
88
+ def normalize_uploaded_file(file):
89
+ """Get a real local path from Gradio filepath/FileData variants."""
 
 
90
  if isinstance(file, (str, os.PathLike)):
91
  return Path(file)
92
  if isinstance(file, dict):
93
+ value = file.get("path") or file.get("name") or file.get("url")
94
+ if value and not str(value).startswith(("http://", "https://")):
95
+ return Path(str(value))
96
+ value = getattr(file, "path", None) or getattr(file, "name", None)
97
+ if value:
98
+ return Path(str(value))
99
+ raise ValueError(f"Unsupported upload object: {type(file).__name__}")
 
 
 
 
 
 
 
100
 
101
 
102
  def upload_training_images(files):
103
+ """Copy uploaded files into persistent dataset storage and register them."""
104
  DATA.mkdir(parents=True, exist_ok=True)
105
  IMAGE_DIR.mkdir(parents=True, exist_ok=True)
106
  db = load_db()
 
123
 
124
  for file in files:
125
  try:
126
+ src = normalize_uploaded_file(file)
127
+ if not src.exists() or not src.is_file():
128
+ raise FileNotFoundError(f"Uploaded file not found: {src}")
129
 
130
  ext = src.suffix.lower() or ".jpg"
131
  if ext not in {".jpg", ".jpeg", ".png", ".webp", ".bmp"}:
 
793
  )
794
  upload_status = gr.Markdown()
795
  saved_images = gr.Gallery(
796
+ value=gallery_items(load_db()),
797
  label="Saved training images",
798
  columns=5,
799
  rows=2,
 
955
  server_port=7860,
956
  ssr_mode=False,
957
  max_file_size="100mb",
958
+ allowed_paths=[str(DATA), str(IMAGE_DIR)],
959
  )