Spaces:
Running
on
Zero
Running
on
Zero
File size: 9,476 Bytes
2af7c18 eaa76b7 2af7c18 5c5e329 6f6d790 2af7c18 b766640 2af7c18 2c08108 2af7c18 406065d cf21a37 406065d cf21a37 2af7c18 eaa76b7 2c08108 2af7c18 365bdd8 2a602ea 2c08108 2af7c18 d47ea1e 2a602ea 2af7c18 365bdd8 2a602ea 365bdd8 2a602ea 365bdd8 2a602ea 365bdd8 2a602ea 365bdd8 5c5e329 365bdd8 2af7c18 2c08108 365bdd8 5c5e329 2a602ea 365bdd8 2a602ea 1c85335 2af7c18 e525d7c 2af7c18 69cab2c 2c08108 49a1eca 2c08108 49a1eca 2c08108 49a1eca 82172b4 50f5a42 49a1eca 82172b4 2c08108 49a1eca 82172b4 2c08108 49a1eca 82172b4 2c08108 49a1eca 2c08108 49a1eca 2c08108 49a1eca 2c08108 49a1eca e525d7c 365bdd8 e525d7c 365bdd8 e525d7c 365bdd8 e525d7c 365bdd8 e525d7c 365bdd8 e525d7c 365bdd8 e525d7c 365bdd8 e525d7c 67ae5ad b8af109 f857cb7 1c85335 50ded98 f857cb7 2af7c18 1c85335 2af7c18 67ae5ad 1c85335 2af7c18 1c85335 cf21a37 1c85335 cf21a37 2af7c18 cf21a37 4218146 cf21a37 2af7c18 8097377 1c85335 2af7c18 cf21a37 d47ea1e d92e171 d47ea1e 2af7c18 |
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 |
import gradio as gr
import numpy as np
import torch
import spaces
from diffusers import FluxPipeline, FluxTransformer2DModel
from diffusers.utils import export_to_gif
from huggingface_hub import hf_hub_download
from PIL import Image
import uuid
import random
device = "cuda" if torch.cuda.is_available() else "cpu"
if torch.cuda.is_available():
torch_dtype = torch.bfloat16
else:
torch_dtype = torch.float32
# νμ΄νλΌμΈ μ΄κΈ°ν μμ
pipe = FluxPipeline.from_pretrained(
"black-forest-labs/FLUX.1-dev",
torch_dtype=torch_dtype,
use_safetensors=True
).to(device)
MAX_SEED = np.iinfo(np.int32).max
def split_image(input_image, num_splits=8):
width = input_image.width
height = input_image.height
split_width = width // num_splits
output_images = []
for i in range(num_splits):
left = i * split_width
right = (i + 1) * split_width
box = (left, 0, right, height)
split = input_image.crop(box)
# μ΄λ―Έμ§ νμ§ κ°μ μ μν μ²λ¦¬
split = split.convert('RGB')
output_images.append(split)
return output_images
@spaces.GPU
def infer(prompt, seed=1, randomize_seed=False, num_inference_steps=20, progress=gr.Progress(track_tqdm=True)):
progress(0, desc="Starting...")
prompt_template = f"A single clear frame of {prompt}. The scene should show only one moment of the action, high quality, detailed, centered composition."
if randomize_seed:
seed = random.randint(0, MAX_SEED)
frames = []
total_frames = 8
# μ§ν μν©μ λ μΈλ°νκ² νμ
for i in range(total_frames):
current_progress = (i / total_frames) * 0.8
progress(current_progress, desc=f"π¨ Generating frame {i+1}/{total_frames}")
frame_prompt = f"{prompt_template} Frame {i+1} of sequence."
frame_seed = seed + i
generator = torch.Generator().manual_seed(frame_seed)
# κ° νλ μμ μμ± λ¨κ³λ νμ
for step in range(num_inference_steps):
step_progress = current_progress + (step / num_inference_steps) * (0.8 / total_frames)
progress(step_progress, desc=f"Frame {i+1}/{total_frames} - Step {step+1}/{num_inference_steps}")
frame = pipe(
prompt=frame_prompt,
num_inference_steps=num_inference_steps,
num_images_per_prompt=1,
generator=generator,
height=320,
width=320,
guidance_scale=7.5,
).images[0]
frames.append(frame)
progress((i + 1) / total_frames * 0.8, desc=f"β
Completed frame {i+1}/{total_frames}")
progress(0.9, desc="π¬ Creating GIF...")
gif_name = f"{uuid.uuid4().hex}-flux.gif"
export_to_gif(frames, gif_name, fps=8)
total_width = 320 * total_frames
preview_image = Image.new('RGB', (total_width, 320))
for i, frame in enumerate(frames):
preview_image.paste(frame, (i * 320, 0))
progress(1.0, desc="β¨ Done!")
return gif_name, preview_image, seed
def create_preview_image(frames):
"""νλ μλ€μ κ°λ‘λ‘ μ°κ²°νμ¬ λ―Έλ¦¬λ³΄κΈ° μ΄λ―Έμ§ μμ±"""
total_width = sum(frame.width for frame in frames)
max_height = max(frame.height for frame in frames)
preview = Image.new('RGB', (total_width, max_height))
x_offset = 0
for frame in frames:
preview.paste(frame, (x_offset, 0))
x_offset += frame.width
return preview
examples = [
"a red panda in mid-backflip",
"an astronaut floating in space",
"a butterfly spreading its wings",
"a robot arm painting with a brush",
"a dragon egg with cracks appearing",
"a person stepping through a glowing portal",
"a mermaid swimming underwater",
"a steampunk clock gear turning",
"a flower bud slowly opening",
"a wizard with magical energy swirling"
]
css = """
... (μ΄μ CSSμ λμΌ)
/* Examples μμ μ€νμΌ μμ μ¬μ μ */
.gr-examples-parent {
background: transparent !important;
}
.gr-examples-parent > div {
background: transparent !important;
}
.gr-examples {
background: transparent !important;
}
.gr-examples * {
background: transparent !important;
}
.gr-samples-table {
background: transparent !important;
}
.gr-samples-table > div {
background: transparent !important;
}
.gr-samples-table button {
background: transparent !important;
border: none !important;
box-shadow: none !important;
}
.gr-samples-table button:hover {
background: rgba(0,0,0,0.05) !important;
}
div[class*="examples"] {
background: transparent !important;
}
/* νλ‘κ·Έλ μ€ λ° μ€νμΌ κ°ν */
.progress-bar {
background-color: #f0f0f0;
border-radius: 10px;
padding: 5px;
margin: 15px 0;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.progress-bar-fill {
background: linear-gradient(45deg, #FF6B6B, #4ECDC4);
height: 25px;
border-radius: 7px;
transition: width 0.3s ease-out;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.progress-text {
color: black;
font-weight: 600;
margin-bottom: 8px;
font-size: 1.1em;
}
/* μ§ν μν ν
μ€νΈ μ€νμΌ */
.progress-label {
display: block;
text-align: center;
margin-top: 5px;
color: #666;
font-size: 0.9em;
}
"""
def create_snow_effect():
# CSS μ€νμΌ μ μ
snow_css = """
@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);
"""
# CSSμ JavaScriptλ₯Ό κ²°ν©ν HTML
snow_html = f"""
<style>
{snow_css}
</style>
<script>
{snow_js}
</script>
"""
return gr.HTML(snow_html)
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
gr.HTML("""
<div style="text-align: center; max-width: 800px; margin: 0 auto;">
<h1 style="font-size: 3rem; font-weight: 700; margin-bottom: 1rem;">
FLUX Animation Creator
</h1>
<p style="font-size: 1.2rem; color: #666; margin-bottom: 2rem;">
Create amazing animated GIFs with AI - Just describe what you want to see!
</p>
</div>
""")
create_snow_effect()
with gr.Column(elem_id="col-container"):
with gr.Row():
prompt = gr.Text(
label="Your Animation Prompt",
show_label=True,
max_lines=1,
placeholder="Describe the animation you want to create...",
container=True,
elem_id="prompt-input"
)
run_button = gr.Button("β¨ Generate", scale=0, variant="primary")
result = gr.Image(
label="Generated Animation",
show_label=True,
elem_id="main-output",
height=500
)
with gr.Row():
result_full = gr.Image(
label="Preview",
elem_id="preview-output",
height=200
)
strip_image = gr.Image(
label="Animation Strip",
elem_id="strip-output",
height=150
)
with gr.Accordion("Advanced Settings", open=False):
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0,
)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
num_inference_steps = gr.Slider(
label="Number of inference steps",
minimum=1,
maximum=25,
step=1,
value=20,
)
gr.Examples(
examples=examples,
inputs=[prompt],
outputs=[result, result_full, seed],
fn=infer,
cache_examples=True,
label="Click on any example to try it out"
)
gr.on(
triggers=[run_button.click, prompt.submit],
fn=infer,
inputs=[prompt, seed, randomize_seed, num_inference_steps],
outputs=[result, result_full, seed]
)
demo.theme = gr.themes.Default().set(
body_text_color="black",
block_label_text_color="black",
block_title_text_color="black",
body_text_color_subdued="black",
background_fill_primary="white"
)
demo.queue().launch() |