Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,7 @@ from diffusers.utils import load_image
|
|
| 12 |
from diffusers.utils import export_to_video
|
| 13 |
import random
|
| 14 |
from transformers import pipeline
|
|
|
|
| 15 |
# Translation model load
|
| 16 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
| 17 |
|
|
@@ -35,31 +36,29 @@ english_labels = {
|
|
| 35 |
"Seed": "Seed"
|
| 36 |
}
|
| 37 |
|
| 38 |
-
#
|
| 39 |
base_model = "black-forest-labs/FLUX.1-schnell"
|
| 40 |
|
| 41 |
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=torch.bfloat16).to("cuda")
|
| 42 |
-
pipe = FluxPipeline.from_pretrained(
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
pipe.transformer.to(memory_format=torch.channels_last)
|
| 47 |
clip_slider = CLIPSliderFlux(pipe, device=torch.device("cuda"))
|
| 48 |
|
| 49 |
-
MAX_SEED = 2**32-1
|
| 50 |
|
| 51 |
def save_images_with_unique_filenames(image_list, save_directory):
|
| 52 |
if not os.path.exists(save_directory):
|
| 53 |
os.makedirs(save_directory)
|
| 54 |
-
|
| 55 |
paths = []
|
| 56 |
for image in image_list:
|
| 57 |
unique_filename = f"{uuid.uuid4()}.png"
|
| 58 |
file_path = os.path.join(save_directory, unique_filename)
|
| 59 |
-
|
| 60 |
image.save(file_path)
|
| 61 |
paths.append(file_path)
|
| 62 |
-
|
| 63 |
return paths
|
| 64 |
|
| 65 |
def convert_to_centered_scale(num):
|
|
@@ -91,8 +90,7 @@ def generate(prompt,
|
|
| 91 |
x_concept_1="", x_concept_2="",
|
| 92 |
avg_diff_x=None,
|
| 93 |
total_images=[],
|
| 94 |
-
gradio_progress=gr.Progress()
|
| 95 |
-
):
|
| 96 |
# Translate prompt and concepts if Korean
|
| 97 |
prompt = translate_if_korean(prompt)
|
| 98 |
concept_1 = translate_if_korean(concept_1)
|
|
@@ -100,7 +98,7 @@ def generate(prompt,
|
|
| 100 |
|
| 101 |
print(f"Prompt: {prompt}, ← {concept_2}, {concept_1} ➡️ . scale {scale}, interm steps {interm_steps}")
|
| 102 |
slider_x = [concept_2, concept_1]
|
| 103 |
-
#
|
| 104 |
if randomize_seed:
|
| 105 |
seed = random.randint(0, MAX_SEED)
|
| 106 |
|
|
@@ -116,18 +114,22 @@ def generate(prompt,
|
|
| 116 |
low_scale = -1 * scale
|
| 117 |
for i in gradio_progress.tqdm(range(interm_steps), desc="Generating images"):
|
| 118 |
cur_scale = low_scale + (high_scale - low_scale) * i / (interm_steps - 1)
|
| 119 |
-
image = clip_slider.generate(
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
images.append(image)
|
| 125 |
-
canvas = Image.new('RGB', (256*interm_steps, 256))
|
| 126 |
for i, im in enumerate(images):
|
| 127 |
-
canvas.paste(im.resize((256,256)), (256 * i, 0))
|
| 128 |
|
| 129 |
comma_concepts_x = f"{slider_x[1]}, {slider_x[0]}"
|
| 130 |
-
|
| 131 |
scale_total = convert_to_centered_scale(interm_steps)
|
| 132 |
scale_min = scale_total[0]
|
| 133 |
scale_max = scale_total[-1]
|
|
@@ -140,9 +142,7 @@ def generate(prompt,
|
|
| 140 |
return x_concept_1, x_concept_2, avg_diff_x, export_to_video(images, video_path, fps=5), canvas, images, images[scale_middle], post_generation_slider_update, seed
|
| 141 |
|
| 142 |
def update_pre_generated_images(slider_value, total_images):
|
| 143 |
-
number_images = 0
|
| 144 |
-
if total_images:
|
| 145 |
-
number_images = len(total_images)
|
| 146 |
if number_images > 0:
|
| 147 |
scale_tuple = convert_to_centered_scale(number_images)
|
| 148 |
return total_images[scale_tuple.index(slider_value)][0]
|
|
@@ -152,41 +152,18 @@ def update_pre_generated_images(slider_value, total_images):
|
|
| 152 |
def reset_recalc_directions():
|
| 153 |
return True
|
| 154 |
|
| 155 |
-
#
|
| 156 |
-
# 개선된 예제 프롬프트
|
| 157 |
-
# =======================
|
| 158 |
examples = [
|
| 159 |
-
[
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
],
|
| 165 |
-
[
|
| 166 |
-
"A bustling city street at dusk with neon lights reflecting on wet pavement, capturing the contrast between day and night.",
|
| 167 |
-
"Daytime",
|
| 168 |
-
"Nighttime",
|
| 169 |
-
2.5
|
| 170 |
-
],
|
| 171 |
-
[
|
| 172 |
-
"An abstract digital art composition featuring vibrant colors and dynamic shapes.",
|
| 173 |
-
"Calm",
|
| 174 |
-
"Energetic",
|
| 175 |
-
2.0
|
| 176 |
-
],
|
| 177 |
-
[
|
| 178 |
-
"여성의 미소와 함께하는 따뜻한 분위기의 인물 사진",
|
| 179 |
-
"젊음",
|
| 180 |
-
"노년",
|
| 181 |
-
2.5
|
| 182 |
-
]
|
| 183 |
]
|
| 184 |
|
| 185 |
-
#
|
| 186 |
-
# 밝고 세련된 UI CSS
|
| 187 |
-
# =======================
|
| 188 |
css = """
|
| 189 |
-
/*
|
| 190 |
body {
|
| 191 |
background: #ffffff url('https://images.unsplash.com/photo-1506748686214-e9df14d4d9d0?ixlib=rb-1.2.1&auto=format&fit=crop&w=1600&q=80') no-repeat center center fixed;
|
| 192 |
background-size: cover;
|
|
@@ -237,7 +214,10 @@ footer {
|
|
| 237 |
}
|
| 238 |
"""
|
| 239 |
|
| 240 |
-
with gr.Blocks(css=css) as demo:
|
|
|
|
|
|
|
|
|
|
| 241 |
x_concept_1 = gr.State("")
|
| 242 |
x_concept_2 = gr.State("")
|
| 243 |
total_images = gr.State([])
|
|
@@ -261,15 +241,14 @@ with gr.Blocks(css=css) as demo:
|
|
| 261 |
concept_1 = gr.Textbox(
|
| 262 |
label=english_labels["1st direction to steer"],
|
| 263 |
info="Initial state",
|
| 264 |
-
placeholder="
|
| 265 |
)
|
| 266 |
with gr.Column(scale=1):
|
| 267 |
concept_2 = gr.Textbox(
|
| 268 |
label=english_labels["2nd direction to steer"],
|
| 269 |
info="Final state",
|
| 270 |
-
placeholder="
|
| 271 |
)
|
| 272 |
-
|
| 273 |
with gr.Row(elem_classes="slider-container"):
|
| 274 |
x = gr.Slider(
|
| 275 |
minimum=0,
|
|
@@ -279,7 +258,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 279 |
label=english_labels["Strength"],
|
| 280 |
info="Maximum strength for each direction (above 2.5 may be unstable)"
|
| 281 |
)
|
| 282 |
-
|
| 283 |
submit = gr.Button(english_labels["Generate directions"], size="lg", variant="primary")
|
| 284 |
|
| 285 |
# Advanced Options Panel
|
|
@@ -301,7 +279,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 301 |
step=0.1,
|
| 302 |
value=3.5
|
| 303 |
)
|
| 304 |
-
|
| 305 |
with gr.Row():
|
| 306 |
with gr.Column(scale=1):
|
| 307 |
iterations = gr.Slider(
|
|
@@ -319,7 +296,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 319 |
maximum=4,
|
| 320 |
step=1
|
| 321 |
)
|
| 322 |
-
|
| 323 |
with gr.Row():
|
| 324 |
with gr.Column(scale=1):
|
| 325 |
randomize_seed = gr.Checkbox(
|
|
@@ -337,32 +313,27 @@ with gr.Blocks(css=css) as demo:
|
|
| 337 |
)
|
| 338 |
|
| 339 |
# Right Column - Output
|
| 340 |
-
with gr.Column(scale=
|
| 341 |
with gr.Group(elem_classes="main-panel"):
|
| 342 |
gr.Markdown("### Generated Results")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
with gr.Row():
|
| 344 |
-
with gr.Column(scale=
|
| 345 |
-
image_seq = gr.Image(
|
| 346 |
-
label=english_labels["Strip"],
|
| 347 |
-
elem_id="strip",
|
| 348 |
-
height=100
|
| 349 |
-
)
|
| 350 |
-
with gr.Column(scale=2):
|
| 351 |
-
output_image = gr.Video(
|
| 352 |
-
label=english_labels["Looping video"],
|
| 353 |
-
elem_id="video",
|
| 354 |
-
loop=True,
|
| 355 |
-
autoplay=True,
|
| 356 |
-
height=100
|
| 357 |
-
)
|
| 358 |
-
with gr.Row(): # Moved this block to be after the video
|
| 359 |
-
with gr.Column():
|
| 360 |
post_generation_image = gr.Image(
|
| 361 |
label=english_labels["Generated Images"],
|
| 362 |
type="filepath",
|
| 363 |
elem_id="interactive",
|
| 364 |
-
elem_classes="image-display"
|
|
|
|
| 365 |
)
|
|
|
|
| 366 |
post_generation_slider = gr.Slider(
|
| 367 |
minimum=-10,
|
| 368 |
maximum=10,
|
|
@@ -376,19 +347,35 @@ with gr.Blocks(css=css) as demo:
|
|
| 376 |
examples=examples,
|
| 377 |
inputs=[prompt, concept_1, concept_2, x],
|
| 378 |
fn=generate,
|
| 379 |
-
outputs=[
|
| 380 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
cache_examples="lazy"
|
| 382 |
)
|
| 383 |
|
| 384 |
# Event Handlers
|
| 385 |
submit.click(
|
| 386 |
fn=generate,
|
| 387 |
-
inputs=[
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
)
|
| 393 |
|
| 394 |
iterations.change(fn=reset_recalc_directions, outputs=[recalc_directions])
|
|
|
|
| 12 |
from diffusers.utils import export_to_video
|
| 13 |
import random
|
| 14 |
from transformers import pipeline
|
| 15 |
+
|
| 16 |
# Translation model load
|
| 17 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
| 18 |
|
|
|
|
| 36 |
"Seed": "Seed"
|
| 37 |
}
|
| 38 |
|
| 39 |
+
# Load pipelines
|
| 40 |
base_model = "black-forest-labs/FLUX.1-schnell"
|
| 41 |
|
| 42 |
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=torch.bfloat16).to("cuda")
|
| 43 |
+
pipe = FluxPipeline.from_pretrained(
|
| 44 |
+
base_model,
|
| 45 |
+
vae=taef1,
|
| 46 |
+
torch_dtype=torch.bfloat16
|
| 47 |
+
)
|
| 48 |
pipe.transformer.to(memory_format=torch.channels_last)
|
| 49 |
clip_slider = CLIPSliderFlux(pipe, device=torch.device("cuda"))
|
| 50 |
|
| 51 |
+
MAX_SEED = 2**32 - 1
|
| 52 |
|
| 53 |
def save_images_with_unique_filenames(image_list, save_directory):
|
| 54 |
if not os.path.exists(save_directory):
|
| 55 |
os.makedirs(save_directory)
|
|
|
|
| 56 |
paths = []
|
| 57 |
for image in image_list:
|
| 58 |
unique_filename = f"{uuid.uuid4()}.png"
|
| 59 |
file_path = os.path.join(save_directory, unique_filename)
|
|
|
|
| 60 |
image.save(file_path)
|
| 61 |
paths.append(file_path)
|
|
|
|
| 62 |
return paths
|
| 63 |
|
| 64 |
def convert_to_centered_scale(num):
|
|
|
|
| 90 |
x_concept_1="", x_concept_2="",
|
| 91 |
avg_diff_x=None,
|
| 92 |
total_images=[],
|
| 93 |
+
gradio_progress=gr.Progress()):
|
|
|
|
| 94 |
# Translate prompt and concepts if Korean
|
| 95 |
prompt = translate_if_korean(prompt)
|
| 96 |
concept_1 = translate_if_korean(concept_1)
|
|
|
|
| 98 |
|
| 99 |
print(f"Prompt: {prompt}, ← {concept_2}, {concept_1} ➡️ . scale {scale}, interm steps {interm_steps}")
|
| 100 |
slider_x = [concept_2, concept_1]
|
| 101 |
+
# Re-calculate latent direction if needed
|
| 102 |
if randomize_seed:
|
| 103 |
seed = random.randint(0, MAX_SEED)
|
| 104 |
|
|
|
|
| 114 |
low_scale = -1 * scale
|
| 115 |
for i in gradio_progress.tqdm(range(interm_steps), desc="Generating images"):
|
| 116 |
cur_scale = low_scale + (high_scale - low_scale) * i / (interm_steps - 1)
|
| 117 |
+
image = clip_slider.generate(
|
| 118 |
+
prompt,
|
| 119 |
+
width=768,
|
| 120 |
+
height=768,
|
| 121 |
+
guidance_scale=guidance_scale,
|
| 122 |
+
scale=cur_scale,
|
| 123 |
+
seed=seed,
|
| 124 |
+
num_inference_steps=steps,
|
| 125 |
+
avg_diff=avg_diff
|
| 126 |
+
)
|
| 127 |
images.append(image)
|
| 128 |
+
canvas = Image.new('RGB', (256 * interm_steps, 256))
|
| 129 |
for i, im in enumerate(images):
|
| 130 |
+
canvas.paste(im.resize((256, 256)), (256 * i, 0))
|
| 131 |
|
| 132 |
comma_concepts_x = f"{slider_x[1]}, {slider_x[0]}"
|
|
|
|
| 133 |
scale_total = convert_to_centered_scale(interm_steps)
|
| 134 |
scale_min = scale_total[0]
|
| 135 |
scale_max = scale_total[-1]
|
|
|
|
| 142 |
return x_concept_1, x_concept_2, avg_diff_x, export_to_video(images, video_path, fps=5), canvas, images, images[scale_middle], post_generation_slider_update, seed
|
| 143 |
|
| 144 |
def update_pre_generated_images(slider_value, total_images):
|
| 145 |
+
number_images = len(total_images) if total_images else 0
|
|
|
|
|
|
|
| 146 |
if number_images > 0:
|
| 147 |
scale_tuple = convert_to_centered_scale(number_images)
|
| 148 |
return total_images[scale_tuple.index(slider_value)][0]
|
|
|
|
| 152 |
def reset_recalc_directions():
|
| 153 |
return True
|
| 154 |
|
| 155 |
+
# Five examples fitting the "Time Stream" theme (one Korean example included)
|
|
|
|
|
|
|
| 156 |
examples = [
|
| 157 |
+
["신선한 토마토가 부패한 토마토�� 변해가는 과정", "Fresh", "Rotten", 2.0],
|
| 158 |
+
["A blooming flower gradually withers into decay", "Bloom", "Wither", 1.5],
|
| 159 |
+
["A vibrant cityscape transforms into a derelict ruin over time", "Modern", "Ruined", 2.5],
|
| 160 |
+
["A lively forest slowly changes into an autumnal landscape", "Spring", "Autumn", 2.0],
|
| 161 |
+
["A calm ocean evolves into a stormy seascape as time passes", "Calm", "Stormy", 3.0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
]
|
| 163 |
|
| 164 |
+
# CSS for a bright and modern UI with a background image
|
|
|
|
|
|
|
| 165 |
css = """
|
| 166 |
+
/* Bright and modern UI with background image */
|
| 167 |
body {
|
| 168 |
background: #ffffff url('https://images.unsplash.com/photo-1506748686214-e9df14d4d9d0?ixlib=rb-1.2.1&auto=format&fit=crop&w=1600&q=80') no-repeat center center fixed;
|
| 169 |
background-size: cover;
|
|
|
|
| 214 |
}
|
| 215 |
"""
|
| 216 |
|
| 217 |
+
with gr.Blocks(css=css, title="타임 스트림") as demo:
|
| 218 |
+
# Title and Description
|
| 219 |
+
gr.Markdown("# 타임 스트림\nA creative journey through the transformation of images over time.")
|
| 220 |
+
|
| 221 |
x_concept_1 = gr.State("")
|
| 222 |
x_concept_2 = gr.State("")
|
| 223 |
total_images = gr.State([])
|
|
|
|
| 241 |
concept_1 = gr.Textbox(
|
| 242 |
label=english_labels["1st direction to steer"],
|
| 243 |
info="Initial state",
|
| 244 |
+
placeholder="Fresh"
|
| 245 |
)
|
| 246 |
with gr.Column(scale=1):
|
| 247 |
concept_2 = gr.Textbox(
|
| 248 |
label=english_labels["2nd direction to steer"],
|
| 249 |
info="Final state",
|
| 250 |
+
placeholder="Rotten"
|
| 251 |
)
|
|
|
|
| 252 |
with gr.Row(elem_classes="slider-container"):
|
| 253 |
x = gr.Slider(
|
| 254 |
minimum=0,
|
|
|
|
| 258 |
label=english_labels["Strength"],
|
| 259 |
info="Maximum strength for each direction (above 2.5 may be unstable)"
|
| 260 |
)
|
|
|
|
| 261 |
submit = gr.Button(english_labels["Generate directions"], size="lg", variant="primary")
|
| 262 |
|
| 263 |
# Advanced Options Panel
|
|
|
|
| 279 |
step=0.1,
|
| 280 |
value=3.5
|
| 281 |
)
|
|
|
|
| 282 |
with gr.Row():
|
| 283 |
with gr.Column(scale=1):
|
| 284 |
iterations = gr.Slider(
|
|
|
|
| 296 |
maximum=4,
|
| 297 |
step=1
|
| 298 |
)
|
|
|
|
| 299 |
with gr.Row():
|
| 300 |
with gr.Column(scale=1):
|
| 301 |
randomize_seed = gr.Checkbox(
|
|
|
|
| 313 |
)
|
| 314 |
|
| 315 |
# Right Column - Output
|
| 316 |
+
with gr.Column(scale=8):
|
| 317 |
with gr.Group(elem_classes="main-panel"):
|
| 318 |
gr.Markdown("### Generated Results")
|
| 319 |
+
# Video output on top (bigger) and image output below
|
| 320 |
+
output_video = gr.Video(
|
| 321 |
+
label=english_labels["Looping video"],
|
| 322 |
+
elem_id="video",
|
| 323 |
+
loop=True,
|
| 324 |
+
autoplay=True,
|
| 325 |
+
height=400
|
| 326 |
+
)
|
| 327 |
with gr.Row():
|
| 328 |
+
with gr.Column(scale=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
post_generation_image = gr.Image(
|
| 330 |
label=english_labels["Generated Images"],
|
| 331 |
type="filepath",
|
| 332 |
elem_id="interactive",
|
| 333 |
+
elem_classes="image-display",
|
| 334 |
+
height=200
|
| 335 |
)
|
| 336 |
+
with gr.Column(scale=1):
|
| 337 |
post_generation_slider = gr.Slider(
|
| 338 |
minimum=-10,
|
| 339 |
maximum=10,
|
|
|
|
| 347 |
examples=examples,
|
| 348 |
inputs=[prompt, concept_1, concept_2, x],
|
| 349 |
fn=generate,
|
| 350 |
+
outputs=[
|
| 351 |
+
x_concept_1, x_concept_2, avg_diff_x,
|
| 352 |
+
output_video, # video output (larger)
|
| 353 |
+
canvas, # image strip (below video)
|
| 354 |
+
total_images,
|
| 355 |
+
post_generation_image,
|
| 356 |
+
post_generation_slider,
|
| 357 |
+
seed
|
| 358 |
+
],
|
| 359 |
cache_examples="lazy"
|
| 360 |
)
|
| 361 |
|
| 362 |
# Event Handlers
|
| 363 |
submit.click(
|
| 364 |
fn=generate,
|
| 365 |
+
inputs=[
|
| 366 |
+
prompt, concept_1, concept_2, x, randomize_seed, seed,
|
| 367 |
+
recalc_directions, iterations, steps, interm_steps,
|
| 368 |
+
guidance_scale, x_concept_1, x_concept_2, avg_diff_x, total_images
|
| 369 |
+
],
|
| 370 |
+
outputs=[
|
| 371 |
+
x_concept_1, x_concept_2, avg_diff_x,
|
| 372 |
+
output_video, # video output (larger)
|
| 373 |
+
canvas, # image strip (below video)
|
| 374 |
+
total_images,
|
| 375 |
+
post_generation_image,
|
| 376 |
+
post_generation_slider,
|
| 377 |
+
seed
|
| 378 |
+
]
|
| 379 |
)
|
| 380 |
|
| 381 |
iterations.change(fn=reset_recalc_directions, outputs=[recalc_directions])
|