Spaces:
Running
Running
fix(app): dedent on_tag_image to module level (was accidentally nested in _build_tagger_html, causing NameError)
Browse files
app.py
CHANGED
|
@@ -537,105 +537,105 @@ def _build_tagger_html(result: dict, lc: str) -> str:
|
|
| 537 |
return "\n".join(lines)
|
| 538 |
|
| 539 |
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
return (
|
| 553 |
-
gr.update(value=
|
| 554 |
gr.update(visible=False),
|
| 555 |
empty_chips, empty_chips,
|
| 556 |
gr.update(visible=False),
|
| 557 |
gr.update(visible=False),
|
| 558 |
"", "",
|
| 559 |
) + hidden_control
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
gr.update(visible=False),
|
| 570 |
-
"", "",
|
| 571 |
-
) + hidden_control
|
| 572 |
-
progress(0.35, desc=t("tagger_ratings_label", lc))
|
| 573 |
-
result = get_ensemble_tagger().tag_image(
|
| 574 |
-
image, gen_threshold, char_threshold,
|
| 575 |
-
mode=mode, skip_pose=skip_pose, with_caption=with_caption,
|
| 576 |
-
caption_kind=caption_kind, pose_mode=pose_mode,
|
| 577 |
-
)
|
| 578 |
-
progress(0.9, desc=t("tagger_results", lc))
|
| 579 |
-
if not result["general"] and not result["characters"] and not result.get("nl_caption"):
|
| 580 |
-
html_out = f'<div style="color:#FBBF24;font-size:12px;">{t("tagger_no_tags", lc)}</div>'
|
| 581 |
-
return (
|
| 582 |
-
gr.update(value=html_out),
|
| 583 |
-
gr.update(visible=False),
|
| 584 |
-
empty_chips, empty_chips,
|
| 585 |
-
gr.update(visible=False),
|
| 586 |
-
gr.update(visible=False),
|
| 587 |
-
"", "",
|
| 588 |
-
) + hidden_control
|
| 589 |
-
html_out = _build_tagger_html(result, lc)
|
| 590 |
-
general_names = list(result["general"].keys())[:60]
|
| 591 |
-
char_names = list(result["characters"].keys())
|
| 592 |
-
|
| 593 |
-
# Merge the NL caption into the tag chips so it can be picked up by
|
| 594 |
-
# the same apply/copy pipeline as booru tags. Pose tags are already
|
| 595 |
-
# folded into `general`/`caption`/`taglist` by the ensemble tagger,
|
| 596 |
-
# so they must NOT be added here again.
|
| 597 |
-
extra_names: list[str] = []
|
| 598 |
-
if result.get("nl_caption"):
|
| 599 |
-
extra_names.append(result["nl_caption"].strip())
|
| 600 |
-
|
| 601 |
-
all_names = extra_names + general_names
|
| 602 |
-
general_choices = [(n, n) for n in all_names]
|
| 603 |
-
char_choices = [(n.replace("_", " "), n) for n in char_names]
|
| 604 |
-
|
| 605 |
-
raw_caption = result["caption"]
|
| 606 |
-
esc_taglist = result["taglist"]
|
| 607 |
-
tags_box = esc_taglist if fmt == "prompt" else raw_caption
|
| 608 |
-
|
| 609 |
-
# ControlNet-style preview built from the same keypoints the pose tags
|
| 610 |
-
# came from (pure numpy / PIL, no extra models). Hidden when the pose
|
| 611 |
-
# pass was skipped or the preview kind has nothing to show.
|
| 612 |
-
control_outputs = _render_control_outputs(
|
| 613 |
-
ImageOps.exif_transpose(Image.fromarray(image)),
|
| 614 |
-
result, control_kind,
|
| 615 |
-
)
|
| 616 |
-
|
| 617 |
return (
|
| 618 |
gr.update(value=html_out),
|
| 619 |
-
gr.update(visible=True),
|
| 620 |
-
gr.update(choices=general_choices, value=all_names, visible=True),
|
| 621 |
-
gr.update(choices=char_choices, value=char_names, visible=True),
|
| 622 |
-
gr.update(value=tags_box, visible=True),
|
| 623 |
-
gr.update(visible=True),
|
| 624 |
-
raw_caption,
|
| 625 |
-
esc_taglist,
|
| 626 |
-
) + control_outputs
|
| 627 |
-
except Exception as exc:
|
| 628 |
-
import traceback
|
| 629 |
-
traceback.print_exc()
|
| 630 |
-
err = t("tagger_error", lc).format(exc=html.escape(str(exc)))
|
| 631 |
-
return (
|
| 632 |
-
gr.update(value=f'<div style="color:#F87171;font-size:12px;">{err}</div>'),
|
| 633 |
gr.update(visible=False),
|
| 634 |
empty_chips, empty_chips,
|
| 635 |
gr.update(visible=False),
|
| 636 |
gr.update(visible=False),
|
| 637 |
"", "",
|
| 638 |
) + hidden_control
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 639 |
|
| 640 |
|
| 641 |
def _render_control_outputs(orig_pil, result, control_kind):
|
|
|
|
| 537 |
return "\n".join(lines)
|
| 538 |
|
| 539 |
|
| 540 |
+
def on_tag_image(image, gen_threshold, char_threshold, fmt, lang, progress=gr.Progress(), skip_pose=False, with_caption=False, caption_kind="florence", control_kind="skeleton_black", pose_mode="yolo"):
|
| 541 |
+
lc = "ru" if lang == "RU" else "en"
|
| 542 |
+
# fmt carries "prompt|<mode>" from the tagger-mode dropdown: "prompt|wd:eva02", "prompt|ensemble".
|
| 543 |
+
if "|" in fmt:
|
| 544 |
+
fmt, mode = fmt.split("|", 1)
|
| 545 |
+
fmt = fmt.strip() or "prompt"
|
| 546 |
+
mode = mode.strip() or "ensemble"
|
| 547 |
+
else:
|
| 548 |
+
mode = "ensemble"
|
| 549 |
+
empty_chips = gr.update(choices=[], value=[], visible=False)
|
| 550 |
+
hidden_control = (gr.update(visible=False), gr.update(visible=False))
|
| 551 |
+
if image is None:
|
| 552 |
+
return (
|
| 553 |
+
gr.update(value=f'<div style="color:#F87171;font-size:12px;">{t("tagger_no_image", lc)}</div>'),
|
| 554 |
+
gr.update(visible=False),
|
| 555 |
+
empty_chips, empty_chips,
|
| 556 |
+
gr.update(visible=False),
|
| 557 |
+
gr.update(visible=False),
|
| 558 |
+
"", "",
|
| 559 |
+
) + hidden_control
|
| 560 |
+
try:
|
| 561 |
+
progress(0, desc=t("tagger_processing", lc))
|
| 562 |
+
if not _TAGGER_DEPS_OK:
|
| 563 |
+
msg = f'<div style="color:#FBBF24;font-size:13px;line-height:1.5;">{t("tagger_unavailable", lc)}</div>'
|
| 564 |
return (
|
| 565 |
+
gr.update(value=msg),
|
| 566 |
gr.update(visible=False),
|
| 567 |
empty_chips, empty_chips,
|
| 568 |
gr.update(visible=False),
|
| 569 |
gr.update(visible=False),
|
| 570 |
"", "",
|
| 571 |
) + hidden_control
|
| 572 |
+
progress(0.35, desc=t("tagger_ratings_label", lc))
|
| 573 |
+
result = get_ensemble_tagger().tag_image(
|
| 574 |
+
image, gen_threshold, char_threshold,
|
| 575 |
+
mode=mode, skip_pose=skip_pose, with_caption=with_caption,
|
| 576 |
+
caption_kind=caption_kind, pose_mode=pose_mode,
|
| 577 |
+
)
|
| 578 |
+
progress(0.9, desc=t("tagger_results", lc))
|
| 579 |
+
if not result["general"] and not result["characters"] and not result.get("nl_caption"):
|
| 580 |
+
html_out = f'<div style="color:#FBBF24;font-size:12px;">{t("tagger_no_tags", lc)}</div>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
return (
|
| 582 |
gr.update(value=html_out),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 583 |
gr.update(visible=False),
|
| 584 |
empty_chips, empty_chips,
|
| 585 |
gr.update(visible=False),
|
| 586 |
gr.update(visible=False),
|
| 587 |
"", "",
|
| 588 |
) + hidden_control
|
| 589 |
+
html_out = _build_tagger_html(result, lc)
|
| 590 |
+
general_names = list(result["general"].keys())[:60]
|
| 591 |
+
char_names = list(result["characters"].keys())
|
| 592 |
+
|
| 593 |
+
# Merge the NL caption into the tag chips so it can be picked up by
|
| 594 |
+
# the same apply/copy pipeline as booru tags. Pose tags are already
|
| 595 |
+
# folded into `general`/`caption`/`taglist` by the ensemble tagger,
|
| 596 |
+
# so they must NOT be added here again.
|
| 597 |
+
extra_names: list[str] = []
|
| 598 |
+
if result.get("nl_caption"):
|
| 599 |
+
extra_names.append(result["nl_caption"].strip())
|
| 600 |
+
|
| 601 |
+
all_names = extra_names + general_names
|
| 602 |
+
general_choices = [(n, n) for n in all_names]
|
| 603 |
+
char_choices = [(n.replace("_", " "), n) for n in char_names]
|
| 604 |
+
|
| 605 |
+
raw_caption = result["caption"]
|
| 606 |
+
esc_taglist = result["taglist"]
|
| 607 |
+
tags_box = esc_taglist if fmt == "prompt" else raw_caption
|
| 608 |
+
|
| 609 |
+
# ControlNet-style preview built from the same keypoints the pose tags
|
| 610 |
+
# came from (pure numpy / PIL, no extra models). Hidden when the pose
|
| 611 |
+
# pass was skipped or the preview kind has nothing to show.
|
| 612 |
+
control_outputs = _render_control_outputs(
|
| 613 |
+
ImageOps.exif_transpose(Image.fromarray(image)),
|
| 614 |
+
result, control_kind,
|
| 615 |
+
)
|
| 616 |
+
|
| 617 |
+
return (
|
| 618 |
+
gr.update(value=html_out),
|
| 619 |
+
gr.update(visible=True),
|
| 620 |
+
gr.update(choices=general_choices, value=all_names, visible=True),
|
| 621 |
+
gr.update(choices=char_choices, value=char_names, visible=True),
|
| 622 |
+
gr.update(value=tags_box, visible=True),
|
| 623 |
+
gr.update(visible=True),
|
| 624 |
+
raw_caption,
|
| 625 |
+
esc_taglist,
|
| 626 |
+
) + control_outputs
|
| 627 |
+
except Exception as exc:
|
| 628 |
+
import traceback
|
| 629 |
+
traceback.print_exc()
|
| 630 |
+
err = t("tagger_error", lc).format(exc=html.escape(str(exc)))
|
| 631 |
+
return (
|
| 632 |
+
gr.update(value=f'<div style="color:#F87171;font-size:12px;">{err}</div>'),
|
| 633 |
+
gr.update(visible=False),
|
| 634 |
+
empty_chips, empty_chips,
|
| 635 |
+
gr.update(visible=False),
|
| 636 |
+
gr.update(visible=False),
|
| 637 |
+
"", "",
|
| 638 |
+
) + hidden_control
|
| 639 |
|
| 640 |
|
| 641 |
def _render_control_outputs(orig_pil, result, control_kind):
|