Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -85,11 +85,11 @@ def resize_image(image, resolution):
|
|
| 85 |
new_h = int(h * ratio)
|
| 86 |
return image.resize((new_w, new_h), Image.LANCZOS)
|
| 87 |
|
| 88 |
-
def text_to_image(text, size=72, position="
|
| 89 |
width, height = 1024, 576
|
| 90 |
image = Image.new("RGB", (width, height), "white")
|
| 91 |
draw = ImageDraw.Draw(image)
|
| 92 |
-
|
| 93 |
font_files = ["Arial_Unicode.ttf"]
|
| 94 |
font = None
|
| 95 |
for font_file in font_files:
|
|
@@ -119,17 +119,33 @@ def text_to_image(text, size=72, position="top-center"):
|
|
| 119 |
|
| 120 |
position_mapping = {
|
| 121 |
"top-left": (10, 10),
|
|
|
|
| 122 |
"top-center": ((width - max_line_width) / 2, 10),
|
|
|
|
| 123 |
"top-right": (width - max_line_width - 10, 10),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
"middle-left": (10, (height - total_height) / 2),
|
|
|
|
| 125 |
"middle-center": ((width - max_line_width) / 2, (height - total_height) / 2),
|
|
|
|
| 126 |
"middle-right": (width - max_line_width - 10, (height - total_height) / 2),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
"bottom-left": (10, height - total_height - 10),
|
|
|
|
| 128 |
"bottom-center": ((width - max_line_width) / 2, height - total_height - 10),
|
|
|
|
| 129 |
"bottom-right": (width - max_line_width - 10, height - total_height - 10),
|
| 130 |
}
|
| 131 |
|
| 132 |
-
x, y = position_mapping.get(position, ((width - max_line_width) / 2, height - total_height
|
| 133 |
for i, line in enumerate(lines):
|
| 134 |
draw.text((x, y), line, fill="black", font=font)
|
| 135 |
y += line_heights[i]
|
|
@@ -149,15 +165,15 @@ def infer_canny(prompt, text_for_image, text_position, font_size,
|
|
| 149 |
):
|
| 150 |
prompt = translate_korean_to_english(prompt)
|
| 151 |
negative_prompt = translate_korean_to_english(negative_prompt)
|
| 152 |
-
|
| 153 |
if randomize_seed:
|
| 154 |
seed = random.randint(0, MAX_SEED)
|
| 155 |
generator = torch.Generator().manual_seed(seed)
|
| 156 |
-
|
| 157 |
# Generate text image
|
| 158 |
init_image = text_to_image(text_for_image, size=font_size, position=text_position)
|
| 159 |
init_image = resize_image(init_image, MAX_IMAGE_SIZE)
|
| 160 |
-
|
| 161 |
pipe = pipe_canny.to("cuda")
|
| 162 |
condi_img = process_canny_condition(init_image)
|
| 163 |
image = pipe(
|
|
@@ -175,16 +191,16 @@ def infer_canny(prompt, text_for_image, text_position, font_size,
|
|
| 175 |
).images[0]
|
| 176 |
return [condi_img, image], seed
|
| 177 |
|
| 178 |
-
|
| 179 |
css = """
|
| 180 |
footer {
|
| 181 |
visibility: hidden;
|
| 182 |
}
|
| 183 |
.text-position-grid {
|
| 184 |
display: grid;
|
| 185 |
-
grid-template-columns: repeat(
|
| 186 |
-
gap:
|
| 187 |
margin-bottom: 10px;
|
|
|
|
| 188 |
}
|
| 189 |
.text-position-grid button {
|
| 190 |
aspect-ratio: 1;
|
|
@@ -192,24 +208,36 @@ footer {
|
|
| 192 |
border: 1px solid #ccc;
|
| 193 |
background-color: #f0f0f0;
|
| 194 |
cursor: pointer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
}
|
| 196 |
.text-position-grid button.selected {
|
| 197 |
background-color: #007bff;
|
| 198 |
color: white;
|
|
|
|
| 199 |
}
|
| 200 |
"""
|
| 201 |
|
| 202 |
def update_button_states(selected_position):
|
| 203 |
return [
|
| 204 |
gr.Button.update(variant="primary" if pos == selected_position else "secondary")
|
| 205 |
-
for pos in
|
| 206 |
-
"middle-left", "middle-center", "middle-right",
|
| 207 |
-
"bottom-left", "bottom-center", "bottom-right"]
|
| 208 |
]
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as Kolors:
|
| 211 |
-
text_position = gr.State("
|
| 212 |
-
|
| 213 |
with gr.Row():
|
| 214 |
with gr.Column(elem_id="col-left"):
|
| 215 |
with gr.Row():
|
|
@@ -228,23 +256,9 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as Kolors:
|
|
| 228 |
with gr.Column():
|
| 229 |
gr.Markdown("Text Position")
|
| 230 |
with gr.Row(elem_classes="text-position-grid"):
|
| 231 |
-
|
| 232 |
-
btn_top_center = gr.Button("↑")
|
| 233 |
-
btn_top_right = gr.Button("↗")
|
| 234 |
-
btn_middle_left = gr.Button("←")
|
| 235 |
-
btn_middle_center = gr.Button("•")
|
| 236 |
-
btn_middle_right = gr.Button("→")
|
| 237 |
-
btn_bottom_left = gr.Button("↙")
|
| 238 |
-
btn_bottom_center = gr.Button("↓")
|
| 239 |
-
btn_bottom_right = gr.Button("↘")
|
| 240 |
-
|
| 241 |
-
position_buttons = [btn_top_left, btn_top_center, btn_top_right,
|
| 242 |
-
btn_middle_left, btn_middle_center, btn_middle_right,
|
| 243 |
-
btn_bottom_left, btn_bottom_center, btn_bottom_right]
|
| 244 |
|
| 245 |
-
for btn, pos in zip(position_buttons,
|
| 246 |
-
"middle-left", "middle-center", "middle-right",
|
| 247 |
-
"bottom-left", "bottom-center", "bottom-right"]):
|
| 248 |
btn.click(lambda x, p=pos: p, outputs=text_position)
|
| 249 |
btn.click(update_button_states, inputs=[text_position], outputs=position_buttons)
|
| 250 |
|
|
@@ -325,4 +339,4 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as Kolors:
|
|
| 325 |
# Set initial button states
|
| 326 |
Kolors.load(update_button_states, inputs=[text_position], outputs=position_buttons)
|
| 327 |
|
| 328 |
-
Kolors.queue().launch(debug=True, share=True)
|
|
|
|
| 85 |
new_h = int(h * ratio)
|
| 86 |
return image.resize((new_w, new_h), Image.LANCZOS)
|
| 87 |
|
| 88 |
+
def text_to_image(text, size=72, position="middle-center"):
|
| 89 |
width, height = 1024, 576
|
| 90 |
image = Image.new("RGB", (width, height), "white")
|
| 91 |
draw = ImageDraw.Draw(image)
|
| 92 |
+
|
| 93 |
font_files = ["Arial_Unicode.ttf"]
|
| 94 |
font = None
|
| 95 |
for font_file in font_files:
|
|
|
|
| 119 |
|
| 120 |
position_mapping = {
|
| 121 |
"top-left": (10, 10),
|
| 122 |
+
"top-left-center": (width // 4 - max_line_width // 2, 10),
|
| 123 |
"top-center": ((width - max_line_width) / 2, 10),
|
| 124 |
+
"top-right-center": (3 * width // 4 - max_line_width // 2, 10),
|
| 125 |
"top-right": (width - max_line_width - 10, 10),
|
| 126 |
+
"upper-left": (10, height // 4 - total_height // 2),
|
| 127 |
+
"upper-left-center": (width // 4 - max_line_width // 2, height // 4 - total_height // 2),
|
| 128 |
+
"upper-center": ((width - max_line_width) / 2, height // 4 - total_height // 2),
|
| 129 |
+
"upper-right-center": (3 * width // 4 - max_line_width // 2, height // 4 - total_height // 2),
|
| 130 |
+
"upper-right": (width - max_line_width - 10, height // 4 - total_height // 2),
|
| 131 |
"middle-left": (10, (height - total_height) / 2),
|
| 132 |
+
"middle-left-center": (width // 4 - max_line_width // 2, (height - total_height) / 2),
|
| 133 |
"middle-center": ((width - max_line_width) / 2, (height - total_height) / 2),
|
| 134 |
+
"middle-right-center": (3 * width // 4 - max_line_width // 2, (height - total_height) / 2),
|
| 135 |
"middle-right": (width - max_line_width - 10, (height - total_height) / 2),
|
| 136 |
+
"lower-left": (10, 3 * height // 4 - total_height // 2),
|
| 137 |
+
"lower-left-center": (width // 4 - max_line_width // 2, 3 * height // 4 - total_height // 2),
|
| 138 |
+
"lower-center": ((width - max_line_width) / 2, 3 * height // 4 - total_height // 2),
|
| 139 |
+
"lower-right-center": (3 * width // 4 - max_line_width // 2, 3 * height // 4 - total_height // 2),
|
| 140 |
+
"lower-right": (width - max_line_width - 10, 3 * height // 4 - total_height // 2),
|
| 141 |
"bottom-left": (10, height - total_height - 10),
|
| 142 |
+
"bottom-left-center": (width // 4 - max_line_width // 2, height - total_height - 10),
|
| 143 |
"bottom-center": ((width - max_line_width) / 2, height - total_height - 10),
|
| 144 |
+
"bottom-right-center": (3 * width // 4 - max_line_width // 2, height - total_height - 10),
|
| 145 |
"bottom-right": (width - max_line_width - 10, height - total_height - 10),
|
| 146 |
}
|
| 147 |
|
| 148 |
+
x, y = position_mapping.get(position, ((width - max_line_width) / 2, (height - total_height) / 2))
|
| 149 |
for i, line in enumerate(lines):
|
| 150 |
draw.text((x, y), line, fill="black", font=font)
|
| 151 |
y += line_heights[i]
|
|
|
|
| 165 |
):
|
| 166 |
prompt = translate_korean_to_english(prompt)
|
| 167 |
negative_prompt = translate_korean_to_english(negative_prompt)
|
| 168 |
+
|
| 169 |
if randomize_seed:
|
| 170 |
seed = random.randint(0, MAX_SEED)
|
| 171 |
generator = torch.Generator().manual_seed(seed)
|
| 172 |
+
|
| 173 |
# Generate text image
|
| 174 |
init_image = text_to_image(text_for_image, size=font_size, position=text_position)
|
| 175 |
init_image = resize_image(init_image, MAX_IMAGE_SIZE)
|
| 176 |
+
|
| 177 |
pipe = pipe_canny.to("cuda")
|
| 178 |
condi_img = process_canny_condition(init_image)
|
| 179 |
image = pipe(
|
|
|
|
| 191 |
).images[0]
|
| 192 |
return [condi_img, image], seed
|
| 193 |
|
|
|
|
| 194 |
css = """
|
| 195 |
footer {
|
| 196 |
visibility: hidden;
|
| 197 |
}
|
| 198 |
.text-position-grid {
|
| 199 |
display: grid;
|
| 200 |
+
grid-template-columns: repeat(5, 1fr);
|
| 201 |
+
gap: 2px;
|
| 202 |
margin-bottom: 10px;
|
| 203 |
+
width: 150px;
|
| 204 |
}
|
| 205 |
.text-position-grid button {
|
| 206 |
aspect-ratio: 1;
|
|
|
|
| 208 |
border: 1px solid #ccc;
|
| 209 |
background-color: #f0f0f0;
|
| 210 |
cursor: pointer;
|
| 211 |
+
font-size: 10px;
|
| 212 |
+
transition: all 0.3s ease;
|
| 213 |
+
}
|
| 214 |
+
.text-position-grid button:hover {
|
| 215 |
+
background-color: #e0e0e0;
|
| 216 |
}
|
| 217 |
.text-position-grid button.selected {
|
| 218 |
background-color: #007bff;
|
| 219 |
color: white;
|
| 220 |
+
transform: scale(1.1);
|
| 221 |
}
|
| 222 |
"""
|
| 223 |
|
| 224 |
def update_button_states(selected_position):
|
| 225 |
return [
|
| 226 |
gr.Button.update(variant="primary" if pos == selected_position else "secondary")
|
| 227 |
+
for pos in position_list
|
|
|
|
|
|
|
| 228 |
]
|
| 229 |
|
| 230 |
+
position_list = [
|
| 231 |
+
"top-left", "top-left-center", "top-center", "top-right-center", "top-right",
|
| 232 |
+
"upper-left", "upper-left-center", "upper-center", "upper-right-center", "upper-right",
|
| 233 |
+
"middle-left", "middle-left-center", "middle-center", "middle-right-center", "middle-right",
|
| 234 |
+
"lower-left", "lower-left-center", "lower-center", "lower-right-center", "lower-right",
|
| 235 |
+
"bottom-left", "bottom-left-center", "bottom-center", "bottom-right-center", "bottom-right"
|
| 236 |
+
]
|
| 237 |
+
|
| 238 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as Kolors:
|
| 239 |
+
text_position = gr.State("middle-center")
|
| 240 |
+
|
| 241 |
with gr.Row():
|
| 242 |
with gr.Column(elem_id="col-left"):
|
| 243 |
with gr.Row():
|
|
|
|
| 256 |
with gr.Column():
|
| 257 |
gr.Markdown("Text Position")
|
| 258 |
with gr.Row(elem_classes="text-position-grid"):
|
| 259 |
+
position_buttons = [gr.Button("•") for _ in range(25)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
+
for btn, pos in zip(position_buttons, position_list):
|
|
|
|
|
|
|
| 262 |
btn.click(lambda x, p=pos: p, outputs=text_position)
|
| 263 |
btn.click(update_button_states, inputs=[text_position], outputs=position_buttons)
|
| 264 |
|
|
|
|
| 339 |
# Set initial button states
|
| 340 |
Kolors.load(update_button_states, inputs=[text_position], outputs=position_buttons)
|
| 341 |
|
| 342 |
+
Kolors.queue().launch(debug=True, share=True)
|