Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -13,143 +13,30 @@ from diffusers.utils import export_to_video
|
|
13 |
import random
|
14 |
from transformers import pipeline
|
15 |
|
16 |
-
#
|
17 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
"Prompt": "
|
22 |
-
"1st direction to steer": "
|
23 |
-
"2nd direction to steer": "
|
24 |
-
"Strength": "
|
25 |
-
"Generate directions": "
|
26 |
-
"Generated Images": "
|
27 |
-
"From 1st to 2nd direction": "
|
28 |
-
"Strip": "
|
29 |
-
"Looping video": "
|
30 |
-
"Advanced options": "
|
31 |
-
"Num of intermediate images": "
|
32 |
-
"Num iterations for clip directions": "
|
33 |
-
"Num inference steps": "
|
34 |
-
"Guidance scale": "
|
35 |
-
"Randomize seed": "
|
36 |
-
"Seed": "
|
37 |
}
|
38 |
|
39 |
-
#
|
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(base_model,
|
44 |
-
vae=taef1,
|
45 |
-
torch_dtype=torch.bfloat16)
|
46 |
-
|
47 |
-
pipe.transformer.to(memory_format=torch.channels_last)
|
48 |
-
clip_slider = CLIPSliderFlux(pipe, device=torch.device("cuda"))
|
49 |
-
|
50 |
-
MAX_SEED = 2**32-1
|
51 |
-
|
52 |
-
def save_images_with_unique_filenames(image_list, save_directory):
|
53 |
-
if not os.path.exists(save_directory):
|
54 |
-
os.makedirs(save_directory)
|
55 |
-
|
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 |
-
|
61 |
-
image.save(file_path)
|
62 |
-
paths.append(file_path)
|
63 |
-
|
64 |
-
return paths
|
65 |
-
|
66 |
-
def convert_to_centered_scale(num):
|
67 |
-
if num % 2 == 0: # even
|
68 |
-
start = -(num // 2 - 1)
|
69 |
-
end = num // 2
|
70 |
-
else: # odd
|
71 |
-
start = -(num // 2)
|
72 |
-
end = num // 2
|
73 |
-
return tuple(range(start, end + 1))
|
74 |
-
|
75 |
-
def translate_if_korean(text):
|
76 |
-
if any('\u3131' <= char <= '\u3163' or '\uac00' <= char <= '\ud7a3' for char in text):
|
77 |
-
return translator(text)[0]['translation_text']
|
78 |
-
return text
|
79 |
-
|
80 |
-
@spaces.GPU(duration=85)
|
81 |
-
def generate(prompt,
|
82 |
-
concept_1,
|
83 |
-
concept_2,
|
84 |
-
scale,
|
85 |
-
randomize_seed=True,
|
86 |
-
seed=42,
|
87 |
-
recalc_directions=True,
|
88 |
-
iterations=200,
|
89 |
-
steps=3,
|
90 |
-
interm_steps=33,
|
91 |
-
guidance_scale=3.5,
|
92 |
-
x_concept_1="", x_concept_2="",
|
93 |
-
avg_diff_x=None,
|
94 |
-
total_images=[],
|
95 |
-
progress=gr.Progress()
|
96 |
-
):
|
97 |
-
# 프롬프트와 컨셉 번역
|
98 |
-
prompt = translate_if_korean(prompt)
|
99 |
-
concept_1 = translate_if_korean(concept_1)
|
100 |
-
concept_2 = translate_if_korean(concept_2)
|
101 |
-
|
102 |
-
print(f"Prompt: {prompt}, ← {concept_2}, {concept_1} ➡️ . scale {scale}, interm steps {interm_steps}")
|
103 |
-
slider_x = [concept_2, concept_1]
|
104 |
-
# check if avg diff for directions need to be re-calculated
|
105 |
-
if randomize_seed:
|
106 |
-
seed = random.randint(0, MAX_SEED)
|
107 |
-
|
108 |
-
if not sorted(slider_x) == sorted([x_concept_1, x_concept_2]) or recalc_directions:
|
109 |
-
progress(0, desc="Calculating directions...")
|
110 |
-
avg_diff = clip_slider.find_latent_direction(slider_x[0], slider_x[1], num_iterations=iterations)
|
111 |
-
x_concept_1, x_concept_2 = slider_x[0], slider_x[1]
|
112 |
-
|
113 |
-
images = []
|
114 |
-
high_scale = scale
|
115 |
-
low_scale = -1 * scale
|
116 |
-
for i in progress.tqdm(range(interm_steps), desc="Generating images"):
|
117 |
-
cur_scale = low_scale + (high_scale - low_scale) * i / (interm_steps - 1)
|
118 |
-
image = clip_slider.generate(prompt,
|
119 |
-
width=768,
|
120 |
-
height=768,
|
121 |
-
guidance_scale=guidance_scale,
|
122 |
-
scale=cur_scale, seed=seed, num_inference_steps=steps, avg_diff=avg_diff)
|
123 |
-
images.append(image)
|
124 |
-
canvas = Image.new('RGB', (256*interm_steps, 256))
|
125 |
-
for i, im in enumerate(images):
|
126 |
-
canvas.paste(im.resize((256,256)), (256 * i, 0))
|
127 |
-
|
128 |
-
comma_concepts_x = f"{slider_x[1]}, {slider_x[0]}"
|
129 |
-
|
130 |
-
scale_total = convert_to_centered_scale(interm_steps)
|
131 |
-
scale_min = scale_total[0]
|
132 |
-
scale_max = scale_total[-1]
|
133 |
-
scale_middle = scale_total.index(0)
|
134 |
-
post_generation_slider_update = gr.update(label=comma_concepts_x, value=0, minimum=scale_min, maximum=scale_max, interactive=True)
|
135 |
-
avg_diff_x = avg_diff.cpu()
|
136 |
-
|
137 |
-
video_path = f"{uuid.uuid4()}.mp4"
|
138 |
-
print(video_path)
|
139 |
-
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
|
140 |
-
|
141 |
-
def update_pre_generated_images(slider_value, total_images):
|
142 |
-
number_images = len(total_images)
|
143 |
-
if(number_images > 0):
|
144 |
-
scale_tuple = convert_to_centered_scale(number_images)
|
145 |
-
return total_images[scale_tuple.index(slider_value)][0]
|
146 |
-
else:
|
147 |
-
return None
|
148 |
-
|
149 |
-
def reset_recalc_directions():
|
150 |
-
return True
|
151 |
-
|
152 |
-
examples = [["flower in mountain", "spring", "winter", 1.5], ["남자", "아기", "노인", 2.5], ["a tomato", "super fresh", "rotten", 2.5]]
|
153 |
|
154 |
css = """
|
155 |
footer {
|
@@ -157,79 +44,106 @@ footer {
|
|
157 |
}
|
158 |
"""
|
159 |
|
160 |
-
|
|
|
161 |
x_concept_1 = gr.State("")
|
162 |
x_concept_2 = gr.State("")
|
163 |
total_images = gr.Gallery(visible=False)
|
164 |
|
165 |
avg_diff_x = gr.State()
|
166 |
-
|
167 |
recalc_directions = gr.State(False)
|
168 |
|
169 |
with gr.Row():
|
170 |
with gr.Column():
|
171 |
with gr.Group():
|
172 |
-
prompt = gr.Textbox(label=
|
|
|
|
|
173 |
with gr.Row():
|
174 |
-
concept_1 = gr.Textbox(label=
|
175 |
-
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
with gr.Column():
|
179 |
with gr.Group(elem_id="group"):
|
180 |
-
post_generation_image = gr.Image(label=
|
181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
with gr.Row():
|
183 |
with gr.Column(scale=4):
|
184 |
-
image_seq = gr.Image(label=
|
|
|
|
|
185 |
with gr.Column(scale=2, min_width=100):
|
186 |
-
output_image = gr.Video(label=
|
187 |
-
|
188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
with gr.Row():
|
190 |
-
iterations = gr.Slider(label=
|
191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
with gr.Row():
|
193 |
guidance_scale = gr.Slider(
|
194 |
-
label=
|
195 |
minimum=0.1,
|
196 |
maximum=10.0,
|
197 |
step=0.1,
|
198 |
value=3.5,
|
199 |
)
|
200 |
with gr.Column():
|
201 |
-
randomize_seed = gr.Checkbox(True, label=
|
202 |
-
seed = gr.Slider(minimum=0,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
examples_gradio = gr.Examples(
|
205 |
examples=examples,
|
206 |
inputs=[prompt, concept_1, concept_2, x],
|
207 |
fn=generate,
|
208 |
-
outputs=[x_concept_1, x_concept_2, avg_diff_x, output_image, image_seq, total_images,
|
|
|
209 |
cache_examples="lazy"
|
210 |
)
|
211 |
|
212 |
-
|
213 |
-
|
214 |
-
inputs=[prompt, concept_1, concept_2, x, randomize_seed, seed, recalc_directions, iterations, steps, interm_steps, guidance_scale, x_concept_1, x_concept_2, avg_diff_x, total_images],
|
215 |
-
outputs=[x_concept_1, x_concept_2, avg_diff_x, output_image, image_seq, total_images, post_generation_image, post_generation_slider, seed]
|
216 |
-
)
|
217 |
-
iterations.change(
|
218 |
-
fn=reset_recalc_directions,
|
219 |
-
outputs=[recalc_directions]
|
220 |
-
)
|
221 |
-
seed.change(
|
222 |
-
fn=reset_recalc_directions,
|
223 |
-
outputs=[recalc_directions]
|
224 |
-
)
|
225 |
-
post_generation_slider.change(
|
226 |
-
fn=update_pre_generated_images,
|
227 |
-
inputs=[post_generation_slider, total_images],
|
228 |
-
outputs=[post_generation_image],
|
229 |
-
queue=False,
|
230 |
-
show_progress="hidden",
|
231 |
-
concurrency_limit=None
|
232 |
-
)
|
233 |
-
|
234 |
if __name__ == "__main__":
|
235 |
demo.launch()
|
|
|
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 |
|
19 |
+
# English menu labels
|
20 |
+
english_labels = {
|
21 |
+
"Prompt": "Prompt",
|
22 |
+
"1st direction to steer": "1st Direction",
|
23 |
+
"2nd direction to steer": "2nd Direction",
|
24 |
+
"Strength": "Strength",
|
25 |
+
"Generate directions": "Generate Directions",
|
26 |
+
"Generated Images": "Generated Images",
|
27 |
+
"From 1st to 2nd direction": "From 1st to 2nd Direction",
|
28 |
+
"Strip": "Image Strip",
|
29 |
+
"Looping video": "Looping Video",
|
30 |
+
"Advanced options": "Advanced Options",
|
31 |
+
"Num of intermediate images": "Number of Intermediate Images",
|
32 |
+
"Num iterations for clip directions": "Number of CLIP Direction Iterations",
|
33 |
+
"Num inference steps": "Number of Inference Steps",
|
34 |
+
"Guidance scale": "Guidance Scale",
|
35 |
+
"Randomize seed": "Randomize Seed",
|
36 |
+
"Seed": "Seed"
|
37 |
}
|
38 |
|
39 |
+
# [Rest of the imports and pipeline setup remains the same...]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
css = """
|
42 |
footer {
|
|
|
44 |
}
|
45 |
"""
|
46 |
|
47 |
+
|
48 |
+
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
49 |
x_concept_1 = gr.State("")
|
50 |
x_concept_2 = gr.State("")
|
51 |
total_images = gr.Gallery(visible=False)
|
52 |
|
53 |
avg_diff_x = gr.State()
|
|
|
54 |
recalc_directions = gr.State(False)
|
55 |
|
56 |
with gr.Row():
|
57 |
with gr.Column():
|
58 |
with gr.Group():
|
59 |
+
prompt = gr.Textbox(label=english_labels["Prompt"],
|
60 |
+
info="Enter the description",
|
61 |
+
placeholder="A dog in the park")
|
62 |
with gr.Row():
|
63 |
+
concept_1 = gr.Textbox(label=english_labels["1st direction to steer"],
|
64 |
+
info="Initial state",
|
65 |
+
placeholder="winter")
|
66 |
+
concept_2 = gr.Textbox(label=english_labels["2nd direction to steer"],
|
67 |
+
info="Final state",
|
68 |
+
placeholder="summer")
|
69 |
+
x = gr.Slider(minimum=0,
|
70 |
+
value=1.75,
|
71 |
+
step=0.1,
|
72 |
+
maximum=4.0,
|
73 |
+
label=english_labels["Strength"],
|
74 |
+
info="Maximum strength for each direction (above 2.5 may be unstable)")
|
75 |
+
submit = gr.Button(english_labels["Generate directions"])
|
76 |
with gr.Column():
|
77 |
with gr.Group(elem_id="group"):
|
78 |
+
post_generation_image = gr.Image(label=english_labels["Generated Images"],
|
79 |
+
type="filepath",
|
80 |
+
elem_id="interactive")
|
81 |
+
post_generation_slider = gr.Slider(minimum=-10,
|
82 |
+
maximum=10,
|
83 |
+
value=0,
|
84 |
+
step=1,
|
85 |
+
label=english_labels["From 1st to 2nd direction"])
|
86 |
with gr.Row():
|
87 |
with gr.Column(scale=4):
|
88 |
+
image_seq = gr.Image(label=english_labels["Strip"],
|
89 |
+
elem_id="strip",
|
90 |
+
height=80)
|
91 |
with gr.Column(scale=2, min_width=100):
|
92 |
+
output_image = gr.Video(label=english_labels["Looping video"],
|
93 |
+
elem_id="video",
|
94 |
+
loop=True,
|
95 |
+
autoplay=True)
|
96 |
+
with gr.Accordion(label=english_labels["Advanced options"], open=False):
|
97 |
+
interm_steps = gr.Slider(label=english_labels["Num of intermediate images"],
|
98 |
+
minimum=3,
|
99 |
+
value=7,
|
100 |
+
maximum=65,
|
101 |
+
step=2)
|
102 |
with gr.Row():
|
103 |
+
iterations = gr.Slider(label=english_labels["Num iterations for clip directions"],
|
104 |
+
minimum=0,
|
105 |
+
value=200,
|
106 |
+
maximum=400,
|
107 |
+
step=1)
|
108 |
+
steps = gr.Slider(label=english_labels["Num inference steps"],
|
109 |
+
minimum=1,
|
110 |
+
value=3,
|
111 |
+
maximum=4,
|
112 |
+
step=1)
|
113 |
with gr.Row():
|
114 |
guidance_scale = gr.Slider(
|
115 |
+
label=english_labels["Guidance scale"],
|
116 |
minimum=0.1,
|
117 |
maximum=10.0,
|
118 |
step=0.1,
|
119 |
value=3.5,
|
120 |
)
|
121 |
with gr.Column():
|
122 |
+
randomize_seed = gr.Checkbox(True, label=english_labels["Randomize seed"])
|
123 |
+
seed = gr.Slider(minimum=0,
|
124 |
+
maximum=MAX_SEED,
|
125 |
+
step=1,
|
126 |
+
label=english_labels["Seed"],
|
127 |
+
interactive=True,
|
128 |
+
randomize=True)
|
129 |
+
|
130 |
+
# Updated examples with English text
|
131 |
+
examples = [
|
132 |
+
["flower in mountain", "spring", "winter", 1.5],
|
133 |
+
["man", "baby", "elderly", 2.5],
|
134 |
+
["a tomato", "super fresh", "rotten", 2.5]
|
135 |
+
]
|
136 |
|
137 |
examples_gradio = gr.Examples(
|
138 |
examples=examples,
|
139 |
inputs=[prompt, concept_1, concept_2, x],
|
140 |
fn=generate,
|
141 |
+
outputs=[x_concept_1, x_concept_2, avg_diff_x, output_image, image_seq, total_images,
|
142 |
+
post_generation_image, post_generation_slider, seed],
|
143 |
cache_examples="lazy"
|
144 |
)
|
145 |
|
146 |
+
# [Rest of the event handlers remain the same...]
|
147 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
if __name__ == "__main__":
|
149 |
demo.launch()
|