Spaces:
Running
on
Zero
Running
on
Zero
File size: 8,079 Bytes
cf72c4b b857620 cf72c4b b857620 cf72c4b 987024c b783233 987024c 6994082 cf72c4b a1db0e9 cf72c4b a1db0e9 cf72c4b a1db0e9 cf72c4b 987024c cf72c4b b857620 cf72c4b a1db0e9 b857620 cf72c4b 5928cde 920dd99 9fa17f1 920dd99 5928cde cf72c4b b857620 cf72c4b 5928cde cf72c4b a1db0e9 bb112fb b857620 a1db0e9 b857620 cf72c4b b857620 cf72c4b b857620 cf72c4b 5928cde 6994082 5928cde a1db0e9 4557488 5928cde bb112fb a1db0e9 cf72c4b b857620 cf72c4b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
from typing import Callable
from PIL import Image
import gradio as gr
from v2 import V2UI
from diffusion import ImageGenerator, image_generation_config_ui
from output import UpsamplingOutput
from utils import (
PEOPLE_TAGS,
gradio_copy_text,
COPY_ACTION_JS,
)
NORMALIZE_RATING_TAG = {
"sfw": "",
"general": "",
"sensitive": "sensitive",
"nsfw": "nsfw",
"questionable": "nsfw",
"explicit": "nsfw, explicit",
}
def example(
copyright: str,
character: str,
general: str,
rating: str,
aspect_ratio: str,
length: str,
identity: str,
image_size: str,
):
return [
copyright,
character,
general,
rating,
aspect_ratio,
length,
identity,
image_size,
]
GRADIO_EXAMPLES = [
example(
copyright="original",
character="",
general="1girl, solo, upper body, :d",
rating="general",
aspect_ratio="tall",
length="long",
identity="none",
image_size="768x1344",
),
example(
copyright="original",
character="",
general="1girl, solo, blue theme, limited palette",
rating="sfw",
aspect_ratio="ultra_wide",
length="long",
identity="lax",
image_size="1536x640",
),
example(
copyright="",
character="",
general="4girls",
rating="sfw",
aspect_ratio="tall",
length="very_long",
identity="lax",
image_size="768x1344",
),
example(
copyright="original",
character="",
general="1girl, solo, upper body, looking at viewer, profile picture",
rating="sfw",
aspect_ratio="square",
length="medium",
identity="none",
image_size="1024x1024",
),
example(
copyright="original",
character="",
general="1girl, solo, chibi, upper body, looking at viewer, simple background, limited palette, square",
rating="sfw",
aspect_ratio="square",
length="medium",
identity="none",
image_size="1024x1024",
),
example(
copyright="original",
character="",
general="1girl, full body, aqua eyes, baseball cap, blonde hair, closed mouth, earrings, hat, hoop earrings, jewelry, looking at viewer, shirt, short hair, solo, yellow shirt, simple background, green background",
rating="sfw",
aspect_ratio="tall",
length="very_long",
identity="strict",
image_size="768x1344",
),
example(
copyright="",
character="",
general="no humans, scenery, spring (season)",
rating="general",
aspect_ratio="ultra_wide",
length="medium",
identity="lax",
image_size="1536x640",
),
example(
copyright="",
character="",
general="no humans, cyberpunk, city, cityscape, building, neon lights, pixel art",
rating="general",
aspect_ratio="ultra_wide",
length="medium",
identity="lax",
image_size="1536x640",
),
example(
copyright="sousou no frieren",
character="frieren",
general="1girl, solo",
rating="general",
aspect_ratio="tall",
length="long",
identity="lax",
image_size="768x1344",
),
example(
copyright="honkai: star rail",
character="firefly (honkai: star rail)",
general="1girl, solo",
rating="sfw",
aspect_ratio="tall",
length="medium",
identity="lax",
image_size="768x1344",
),
example(
copyright="honkai: star rail",
character="silver wolf (honkai: star rail)",
general="1girl, solo, annoyed",
rating="sfw",
aspect_ratio="tall",
length="long",
identity="lax",
image_size="768x1344",
),
example(
copyright="chuunibyou demo koi ga shitai!",
character="takanashi rikka",
general="1girl, solo",
rating="sfw",
aspect_ratio="ultra_tall",
length="medium",
identity="lax",
image_size="640x1536",
),
]
def animagine_xl_v3_1(output: UpsamplingOutput):
# separate people tags (e.g. 1girl)
people_tags = []
other_general_tags = []
for tag in output.general_tags.split(","):
tag = tag.strip()
if tag in PEOPLE_TAGS:
people_tags.append(tag)
else:
other_general_tags.append(tag)
return ", ".join(
[
part.strip()
for part in [
*people_tags,
output.character_tags,
output.copyright_tags,
*other_general_tags,
output.upsampled_tags,
NORMALIZE_RATING_TAG[output.rating_tag],
]
if part.strip() != ""
]
)
def elapsed_time_format(elapsed_time: float) -> str:
return f"Elapsed: {elapsed_time:.2f} seconds"
def parse_upsampling_output(
upsampler: Callable[..., UpsamplingOutput],
):
def _parse_upsampling_output(*args) -> tuple[str, str, dict, dict]:
output = upsampler(*args)
print(output)
return (
animagine_xl_v3_1(output),
elapsed_time_format(output.elapsed_time),
gr.update(
interactive=True,
),
gr.update(
interactive=True,
),
)
return _parse_upsampling_output
def description_ui():
gr.Markdown(
"""
# Danbooru Tags Transformer V2 Demo
Models:
- [dart-v2-moe-sft](https://huggingface.co/p1atdev/dart-v2-moe-sft) (Mixtral architecture)
- [dart-v2-sft](https://huggingface.co/p1atdev/dart-v2-sft) (Mistral architecture)
- [Animagine XL v3.1](https://huggingface.co/cagliostrolab/animagine-xl-3.1) (Image generation model)
"""
)
def main():
v2 = V2UI()
print("Loading diffusion model...")
image_generator = ImageGenerator()
print("Loaded.")
with gr.Blocks() as ui:
description_ui()
with gr.Row():
with gr.Column():
v2.ui()
with gr.Column():
generate_btn = gr.Button(value="Generate tags", variant="primary")
with gr.Group():
output_text = gr.TextArea(label="Output tags", interactive=False)
copy_btn = gr.Button(
value="Copy to clipboard",
interactive=False,
)
elapsed_time_md = gr.Markdown(label="Elapsed time", value="")
generate_image_btn = gr.Button(
value="Generate image with this prompt!",
interactive=False,
)
accordion, image_generation_config_components = (
image_generation_config_ui()
)
output_image = gr.Gallery(
label="Generated image",
show_label=True,
columns=1,
preview=True,
visible=True,
)
gr.Examples(
examples=GRADIO_EXAMPLES,
inputs=[
*v2.get_inputs()[1:8],
image_generation_config_components[0], # image_size
],
)
generate_btn.click(
parse_upsampling_output(v2.on_generate),
inputs=[
*v2.get_inputs(),
],
outputs=[output_text, elapsed_time_md, copy_btn, generate_image_btn],
)
copy_btn.click(gradio_copy_text, inputs=[output_text], js=COPY_ACTION_JS)
generate_image_btn.click(
image_generator.generate,
inputs=[output_text, *image_generation_config_components],
outputs=[output_image],
)
ui.launch()
if __name__ == "__main__":
main()
|