File size: 16,141 Bytes
0f2cc0f e6a4b4e 0f2cc0f c51a859 0f2cc0f c51a859 1c80f45 0f2cc0f 1c80f45 c51a859 1c80f45 0f2cc0f 1c80f45 c51a859 1c80f45 c51a859 1c80f45 db73293 c51a859 1c80f45 c51a859 1c80f45 c51a859 1c80f45 c51a859 db73293 1c80f45 db73293 1c80f45 cd72379 1c80f45 cd72379 1c80f45 cd72379 1c80f45 cd72379 1c80f45 cd72379 1c80f45 cd72379 e5a5926 db73293 cd72379 db73293 cd72379 db73293 cd72379 db73293 cd72379 db73293 cd72379 0f2cc0f 47e5103 e5a5926 db73293 cd72379 db73293 e389efe cd72379 e389efe 0f2cc0f e6a4b4e 1c80f45 c51a859 cd72379 e5a5926 1c80f45 cd72379 c51a859 db73293 cd72379 db73293 cd72379 47e5103 e6a4b4e e389efe cd72379 0f2cc0f db73293 0f2cc0f e5a5926 cd72379 e5a5926 1c80f45 e389efe cd72379 e6a4b4e e389efe db73293 e6a4b4e db73293 e6a4b4e db73293 e389efe cd72379 db73293 e389efe cd72379 db73293 cd72379 e389efe db73293 cd72379 db73293 e389efe db73293 e389efe db73293 e389efe db73293 e6a4b4e e389efe db73293 cd72379 db73293 e389efe db73293 c51a859 db73293 e389efe e6a4b4e db73293 cd72379 db73293 cd72379 e6a4b4e db73293 cd72379 db73293 e389efe e6a4b4e cd72379 db73293 cd72379 e6a4b4e cd72379 db73293 e389efe db73293 e6a4b4e cd72379 c51a859 e389efe db73293 cd72379 e389efe db73293 e6a4b4e cd72379 db73293 cd72379 db73293 cd72379 db73293 cd72379 db73293 cd72379 db73293 cd72379 e6a4b4e db73293 cd72379 e389efe cd72379 db73293 cd72379 db73293 cd72379 e6a4b4e cd72379 db73293 e6a4b4e db73293 cd72379 e389efe db73293 cd72379 db73293 cd72379 e389efe cd72379 db73293 cd72379 c51a859 db73293 c51a859 db73293 c51a859 db73293 c51a859 1c80f45 cd72379 db73293 cd72379 db73293 e6a4b4e db73293 e6a4b4e db73293 e389efe cd72379 db73293 e6a4b4e cd72379 1c80f45 cd72379 db73293 e389efe cd72379 e389efe cd72379 1c80f45 db73293 e389efe cd72379 db73293 cd72379 e389efe db73293 e389efe 0f2cc0f 1c80f45 e389efe e6a4b4e 1c80f45 e389efe db73293 0f2cc0f db73293 cd72379 db73293 cd72379 db73293 cd72379 e389efe c51a859 db73293 1c80f45 db73293 cd72379 db73293 cd72379 c51a859 1c80f45 cd72379 db73293 1c80f45 c51a859 db73293 cd72379 db73293 1c80f45 e389efe 0f2cc0f e389efe db73293 0f2cc0f | 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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | import gradio as gr
import torch
import spaces
from diffusers import StableDiffusionXLPipeline, DPMSolverMultistepScheduler
from huggingface_hub import hf_hub_download, InferenceClient
import random
import os
import re
# โโ Config โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
HF_TOKEN = os.environ.get("HF_TOKEN", None)
MODEL_REPO = "John6666/nova-3dcg-xl-illustrious-v40-sdxl"
# Quality tags for Illustrious-based models
IL_POS = "masterpiece, best quality, very aesthetic, absurdres, "
IL_NEG = "worst quality, low quality, bad quality, ugly, "
# โโ LLM client โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
llm_client = InferenceClient(
model="mistralai/Mistral-7B-Instruct-v0.3",
token=HF_TOKEN,
)
EXPANSION_SYSTEM = """You are an expert Stable Diffusion prompt engineer specialising in 3DCG character art and illustration.
Your job: take a short user description and rewrite it as a detailed, accurate image generation prompt optimised for a 3D CGI character art model (Nova 3DCG XL).
Rules:
- PRESERVE every specific detail โ colours, numbers, states, accessories, clothing
- Wrap unique specific details in attention weights e.g. (red scarf:1.4), (one eye closed:1.3)
- Add: character pose, expression, lighting, background atmosphere, material quality, render style
- Add 3DCG-appropriate quality boosters: sharp edges, subsurface scattering, ray tracing, ambient occlusion
- Do NOT add NSFW content
- Do NOT invent things not implied by the user
- Return ONLY the final prompt โ no explanation, no preamble, no quotes
- Keep under 130 words
- Use comma-separated tags and phrases"""
def expand_prompt_llm(raw_prompt, style):
if not raw_prompt.strip():
return ""
style_hint = f" The desired style is: {style}." if style != "Auto" else ""
user_msg = f"Expand this into a detailed 3DCG character art prompt:{style_hint}\n\n{raw_prompt.strip()}"
try:
response = llm_client.chat_completion(
messages=[
{"role": "system", "content": EXPANSION_SYSTEM},
{"role": "user", "content": user_msg},
],
max_tokens=220,
temperature=0.7,
)
expanded = response.choices[0].message.content.strip()
expanded = expanded.strip('"').strip("'")
if expanded.lower().startswith("prompt:"):
expanded = expanded[7:].strip()
return expanded
except Exception as e:
print(f"LLM expansion failed: {e}")
return raw_prompt.strip()
# โโ Load model โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
print(f"Loading Nova 3DCG XL from {MODEL_REPO}...")
pipe = StableDiffusionXLPipeline.from_pretrained(
MODEL_REPO,
torch_dtype=torch.float16,
token=HF_TOKEN,
)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(
pipe.scheduler.config,
use_karras_sigmas=True,
)
pipe.enable_attention_slicing()
print("Pipeline ready.")
# โโ Style presets โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
STYLES = {
"Auto": {"pos": "", "neg": ""},
"๐ฎ 3DCG Render": {
"pos": "3DCG render, Pixar style, ray tracing, subsurface scattering, ambient occlusion, sharp edges, studio lighting, ",
"neg": "flat, 2D, anime flat colour, sketch, ",
},
"โ๏ธ Fantasy": {
"pos": "fantasy character, epic armour, magical atmosphere, dramatic lighting, volumetric fog, concept art, artstation, ",
"neg": "modern, mundane, sci-fi, ",
},
"๐ค Sci-Fi": {
"pos": "sci-fi character, futuristic suit, neon accents, holographic elements, dark background, cinematic, ",
"neg": "medieval, fantasy, nature, ",
},
"๐ธ Stylised": {
"pos": "stylised illustration, vibrant colours, soft cel shading, clean lineart, anime-adjacent, ",
"neg": "photorealistic, gritty, dark, ",
},
"๐ฌ Cinematic": {
"pos": "cinematic portrait, dramatic rim lighting, shallow depth of field, film grain, color graded, ",
"neg": "flat, overexposed, sketch, ",
},
"๐๏ธ Urban": {
"pos": "urban streetwear character, city background, neon lights, night scene, realistic clothing, ",
"neg": "fantasy, medieval, nature, ",
},
}
# โโ LoRAs โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
LORAS = {
"None": None,
"โ Better Hands": {
"repo": "WolfAether21/PONY-DIFFUSION-SDXL-LORA",
"file": "Perfect Hands v2.safetensors",
"strength": 0.7,
},
"๐ More Detail": {
"repo": "WolfAether21/PONY-DIFFUSION-SDXL-LORA",
"file": "SDXL Detail.safetensors",
"strength": 0.6,
},
}
# โโ Generation โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@spaces.GPU(duration=180)
def generate(raw_prompt, negative_prompt, style, lora_name, lora_strength,
width, height, steps, guidance, seed, randomize, show_expanded):
if not raw_prompt.strip():
raise gr.Error("Please enter a prompt.")
if randomize:
seed = random.randint(0, 2**32 - 1)
seed = int(seed)
# LLM expansion
expanded = expand_prompt_llm(raw_prompt, style)
style_data = STYLES.get(style, STYLES["Auto"])
final_pos = IL_POS + style_data["pos"] + expanded
final_neg = IL_NEG + style_data["neg"] + negative_prompt.strip()
pipe.to("cuda")
# LoRA
lora_loaded = False
lora_data = LORAS.get(lora_name)
if lora_data:
try:
lp = hf_hub_download(
repo_id=lora_data["repo"],
filename=lora_data["file"],
token=HF_TOKEN,
)
pipe.load_lora_weights(lp)
pipe.fuse_lora(lora_scale=float(lora_strength))
lora_loaded = True
except Exception as e:
print(f"LoRA failed, skipping: {e}")
generator = torch.Generator(device="cpu").manual_seed(seed)
result = pipe(
prompt=final_pos,
negative_prompt=final_neg,
width=int(width),
height=int(height),
num_inference_steps=int(steps),
guidance_scale=float(guidance),
generator=generator,
clip_skip=1,
)
if lora_loaded:
pipe.unfuse_lora()
pipe.unload_lora_weights()
pipe.to("cpu")
debug = f"**Expanded prompt:**\n\n{final_pos}" if show_expanded else ""
return result.images[0], seed, debug
# โโ CSS โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
css = """
* { box-sizing: border-box; margin: 0; padding: 0; }
body, .gradio-container {
background: #07070e !important;
font-family: 'Inter', system-ui, -apple-system, sans-serif !important;
max-width: 500px !important;
margin: 0 auto !important;
padding: 8px !important;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 2px 14px;
}
.topbar-title {
color: #e8e0ff;
font-size: 0.95em;
font-weight: 800;
}
.gpu-pill {
background: #1aff7a18;
border: 1px solid #1aff7a44;
color: #1aff7a;
font-size: 0.6em;
font-weight: 800;
padding: 4px 12px;
border-radius: 20px;
letter-spacing: 1.5px;
text-transform: uppercase;
}
.img-out {
background: #0d0d1a;
border: 1px solid #16162a;
border-radius: 20px;
overflow: hidden;
margin-bottom: 8px;
min-height: 380px;
display: flex;
align-items: center;
justify-content: center;
}
.img-out img {
width: 100% !important;
border-radius: 20px;
display: block;
}
.seed-pill input[type=number] {
background: transparent !important;
border: none !important;
color: #2e2848 !important;
font-size: 0.7em !important;
text-align: center !important;
padding: 2px !important;
width: 100% !important;
}
.card {
background: #0d0d1a;
border: 1px solid #16162a;
border-radius: 14px;
padding: 14px;
margin-bottom: 8px;
}
.card-label {
color: #3d3060;
font-size: 0.62em;
font-weight: 800;
text-transform: uppercase;
letter-spacing: 2px;
margin-bottom: 8px;
}
textarea {
background: transparent !important;
border: none !important;
color: #c8b8f0 !important;
font-size: 15px !important;
line-height: 1.6 !important;
padding: 0 !important;
resize: none !important;
box-shadow: none !important;
width: 100% !important;
outline: none !important;
}
textarea::placeholder { color: #252038 !important; }
textarea:focus {
outline: none !important;
box-shadow: none !important;
border: none !important;
}
.style-wrap .gr-radio {
display: flex !important;
flex-wrap: wrap !important;
gap: 6px !important;
}
.style-wrap label {
background: #0d0d1a !important;
border: 1px solid #1a1a2e !important;
border-radius: 30px !important;
color: #4a3a6a !important;
font-size: 0.75em !important;
font-weight: 600 !important;
padding: 6px 14px !important;
cursor: pointer !important;
transition: all 0.15s ease !important;
white-space: nowrap !important;
}
.style-wrap label:has(input:checked) {
background: #18083a !important;
border-color: #7744ee !important;
color: #bb99ff !important;
box-shadow: 0 0 10px #7744ee33 !important;
}
.style-wrap input[type=radio] { display: none !important; }
.gradio-accordion {
background: #0d0d1a !important;
border: 1px solid #16162a !important;
border-radius: 14px !important;
margin-bottom: 8px !important;
overflow: hidden !important;
}
.gradio-accordion .label-wrap button {
color: #4a3a6a !important;
font-size: 0.72em !important;
font-weight: 700 !important;
text-transform: uppercase !important;
letter-spacing: 1.5px !important;
padding: 12px 16px !important;
}
.gradio-slider {
background: transparent !important;
border: none !important;
padding: 4px 0 10px !important;
}
input[type=range] {
accent-color: #6633bb !important;
width: 100% !important;
}
input[type=number] {
background: #0a0a14 !important;
border: 1px solid #18182a !important;
border-radius: 10px !important;
color: #9977cc !important;
font-size: 13px !important;
padding: 8px 10px !important;
}
input[type=checkbox] { accent-color: #6633bb !important; }
.gradio-checkbox label span {
color: #4a3a6a !important;
font-size: 0.75em !important;
font-weight: 600 !important;
}
.gradio-dropdown {
background: #0a0a14 !important;
border: 1px solid #18182a !important;
border-radius: 10px !important;
}
label > span:first-child {
color: #3a2d55 !important;
font-size: 0.7em !important;
font-weight: 700 !important;
text-transform: uppercase !important;
letter-spacing: 1px !important;
}
.debug-box {
background: #080814;
border: 1px solid #111122;
border-radius: 10px;
padding: 10px 12px;
color: #443366;
font-size: 0.7em;
line-height: 1.7;
font-family: monospace;
word-break: break-word;
margin-bottom: 8px;
min-height: 10px;
}
.gen-btn button {
background: linear-gradient(135deg, #4a1aaa 0%, #2d0e77 100%) !important;
border: 1px solid #6633cc !important;
border-radius: 14px !important;
color: #fff !important;
font-size: 0.88em !important;
font-weight: 900 !important;
padding: 17px !important;
width: 100% !important;
letter-spacing: 2px !important;
text-transform: uppercase !important;
box-shadow: 0 4px 24px #4a1aaa55 !important;
transition: all 0.15s ease !important;
margin-top: 6px !important;
}
.gen-btn button:hover {
box-shadow: 0 6px 32px #4a1aaa99 !important;
transform: translateY(-1px) !important;
}
.gen-btn button:active {
transform: scale(0.98) !important;
box-shadow: 0 2px 12px #4a1aaa33 !important;
}
footer, .built-with { display: none !important; }
"""
# โโ UI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
with gr.Blocks(css=css, title="ImageGen") as demo:
gr.HTML("""
<div class="topbar">
<span class="topbar-title">Nova 3DCG XL</span>
<span class="gpu-pill">โก ZeroGPU</span>
</div>
""")
output_image = gr.Image(
show_label=False, type="pil",
height=460, elem_classes="img-out",
)
used_seed = gr.Number(
label="seed", interactive=False,
elem_classes="seed-pill",
)
gr.HTML('<div class="card"><div class="card-label">โฆ Prompt โ describe your character</div>')
prompt = gr.Textbox(
show_label=False,
placeholder="warrior woman in red armour, glowing sword, forest background...",
lines=3,
)
gr.HTML('</div>')
gr.HTML('<div class="card-label" style="padding:4px 2px 8px;color:#3d3060;font-size:0.62em;font-weight:800;text-transform:uppercase;letter-spacing:2px;">Style</div>')
style = gr.Radio(
choices=list(STYLES.keys()),
value="Auto",
show_label=False,
elem_classes="style-wrap",
)
generate_btn = gr.Button(
"Generate โฆ", variant="primary",
size="lg", elem_classes="gen-btn",
)
expanded_out = gr.Markdown(
value="",
elem_classes="debug-box",
)
with gr.Accordion("โ๏ธ Settings", open=False):
gr.HTML('<div style="height:6px"></div>')
negative_prompt = gr.Textbox(
label="Negative Prompt",
value=(
"worst quality, low quality, bad anatomy, bad hands, "
"extra limbs, missing limbs, watermark, signature, "
"blurry, deformed, ugly, text"
),
lines=2,
)
with gr.Row():
width = gr.Slider(512, 1024, value=832, step=64, label="Width")
height = gr.Slider(512, 1216, value=1216, step=64, label="Height")
steps = gr.Slider(20, 60, value=30, step=1, label="Steps")
guidance = gr.Slider(1.0, 10.0, value=6.0, step=0.5, label="CFG Scale")
with gr.Row():
seed = gr.Number(
label="Seed", value=42, precision=0,
minimum=0, maximum=2**32-1, scale=3,
)
randomize = gr.Checkbox(label="Random seed", value=True, scale=1)
show_expanded = gr.Checkbox(
label="Show expanded prompt",
value=True,
)
with gr.Accordion("๐จ LoRA", open=False):
gr.HTML('<div style="height:6px"></div>')
lora_name = gr.Dropdown(choices=list(LORAS.keys()), value="None", label="LoRA")
lora_strength = gr.Slider(0.1, 1.0, value=0.7, step=0.05, label="Strength")
generate_btn.click(
fn=generate,
inputs=[
prompt, negative_prompt, style, lora_name, lora_strength,
width, height, steps, guidance, seed, randomize, show_expanded,
],
outputs=[output_image, used_seed, expanded_out],
)
demo.launch()
|