Spaces:
Running
on
Zero
Running
on
Zero
File size: 10,748 Bytes
27e8bc2 |
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 |
import spaces
import gradio as gr
import torch
from PIL import Image
from diffusers import DiffusionPipeline
import random
from transformers import pipeline
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.backends.cuda.matmul.allow_tf32 = True
# ๋ฒ์ญ ๋ชจ๋ธ ์ด๊ธฐํ
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
base_model = "black-forest-labs/FLUX.1-dev"
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
lora_repo = "prithivMLmods/Canopus-Clothing-Flux-LoRA"
trigger_word = ""
pipe.load_lora_weights(lora_repo)
pipe.to("cuda")
MAX_SEED = 2**32-1
@spaces.GPU()
def translate_and_generate(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
# ํ๊ธ ๊ฐ์ง ๋ฐ ๋ฒ์ญ
def contains_korean(text):
return any(ord('๊ฐ') <= ord(char) <= ord('ํฃ') for char in text)
if contains_korean(prompt):
# ํ๊ธ์ ์์ด๋ก ๋ฒ์ญ
translated = translator(prompt)[0]['translation_text']
actual_prompt = translated
else:
actual_prompt = prompt
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.Generator(device="cuda").manual_seed(seed)
progress(0, "Starting image generation...")
for i in range(1, steps + 1):
if i % (steps // 10) == 0:
progress(i / steps * 100, f"Processing step {i} of {steps}...")
image = pipe(
prompt=f"{actual_prompt} {trigger_word}",
num_inference_steps=steps,
guidance_scale=cfg_scale,
width=width,
height=height,
generator=generator,
joint_attention_kwargs={"scale": lora_scale},
).images[0]
progress(100, "Completed!")
return image, seed
example_image_path = "example0.webp"
example_prompt = """Cozy winter scene with a Christmas atmosphere: a snow-covered cabin in the forest, warm light glowing from the windows, surrounded by sparkling Christmas decorations and a beautifully adorned Christmas tree. The sky is filled with stars, and soft snowflakes are gently falling, creating a serene and warm ambiance"""
example_cfg_scale = 3.2
example_steps = 32
example_width = 1152
example_height = 896
example_seed = 3981632454
example_lora_scale = 0.85
def load_example():
example_image = Image.open(example_image_path)
return example_prompt, example_cfg_scale, example_steps, True, example_seed, example_width, example_height, example_lora_scale, example_image
# CSS ์ ์
custom_css = """
/* ๊ธฐ๋ณธ ์คํ์ผ */
body {
margin: 0;
padding: 0;
background: url('file/example0.webp') no-repeat center center fixed;
background-size: cover;
min-height: 100vh;
}
/* ์ปจํ
์ด๋ */
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
box-sizing: border-box;
}
/* ํค๋ */
.header {
text-align: center;
color: white;
text-shadow: 2px 2px 4px rgba(0,0,0,0.7);
margin-bottom: 30px;
font-size: 2.5em;
}
/* ๋ฐ์ค ๊ณตํต ์คํ์ผ */
.box-common {
background-color: rgba(255, 255, 255, 0.85);
backdrop-filter: blur(10px);
border-radius: 15px;
padding: 20px;
margin: 20px 0;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
/* ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋ฐ์ค */
.result-box {
width: 90%;
max-width: 1000px;
margin: 20px auto;
}
.image-output {
width: 100%;
max-width: 800px;
margin: 0 auto;
display: block;
}
/* ํ๋กฌํํธ ์
๋ ฅ ๋ฐ์ค */
.prompt-box {
width: 90%;
max-width: 1000px;
margin: 20px auto;
}
/* ๋ฒํผ ์คํ์ผ */
.generate-btn {
background-color: #2ecc71 !important;
color: white !important;
padding: 12px 30px !important;
border-radius: 8px !important;
border: none !important;
font-size: 1.1em !important;
cursor: pointer !important;
transition: background-color 0.3s ease !important;
width: 200px !important;
margin: 20px auto !important;
display: block !important;
}
.generate-btn:hover {
background-color: #27ae60 !important;
}
/* ์์ฝ๋์ธ ์คํ์ผ */
.accordion {
width: 90%;
max-width: 1000px;
margin: 20px auto;
}
/* ์ค๋์ค ์ปจํธ๋กค */
.audio-controls {
position: fixed;
bottom: 20px;
right: 20px;
z-index: 1000;
display: flex;
gap: 10px;
background-color: rgba(255, 255, 255, 0.9);
padding: 15px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.audio-btn {
background-color: #3498db;
color: white;
border: none;
padding: 8px 15px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.audio-btn:hover {
background-color: #2980b9;
}
/* ๋ ๋ด๋ฆฌ๋ ํจ๊ณผ */
@keyframes snowfall {
0% {
transform: translateY(-10vh) translateX(0);
opacity: 1;
}
100% {
transform: translateY(100vh) translateX(100px);
opacity: 0.3;
}
}
.snowflake {
position: fixed;
color: white;
font-size: 1.5em;
user-select: none;
z-index: 1000;
pointer-events: none;
animation: snowfall linear infinite;
}
"""
# JavaScript ์ฝ๋
snow_js = """
function createSnowflake() {
const snowflake = document.createElement('div');
snowflake.innerHTML = 'โ';
snowflake.className = 'snowflake';
snowflake.style.left = Math.random() * 100 + 'vw';
snowflake.style.animationDuration = Math.random() * 3 + 2 + 's';
snowflake.style.opacity = Math.random();
document.body.appendChild(snowflake);
setTimeout(() => {
snowflake.remove();
}, 5000);
}
setInterval(createSnowflake, 200);
"""
audio_js = """
let currentlyPlaying = null;
function toggleAudio(num) {
const audio = document.getElementById('bgMusic' + num);
const otherAudio = document.getElementById('bgMusic' + (num === 1 ? 2 : 1));
if (currentlyPlaying === audio) {
audio.pause();
currentlyPlaying = null;
} else {
if (currentlyPlaying) {
currentlyPlaying.pause();
}
otherAudio.pause();
audio.play();
currentlyPlaying = audio;
}
}
function stopAllAudio() {
const audios = document.querySelectorAll('audio');
audios.forEach(audio => audio.pause());
currentlyPlaying = null;
}
"""
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
app = gr.Blocks(css=custom_css,theme="Yntec/HaleyCH_Theme_Orange")
with app:
# JavaScript ์ด๊ธฐํ
gr.HTML(f"<script>{snow_js}</script>")
with gr.Column(elem_classes="container"):
gr.Markdown("# ๐ X-MAS LoRA", elem_classes="header")
# ์ด๋ฏธ์ง ์ถ๋ ฅ ์์ญ
with gr.Group(elem_classes="result-box box-common"):
gr.Markdown("### ๐ผ๏ธ Generated Image")
result = gr.Image(label="Result", elem_classes="image-output")
# ํ๋กฌํํธ ์
๋ ฅ ์์ญ
with gr.Group(elem_classes="prompt-box box-common"):
prompt = gr.TextArea(
label="โ๏ธ Your Prompt (ํ๊ธ ๋๋ ์์ด)",
placeholder="์ด๋ฏธ์ง๋ฅผ ์ค๋ช
ํ์ธ์...",
lines=5
)
generate_button = gr.Button(
"๐ Generate Image",
elem_classes="generate-btn"
)
# ๊ณ ๊ธ ์ต์
with gr.Accordion("๐จ Advanced Options", open=False, elem_classes="accordion box-common"):
with gr.Group(elem_classes="parameter-box"):
gr.Markdown("### ๐๏ธ Generation Parameters")
with gr.Row():
with gr.Column():
cfg_scale = gr.Slider(
label="CFG Scale",
minimum=1,
maximum=20,
step=0.5,
value=example_cfg_scale
)
steps = gr.Slider(
label="Steps",
minimum=1,
maximum=100,
step=1,
value=example_steps
)
lora_scale = gr.Slider(
label="LoRA Scale",
minimum=0,
maximum=1,
step=0.01,
value=example_lora_scale
)
with gr.Group(elem_classes="parameter-box"):
gr.Markdown("### ๐ Image Dimensions")
with gr.Row():
width = gr.Slider(
label="Width",
minimum=256,
maximum=1536,
step=64,
value=example_width
)
height = gr.Slider(
label="Height",
minimum=256,
maximum=1536,
step=64,
value=example_height
)
with gr.Group(elem_classes="parameter-box"):
gr.Markdown("### ๐ฒ Seed Settings")
with gr.Row():
randomize_seed = gr.Checkbox(
True,
label="Randomize seed"
)
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=example_seed
)
# ์ค๋์ค ์ปจํธ๋กค
gr.HTML(f"""
<div class="audio-controls">
<button class="audio-btn" onclick="toggleAudio(1)">๐ต Music 1</button>
<button class="audio-btn" onclick="toggleAudio(2)">๐ต Music 2</button>
<button class="audio-btn" onclick="stopAllAudio()">โน Stop</button>
</div>
<audio id="bgMusic1" loop>
<source src="file/1.mp3" type="audio/mp3">
</audio>
<audio id="bgMusic2" loop>
<source src="file/2.mp3" type="audio/mp3">
</audio>
<script>{audio_js}</script>
""")
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
app.load(
load_example,
inputs=[],
outputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, result]
)
generate_button.click(
translate_and_generate,
inputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale],
outputs=[result, seed]
)
app.queue()
app.launch() |