phonghh commited on
Commit
4a77736
·
1 Parent(s): 2e4a3a4

refactor: collapse app.py to import main from danbooru_deploy

Browse files

All logic (predictor, UI, hub download, model switcher, ZeroGPU) now lives
in the shared danbooru-deploy package. app.py just runs its CPU-forced
entry point.

- app.py reduced to: from danbooru_deploy import main; main(use_cuda=False)
- requirements: install git subdirectory=deploy[hf] (hub extra)
- CPU guaranteed two ways: explicit use_cuda=False + CPU onnxruntime wheel

Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +6 -506
  3. requirements.txt +1 -1
README.md CHANGED
@@ -13,7 +13,7 @@ pinned: false
13
 
14
  ONNX-powered anime image tagger. Upload an image or paste a URL to get predicted Danbooru tags with scores and categories.
15
 
16
- Runs **CPU-only** by default. Core predictor, preprocessing, and category logic come from the shared `danbooru-deploy` package (see [realphongha/DanbooruTagQuery](https://github.com/realphongha/DanbooruTagQuery) `deploy/`).
17
 
18
  ## Local testing
19
 
 
13
 
14
  ONNX-powered anime image tagger. Upload an image or paste a URL to get predicted Danbooru tags with scores and categories.
15
 
16
+ Runs **CPU-only** by default. All logic (predictor, preprocessing, category, UI, hub download, model switcher) lives in the shared `danbooru-deploy` package — this `app.py` only imports its `main()` entry point (see [realphongha/DanbooruTagQuery](https://github.com/realphongha/DanbooruTagQuery) `deploy/`).
17
 
18
  ## Local testing
19
 
app.py CHANGED
@@ -1,511 +1,11 @@
1
  #!/usr/bin/env python3
2
- """
3
- DanbooruTagQuery — Hugging Face ZeroGPU Space
4
 
5
- Usage:
6
- python app.py # download model from HF hub
7
- python app.py /path/to/model.onnx # use local model
8
- MODEL_DIR=/path/to python app.py # env var with model dir
9
  """
10
 
11
- from __future__ import annotations
12
-
13
- import os
14
- import sys
15
- import tempfile
16
- import time
17
- from pathlib import Path
18
- try:
19
- import spaces
20
- except ImportError:
21
- class MockSpaces:
22
- def GPU(self, func=None, **kwargs):
23
- if func is not None and callable(func):
24
- # Used as @spaces.GPU
25
- return func
26
-
27
- # Used as @spaces.GPU(...)
28
- def decorator(f):
29
- return f
30
- return decorator
31
-
32
- spaces = MockSpaces()
33
-
34
- import gradio as gr
35
- import numpy as np
36
- from PIL import Image
37
-
38
- # ── optional deps (loaded on demand) ────────────────────────────────────────
39
-
40
- _hf_hub = None
41
-
42
- def _import_hf_hub():
43
- global _hf_hub
44
- if _hf_hub is None:
45
- import huggingface_hub as h
46
- _hf_hub = h
47
- return _hf_hub
48
-
49
-
50
- # ── constants ───────────────────────────────────────────────────────────────
51
-
52
- HF_REPO = "realphongha/danbooru-tag-query"
53
- MODELS_DIR = "models"
54
- CATEGORY_JSON = "tag_category.json"
55
-
56
- DEFAULT_TOP_K = None
57
- DEFAULT_MIN_SCORE = 0.2
58
-
59
- # ── shared core (pip-installed danbooru-deploy) ────────────────────────────
60
-
61
- from danbooru_deploy import Predictor, preprocess
62
- from danbooru_deploy.core import CATEGORY_MAP, get_category_name
63
-
64
- # CPU as default for the HF Space (no GPU expected)
65
- DEFAULT_USE_CUDA = False
66
-
67
-
68
- # ── model discovery & loading (HF hub) ─────────────────────────────────────
69
-
70
- def discover_model_variants() -> list[str]:
71
- try:
72
- hf = _import_hf_hub()
73
- api = hf.HfApi()
74
- siblings = api.list_repo_files(HF_REPO, repo_type="model")
75
- variants: set[str] = set()
76
- for path in siblings:
77
- if path.startswith(f"{MODELS_DIR}/") and "/" in path[len(MODELS_DIR) + 1:]:
78
- variant = path.split("/")[1]
79
- if variant:
80
- variants.add(variant)
81
- return sorted(variants, reverse=True)
82
- except Exception as exc:
83
- print(f"Warning: could not discover models on hub: {exc}")
84
- return []
85
-
86
-
87
- def download_model_variant(variant: str) -> Path:
88
- hf = _import_hf_hub()
89
- onnx_path = hf.hf_hub_download(
90
- repo_id=HF_REPO,
91
- filename=f"{MODELS_DIR}/{variant}/model.onnx",
92
- repo_type="model",
93
- )
94
- # download sidecars (config, tag_to_id, tag_category) if they exist
95
- for sidecar in ["config.json", "tag_to_id.json", CATEGORY_JSON]:
96
- try:
97
- hf.hf_hub_download(
98
- repo_id=HF_REPO,
99
- filename=f"{MODELS_DIR}/{variant}/{sidecar}",
100
- repo_type="model",
101
- )
102
- except Exception:
103
- pass # optional — missing is fine
104
- return Path(onnx_path)
105
-
106
-
107
- # ── Gradio UI ──────────────────────────────────────────────────────────────
108
-
109
- def build_app(predict_fn, model_choices: list[str]) -> gr.Blocks:
110
- state = {
111
- "all_logits": None,
112
- "tag_metadata": None,
113
- "current_image": None,
114
- "predictor": None,
115
- "predict_fn": predict_fn,
116
- }
117
-
118
- css = """
119
- #csv-wrap { position: relative; }
120
- #copy-csv-btn { position: absolute; top: 4px; right: 4px; z-index: 10;
121
- min-width: 0; padding: 0 6px; height: 24px;
122
- font-size: 13px; line-height: 24px; }
123
- """
124
-
125
- category_names = sorted(CATEGORY_MAP.values())
126
-
127
- with gr.Blocks(title="DanbooruTagQuery", theme=gr.themes.Soft(), css=css) as app:
128
- gr.Markdown("# 🏷️ DanbooruTagQuery")
129
-
130
- with gr.Row():
131
- with gr.Column(scale=1):
132
- image_input = gr.Image(
133
- label="Image",
134
- type="pil",
135
- sources=["upload", "clipboard"],
136
- height=300,
137
- )
138
- url_input = gr.Textbox(
139
- label="Image URL",
140
- placeholder="Paste image URL and press Enter",
141
- )
142
- with gr.Row():
143
- analyze_btn = gr.Button("🔍 Analyze", variant="primary", scale=2)
144
- clear_btn = gr.Button("🗑️ Clear", scale=1)
145
- with gr.Column():
146
- gr.Markdown('[📄 **Model Card**](https://huggingface.co/realphongha/danbooru-tag-query)')
147
-
148
- gr.Markdown("### 🤖 Model")
149
- model_dropdown = gr.Dropdown(
150
- choices=model_choices,
151
- value=model_choices[0] if model_choices else None,
152
- label="Model variant",
153
- interactive=True,
154
- )
155
- model_status = gr.Markdown("Ready")
156
-
157
- with gr.Column(scale=1):
158
- top_k = gr.Number(
159
- label="Top-K", value=DEFAULT_TOP_K, minimum=0, step=1
160
- )
161
- min_score = gr.Slider(
162
- label="Min Score",
163
- value=DEFAULT_MIN_SCORE,
164
- minimum=0.0,
165
- maximum=1.0,
166
- step=0.01,
167
- )
168
- sort_by = gr.Radio(
169
- label="Sort by", choices=["score", "name"], value="score"
170
- )
171
- use_underscore = gr.Checkbox(
172
- label="Use underscore (_)", value=False
173
- )
174
- categories = gr.CheckboxGroup(
175
- label="Categories",
176
- choices=category_names,
177
- value=["general"],
178
- )
179
-
180
- with gr.Tabs():
181
- with gr.TabItem("📋 Tag list"):
182
- tag_table = gr.HTML(label="Tags")
183
- with gr.TabItem("📝 Comma-separated"):
184
- with gr.Column(elem_id="csv-wrap"):
185
- tag_string = gr.Textbox(label="Tags", lines=6, elem_id="csv-text")
186
- copy_btn = gr.Button("📋", elem_id="copy-csv-btn")
187
-
188
- with gr.Row():
189
- status = gr.Markdown("Ready. Load an image and click **Analyze**.")
190
-
191
- gr.Markdown("### 🔍 Tag score query")
192
- with gr.Row():
193
- tag_query = gr.Textbox(
194
- label="Search tag",
195
- placeholder="Type to search…",
196
- scale=3,
197
- )
198
- tag_query_output = gr.HTML(label="Results")
199
-
200
- # ── callbacks ──────────────────────────────────────────────────────
201
-
202
- def refresh_results(
203
- _top_k, _min_score, _sort_by, _use_underscore, _categories,
204
- ):
205
- if state["all_logits"] is None or state["tag_metadata"] is None:
206
- return "<i>No results yet.</i>", ""
207
-
208
- meta = state["tag_metadata"]
209
- all_tags = list(meta.keys())
210
- if _categories:
211
- all_tags = [
212
- t for t in all_tags
213
- if meta[t]["category_name"] in _categories
214
- ]
215
-
216
- items = [(t, meta[t]["score"]) for t in all_tags]
217
- if _sort_by == "name":
218
- items.sort(key=lambda x: format_tag(x[0], _use_underscore))
219
- else:
220
- items.sort(key=lambda x: x[1], reverse=True)
221
-
222
- items = [(t, s) for t, s in items if s >= _min_score]
223
- if _top_k is not None and _top_k > 0:
224
- items = items[:_top_k]
225
-
226
- if not items:
227
- return "<i>No tags pass the filters.</i>", ""
228
-
229
- rows = []
230
- for tag, score in items:
231
- m = meta[tag]
232
- link = (
233
- f'<a href="https://danbooru.donmai.us/posts?tags={tag}"'
234
- f' target="_blank">{tag}</a>'
235
- )
236
- display = format_tag(tag, _use_underscore)
237
- rows.append(
238
- f"<tr>"
239
- f"<td>{link}</td>"
240
- f"<td>{display}</td>"
241
- f"<td style='text-align:right'>{score:.4f}</td>"
242
- f"<td><code>{m['category_name']}</code></td>"
243
- f"</tr>"
244
- )
245
- table = (
246
- '<table style="width:100%">'
247
- '<thead><tr>'
248
- '<th>Link</th><th>Tag</th>'
249
- '<th style="text-align:right">Score</th>'
250
- '<th>Category</th>'
251
- '</tr></thead>'
252
- '<tbody>' + "".join(rows) + '</tbody></table>'
253
- )
254
-
255
- csv = ", ".join(format_tag(t, _use_underscore) for t, _ in items)
256
- return table, csv
257
-
258
- def on_analyze(image, url):
259
- if image is None and not url:
260
- return *refresh_results(
261
- top_k.value, min_score.value,
262
- sort_by.value, use_underscore.value,
263
- categories.value,
264
- ), "⚠️ No image loaded."
265
-
266
- pil = image
267
- if pil is None and url:
268
- import requests as std_requests
269
- try:
270
- resp = std_requests.get(url, timeout=30)
271
- resp.raise_for_status()
272
- tmp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
273
- tmp.write(resp.content)
274
- tmp.close()
275
- pil = Image.open(tmp.name).convert("RGB")
276
- except Exception as exc:
277
- return "<i>Error loading URL.</i>", "", f"❌ {exc}"
278
-
279
- state["current_image"] = pil
280
- fn = state["predict_fn"]
281
- if fn is None:
282
- return "<i>No model loaded.</i>", "", "❌ No model loaded."
283
-
284
- t0 = time.time()
285
- all_logits = fn(pil)
286
- state["all_logits"] = all_logits
287
- state["tag_metadata"] = enrich_tags(
288
- all_logits, state["predictor"].cat_map if state["predictor"] else {}
289
- )
290
-
291
- table, csv = refresh_results(
292
- top_k.value, min_score.value,
293
- sort_by.value, use_underscore.value,
294
- categories.value,
295
- )
296
- elapsed = time.time() - t0
297
- n = len(state["tag_metadata"])
298
- return table, csv, f"✅ {n} tags · {elapsed:.2f}s"
299
-
300
- analyze_btn.click(
301
- fn=on_analyze,
302
- inputs=[image_input, url_input],
303
- outputs=[tag_table, tag_string, status],
304
- )
305
-
306
- url_input.submit(
307
- fn=on_analyze,
308
- inputs=[image_input, url_input],
309
- outputs=[tag_table, tag_string, status],
310
- )
311
-
312
- def on_clear():
313
- state["all_logits"] = None
314
- state["tag_metadata"] = None
315
- state["current_image"] = None
316
- return None, "", "<i>No results yet.</i>", "Cleared.", "", "", "<i>No results yet.</i>"
317
-
318
- clear_btn.click(
319
- fn=on_clear,
320
- inputs=[],
321
- outputs=[image_input, url_input, tag_table, tag_string,
322
- status, tag_query, tag_query_output],
323
- )
324
-
325
- for widget in [top_k, min_score, sort_by, use_underscore, categories]:
326
- widget.change(
327
- fn=refresh_results,
328
- inputs=[top_k, min_score, sort_by, use_underscore, categories],
329
- outputs=[tag_table, tag_string],
330
- )
331
-
332
- def query_tag_score(query):
333
- meta = state.get("tag_metadata")
334
- if not meta or not query:
335
- return ""
336
- query_l = query.lower()
337
- matches = sorted(
338
- [(t, meta[t]["score"]) for t in meta if query_l in t.lower()],
339
- key=lambda x: x[1], reverse=True,
340
- )[:20]
341
- if not matches:
342
- return "<i>No matching tags.</i>"
343
- rows = "".join(
344
- f"<tr><td>{t}</td><td>{s:.4f}</td>"
345
- f"<td><code>{meta[t]['category_name']}</code></td></tr>"
346
- for t, s in matches
347
- )
348
- return (f"<table style='width:100%'>"
349
- f"<tr><th>Tag</th><th>Score</th><th>Category</th></tr>"
350
- f"{rows}</table>")
351
-
352
- tag_query.change(
353
- fn=query_tag_score,
354
- inputs=[tag_query],
355
- outputs=[tag_query_output],
356
- )
357
-
358
- copy_btn.click(
359
- fn=lambda: None,
360
- inputs=[],
361
- outputs=[],
362
- js="""() => {
363
- const tb = document.querySelector('#csv-text textarea');
364
- if (tb) { navigator.clipboard.writeText(tb.value); }
365
- }"""
366
- )
367
-
368
- # HF ZeroGPU requires @spaces.GPU somewhere in code
369
- gpu_hidden_state = gr.State(value=None)
370
-
371
- @spaces.GPU
372
- def _gpu_dummy():
373
- return None
374
-
375
- gpu_hidden_state.change(fn=_gpu_dummy, inputs=[gpu_hidden_state], outputs=[gpu_hidden_state])
376
-
377
-
378
- # ── model switcher ────────────────────────────────────────────────
379
-
380
- def on_model_change(variant):
381
- if not variant:
382
- return "⚠️ No model selected"
383
- try:
384
- onnx_path = download_model_variant(variant)
385
- predictor = Predictor(onnx_path, use_cuda=DEFAULT_USE_CUDA)
386
- state["predictor"] = predictor
387
-
388
- def new_predict_fn(image: Image.Image) -> list[tuple[str, float]]:
389
- tensor = preprocess(image, predictor.image_size)
390
- logits = predictor.run(tensor)[0]
391
- inv = {v: k for k, v in predictor.tag_to_id.items()}
392
- indices = np.argsort(logits)[::-1]
393
- return [(inv[int(i)], float(logits[i])) for i in indices]
394
-
395
- state["predict_fn"] = new_predict_fn
396
- state["all_logits"] = None
397
- state["tag_metadata"] = None
398
- return f"✅ Switched to {variant} ({predictor.num_classes} tags)"
399
- except Exception as exc:
400
- return f"❌ Failed to load model: {exc}"
401
-
402
- model_dropdown.change(
403
- fn=on_model_change,
404
- inputs=[model_dropdown],
405
- outputs=[model_status],
406
- )
407
-
408
- return app
409
-
410
-
411
- # ── helpers ────────────────────────────────────────────────────────────────
412
-
413
- def enrich_tags(
414
- tags_scores: list[tuple[str, float]], cat_map: dict[str, int]
415
- ) -> dict[str, dict]:
416
- result: dict[str, dict] = {}
417
- for tag, score in tags_scores:
418
- cat_id = cat_map.get(tag, 0)
419
- result[tag] = {
420
- "score": score,
421
- "category": cat_id,
422
- "category_name": CATEGORY_MAP.get(cat_id, "general"),
423
- }
424
- return result
425
-
426
-
427
- def format_tag(tag: str, use_underscore: bool) -> str:
428
- return tag if use_underscore else tag.replace("_", " ")
429
-
430
-
431
- # ── main ────────────────────────────────────────────────────────────────────
432
-
433
- def main():
434
- model_arg = sys.argv[1] if len(sys.argv) > 1 else None
435
- model_env = os.environ.get("MODEL_DIR")
436
- model_variants: list[str] = []
437
- initial_predict_fn = None
438
-
439
- if model_arg:
440
- onnx = Path(model_arg)
441
- if not onnx.exists():
442
- print(f"Error: {onnx} not found", file=sys.stderr)
443
- sys.exit(1)
444
- print(f"Loading local model: {onnx}")
445
- predictor = Predictor(onnx, use_cuda=DEFAULT_USE_CUDA)
446
-
447
- def _predict(image: Image.Image) -> list[tuple[str, float]]:
448
- tensor = preprocess(image, predictor.image_size)
449
- logits = predictor.run(tensor)[0]
450
- inv = {v: k for k, v in predictor.tag_to_id.items()}
451
- indices = np.argsort(logits)[::-1]
452
- return [(inv[int(i)], float(logits[i])) for i in indices]
453
-
454
- initial_predict_fn = _predict
455
- elif model_env:
456
- env_dir = Path(model_env)
457
- if not env_dir.is_dir():
458
- print(f"Error: MODEL_DIR {env_dir} is not a directory", file=sys.stderr)
459
- sys.exit(1)
460
- onnx_files = list(env_dir.glob("*.onnx"))
461
- if not onnx_files:
462
- print(f"Error: no .onnx files in {env_dir}", file=sys.stderr)
463
- sys.exit(1)
464
- onnx = onnx_files[0]
465
- print(f"Loading local model from MODEL_DIR: {onnx}")
466
- predictor = Predictor(onnx, use_cuda=DEFAULT_USE_CUDA)
467
-
468
- def _predict(image: Image.Image) -> list[tuple[str, float]]:
469
- tensor = preprocess(image, predictor.image_size)
470
- logits = predictor.run(tensor)[0]
471
- inv = {v: k for k, v in predictor.tag_to_id.items()}
472
- indices = np.argsort(logits)[::-1]
473
- return [(inv[int(i)], float(logits[i])) for i in indices]
474
-
475
- initial_predict_fn = _predict
476
- else:
477
- print("Discovering model variants on HF hub …")
478
- model_variants = discover_model_variants()
479
- if not model_variants:
480
- print("Warning: no models found on hub.")
481
- else:
482
- print(f"Found variants: {model_variants}")
483
- default = model_variants[0]
484
- print(f"Downloading default model: {default} …")
485
- try:
486
- onnx_path = download_model_variant(default)
487
- predictor = Predictor(onnx_path, use_cuda=DEFAULT_USE_CUDA)
488
-
489
- def _predict(image: Image.Image) -> list[tuple[str, float]]:
490
- tensor = preprocess(image, predictor.image_size)
491
- logits = predictor.run(tensor)[0]
492
- inv = {v: k for k, v in predictor.tag_to_id.items()}
493
- indices = np.argsort(logits)[::-1]
494
- return [(inv[int(i)], float(logits[i])) for i in indices]
495
-
496
- initial_predict_fn = _predict
497
- print(f"Loaded {default} ({predictor.num_classes} tags)")
498
- except Exception as exc:
499
- print(f"Error loading default model: {exc}")
500
-
501
- app = build_app(initial_predict_fn, model_variants)
502
-
503
- host = os.environ.get("GRADIO_SERVER_NAME") or os.environ.get("HOST")
504
- port_str = os.environ.get("GRADIO_SERVER_PORT") or os.environ.get("PORT")
505
- port = int(port_str) if port_str else None
506
-
507
- app.launch(server_name=host, server_port=port, ssr_mode=False)
508
-
509
 
510
- if __name__ == "__main__":
511
- main()
 
1
  #!/usr/bin/env python3
2
+ """DanbooruTagQuery Hugging Face Space.
 
3
 
4
+ All logic lives in the shared `danbooru_deploy` package (pip-installed from
5
+ realphongha/DanbooruTagQuery `deploy/`). This file only imports and runs its
6
+ single entry point, forced to CPU (the Space has no GPU).
 
7
  """
8
 
9
+ from danbooru_deploy import main
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ main(use_cuda=False)
 
requirements.txt CHANGED
@@ -1,5 +1,5 @@
1
  onnxruntime==1.18.0
2
- git+https://github.com/realphongha/DanbooruTagQuery.git#subdirectory=deploy
3
  gradio>=5
4
  numpy==1.26.4
5
  pillow
 
1
  onnxruntime==1.18.0
2
+ git+https://github.com/realphongha/DanbooruTagQuery.git#subdirectory=deploy[hf]
3
  gradio>=5
4
  numpy==1.26.4
5
  pillow