Plana-Archive commited on
Commit
b9c351f
·
verified ·
1 Parent(s): 88e513b

Upload wd-tagger-heatmap-more-models/app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. wd-tagger-heatmap-more-models/app.py +157 -0
wd-tagger-heatmap-more-models/app.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from os import getenv
2
+ from pathlib import Path
3
+
4
+ import gradio as gr
5
+ from PIL import Image
6
+ from rich.traceback import install as traceback_install
7
+
8
+ from tagger.common import Heatmap, ImageLabels, LabelData, load_labels_hf, preprocess_image
9
+ from tagger.model import load_model_and_transform, process_heatmap
10
+
11
+ TITLE = "WD Tagger Heatmap For More Models"
12
+ DESCRIPTION = """WD Tagger v3 Heatmap Generator."""
13
+ # get HF token
14
+ HF_TOKEN = getenv("HF_TOKEN", None)
15
+
16
+ # model repo and cache
17
+ AVAILABLE_MODEL_REPOS = [
18
+ 'SmilingWolf/wd-convnext-tagger-v3',
19
+ 'SmilingWolf/wd-swinv2-tagger-v3',
20
+ 'SmilingWolf/wd-vit-tagger-v3',
21
+ 'SmilingWolf/wd-vit-large-tagger-v3',
22
+ "SmilingWolf/wd-eva02-large-tagger-v3",
23
+ ]
24
+ MODEL_REPO = "SmilingWolf/wd-vit-tagger-v3"
25
+ # get the repo root (or the current working directory if running in ipython)
26
+ WORK_DIR = Path(__file__).parent.resolve() if "__file__" in globals() else Path().resolve()
27
+ # allowed extensions
28
+ IMAGE_EXTENSIONS = [".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".tiff", ".tif"]
29
+
30
+ _ = traceback_install(show_locals=True, locals_max_length=0)
31
+
32
+ # get the example images
33
+ example_images = sorted(
34
+ [
35
+ str(x.relative_to(WORK_DIR))
36
+ for x in WORK_DIR.joinpath("examples").iterdir()
37
+ if x.is_file() and x.suffix.lower() in IMAGE_EXTENSIONS
38
+ ]
39
+ )
40
+
41
+
42
+ def predict(
43
+ image: Image.Image,
44
+ model_repo: str,
45
+ threshold: float = 0.5,
46
+ ):
47
+ # join variant for cache key
48
+ model, transform = load_model_and_transform(model_repo)
49
+ # load labels
50
+ labels: LabelData = load_labels_hf(model_repo)
51
+ # preprocess image
52
+ image = preprocess_image(image, (448, 448))
53
+ image = transform(image).unsqueeze(0)
54
+
55
+ # get the model output
56
+ heatmaps: list[Heatmap]
57
+ image_labels: ImageLabels
58
+ heatmaps, heatmap_grid, image_labels = process_heatmap(model, image, labels, threshold)
59
+
60
+ heatmap_images = [(x.image, x.label) for x in heatmaps]
61
+
62
+ return (
63
+ heatmap_images,
64
+ heatmap_grid,
65
+ image_labels.caption,
66
+ image_labels.booru,
67
+ image_labels.rating,
68
+ image_labels.character,
69
+ image_labels.general,
70
+ )
71
+
72
+
73
+ css = """
74
+ #use_mcut, #char_mcut {
75
+ padding-top: var(--scale-3);
76
+ }
77
+ #threshold.dimmed {
78
+ filter: brightness(75%);
79
+ }
80
+ """
81
+
82
+ with gr.Blocks(theme="NoCrypt/miku", analytics_enabled=False, title=TITLE, css=css) as demo:
83
+ with gr.Row(equal_height=False):
84
+ with gr.Column(min_width=720):
85
+ with gr.Group():
86
+ img_input = gr.Image(
87
+ label="Input",
88
+ type="pil",
89
+ image_mode="RGB",
90
+ sources=["upload", "clipboard"],
91
+ )
92
+ with gr.Group():
93
+ with gr.Row():
94
+ threshold = gr.Slider(
95
+ minimum=0.0,
96
+ maximum=1.0,
97
+ value=0.35,
98
+ step=0.01,
99
+ label="Tag Threshold",
100
+ scale=5,
101
+ elem_id="threshold",
102
+ )
103
+ model_to_use = gr.Dropdown(
104
+ choices=AVAILABLE_MODEL_REPOS,
105
+ value=MODEL_REPO,
106
+ )
107
+ with gr.Row():
108
+ clear = gr.ClearButton(
109
+ components=[],
110
+ variant="secondary",
111
+ size="lg",
112
+ )
113
+ submit = gr.Button(value="Submit", variant="primary", size="lg")
114
+
115
+ with gr.Column(min_width=720):
116
+ with gr.Tab(label="Heatmaps"):
117
+ heatmap_gallery = gr.Gallery(columns=3, show_label=False)
118
+ with gr.Tab(label="Grid"):
119
+ heatmap_grid = gr.Image(show_label=False)
120
+ with gr.Tab(label="Tags"):
121
+ with gr.Group():
122
+ caption = gr.Textbox(label="Caption", show_copy_button=True)
123
+ tags = gr.Textbox(label="Tags", show_copy_button=True)
124
+ with gr.Group():
125
+ rating = gr.Label(label="Rating")
126
+ with gr.Group():
127
+ character = gr.Label(label="Character")
128
+ with gr.Group():
129
+ general = gr.Label(label="General")
130
+
131
+ with gr.Row():
132
+ examples = [[imgpath, MODEL_REPO, 0.35] for imgpath in example_images]
133
+ examples = gr.Examples(
134
+ examples=examples,
135
+ inputs=[img_input, model_to_use, threshold],
136
+ )
137
+
138
+ # tell clear button which components to clear
139
+ clear.add([img_input, heatmap_gallery, heatmap_grid, caption, tags, rating, character, general])
140
+
141
+ submit.click(
142
+ predict,
143
+ inputs=[img_input, model_to_use, threshold],
144
+ outputs=[heatmap_gallery, heatmap_grid, caption, tags, rating, character, general],
145
+ api_name="predict",
146
+ )
147
+
148
+ if __name__ == "__main__":
149
+ demo.queue(max_size=10)
150
+ if getenv("SPACE_ID", None) is not None:
151
+ demo.launch()
152
+ else:
153
+ demo.launch(
154
+ server_name="0.0.0.0",
155
+ server_port=7871,
156
+ debug=True,
157
+ )