iljung1106 commited on
Commit
934245f
·
1 Parent(s): e918622

Stabilize added artist tabs

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +24 -9
  3. requirements.txt +1 -1
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🎨
4
  colorFrom: red
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 6.13.0
8
  app_file: app.py
9
  pinned: false
10
  license: other
 
4
  colorFrom: red
5
  colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 4.44.1
8
  app_file: app.py
9
  pinned: false
10
  license: other
app.py CHANGED
@@ -31,6 +31,7 @@ ROOT = Path(__file__).resolve().parent
31
  STORE_ROOT = Path(os.environ.get("CUSTOM_STORE_DIR") or ("/data/custom_store" if Path("/data").exists() else ROOT / "custom_store"))
32
  INDEX_PATH = STORE_ROOT / "index.json"
33
  ARTISTS_DIR = STORE_ROOT / "artists"
 
34
 
35
 
36
  def _env_path(name: str, default: Path) -> str:
@@ -38,13 +39,20 @@ def _env_path(name: str, default: Path) -> str:
38
 
39
 
40
  def _ensure_store() -> None:
 
41
  ARTISTS_DIR.mkdir(parents=True, exist_ok=True)
42
  if not INDEX_PATH.exists():
43
  INDEX_PATH.write_text(json.dumps({"schema_version": 1, "artists": []}, indent=2), encoding="utf-8")
 
44
 
45
 
46
  def _load_index() -> dict:
47
- _ensure_store()
 
 
 
 
 
48
  try:
49
  payload = json.loads(INDEX_PATH.read_text(encoding="utf-8"))
50
  except Exception:
@@ -55,7 +63,10 @@ def _load_index() -> dict:
55
 
56
 
57
  def _save_index(payload: dict) -> None:
58
- _ensure_store()
 
 
 
59
  tmp = INDEX_PATH.with_suffix(".tmp")
60
  tmp.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
61
  tmp.replace(INDEX_PATH)
@@ -306,8 +317,11 @@ def add_artist(display_name: str, files: List[object], use_tta: bool, device_nam
306
  "prototype_descriptors": prototype_descriptors,
307
  "crop_statuses": statuses,
308
  }
309
- _ensure_store()
310
- torch.save(payload, STORE_ROOT / relative_path)
 
 
 
311
 
312
  index = _load_index()
313
  index["artists"].append(
@@ -325,18 +339,19 @@ def add_artist(display_name: str, files: List[object], use_tta: bool, device_nam
325
  return f"Added {display_name}. Refresh the Manage tab to see it."
326
 
327
 
328
- def added_artist_choices() -> gr.Dropdown:
329
  index = _load_index()
330
  choices = [
331
  (f"{item['display_name']} ({item['artist_id'][:8]})", item["artist_id"])
332
  for item in index.get("artists", [])
333
  if item.get("enabled", True)
334
  ]
335
- return gr.Dropdown(choices=choices, value=choices[0][1] if choices else None)
336
 
337
 
338
  def refresh_added_artists():
339
- return added_artist_choices(), "List refreshed."
 
340
 
341
 
342
  def delete_artist(artist_id: str):
@@ -402,8 +417,8 @@ def build_app() -> gr.Blocks:
402
  refresh_button.click(refresh_added_artists, outputs=[artist_select, manage_status])
403
  delete_button.click(delete_artist, inputs=[artist_select], outputs=[artist_select, manage_status])
404
  demo.load(refresh_added_artists, outputs=[artist_select, manage_status])
405
- return demo
406
 
407
 
408
  if __name__ == "__main__":
409
- build_app().launch()
 
31
  STORE_ROOT = Path(os.environ.get("CUSTOM_STORE_DIR") or ("/data/custom_store" if Path("/data").exists() else ROOT / "custom_store"))
32
  INDEX_PATH = STORE_ROOT / "index.json"
33
  ARTISTS_DIR = STORE_ROOT / "artists"
34
+ STORE_ERROR: Optional[str] = None
35
 
36
 
37
  def _env_path(name: str, default: Path) -> str:
 
39
 
40
 
41
  def _ensure_store() -> None:
42
+ global STORE_ERROR
43
  ARTISTS_DIR.mkdir(parents=True, exist_ok=True)
44
  if not INDEX_PATH.exists():
45
  INDEX_PATH.write_text(json.dumps({"schema_version": 1, "artists": []}, indent=2), encoding="utf-8")
46
+ STORE_ERROR = None
47
 
48
 
49
  def _load_index() -> dict:
50
+ try:
51
+ _ensure_store()
52
+ except Exception as exc:
53
+ global STORE_ERROR
54
+ STORE_ERROR = f"Added-artist storage is not writable: {exc}"
55
+ return {"schema_version": 1, "artists": []}
56
  try:
57
  payload = json.loads(INDEX_PATH.read_text(encoding="utf-8"))
58
  except Exception:
 
63
 
64
 
65
  def _save_index(payload: dict) -> None:
66
+ try:
67
+ _ensure_store()
68
+ except Exception as exc:
69
+ raise gr.Error(f"Added-artist storage is not writable: {exc}") from exc
70
  tmp = INDEX_PATH.with_suffix(".tmp")
71
  tmp.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
72
  tmp.replace(INDEX_PATH)
 
317
  "prototype_descriptors": prototype_descriptors,
318
  "crop_statuses": statuses,
319
  }
320
+ try:
321
+ _ensure_store()
322
+ torch.save(payload, STORE_ROOT / relative_path)
323
+ except Exception as exc:
324
+ raise gr.Error(f"Failed to save added artist. Check persistent storage: {exc}") from exc
325
 
326
  index = _load_index()
327
  index["artists"].append(
 
339
  return f"Added {display_name}. Refresh the Manage tab to see it."
340
 
341
 
342
+ def added_artist_choices():
343
  index = _load_index()
344
  choices = [
345
  (f"{item['display_name']} ({item['artist_id'][:8]})", item["artist_id"])
346
  for item in index.get("artists", [])
347
  if item.get("enabled", True)
348
  ]
349
+ return gr.update(choices=choices, value=choices[0][1] if choices else None)
350
 
351
 
352
  def refresh_added_artists():
353
+ status = STORE_ERROR or "List refreshed."
354
+ return added_artist_choices(), status
355
 
356
 
357
  def delete_artist(artist_id: str):
 
417
  refresh_button.click(refresh_added_artists, outputs=[artist_select, manage_status])
418
  delete_button.click(delete_artist, inputs=[artist_select], outputs=[artist_select, manage_status])
419
  demo.load(refresh_added_artists, outputs=[artist_select, manage_status])
420
+ return demo.queue()
421
 
422
 
423
  if __name__ == "__main__":
424
+ build_app().launch(show_error=True)
requirements.txt CHANGED
@@ -2,7 +2,7 @@ torch
2
  torchvision
3
  pillow
4
  numpy
5
- gradio
6
  spaces
7
  opencv-python
8
  matplotlib
 
2
  torchvision
3
  pillow
4
  numpy
5
+ gradio==4.44.1
6
  spaces
7
  opencv-python
8
  matplotlib