Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -85,24 +85,30 @@ def resize_image(image, resolution):
|
|
85 |
new_h = int(h * ratio)
|
86 |
return image.resize((new_w, new_h), Image.LANCZOS)
|
87 |
|
88 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
width, height = 1024, 576
|
90 |
image = Image.new("RGB", (width, height), "white")
|
91 |
draw = ImageDraw.Draw(image)
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
if os.path.exists(font_path):
|
98 |
-
try:
|
99 |
-
font = ImageFont.truetype(font_path, size=size)
|
100 |
-
print(f"Using font: {font_file}")
|
101 |
-
break
|
102 |
-
except IOError:
|
103 |
-
print(f"Error loading font: {font_file}")
|
104 |
-
if font is None:
|
105 |
-
print("No suitable font found. Using default font.")
|
106 |
font = ImageFont.load_default()
|
107 |
|
108 |
lines = text.split('\n')
|
@@ -153,7 +159,7 @@ def text_to_image(text, size=72, position="middle-center"):
|
|
153 |
return image
|
154 |
|
155 |
@spaces.GPU
|
156 |
-
def infer_canny(prompt, text_for_image, text_position, font_size,
|
157 |
negative_prompt = "nsfw, facial shadows, low resolution, jpeg artifacts, blurry, bad quality, dark face, neon lights",
|
158 |
seed = 397886929,
|
159 |
randomize_seed = False,
|
@@ -171,8 +177,8 @@ def infer_canny(prompt, text_for_image, text_position, font_size,
|
|
171 |
seed = random.randint(0, MAX_SEED)
|
172 |
generator = torch.Generator().manual_seed(seed)
|
173 |
|
174 |
-
# Generate text image
|
175 |
-
init_image = text_to_image(text_for_image, size=font_size, position=text_position)
|
176 |
init_image = resize_image(init_image, MAX_IMAGE_SIZE)
|
177 |
|
178 |
pipe = pipe_canny.to("cuda")
|
@@ -191,7 +197,7 @@ def infer_canny(prompt, text_for_image, text_position, font_size,
|
|
191 |
num_images_per_prompt=1,
|
192 |
generator=generator,
|
193 |
).images[0]
|
194 |
-
return image, seed
|
195 |
|
196 |
def update_button_states(selected_position):
|
197 |
return [
|
@@ -199,6 +205,20 @@ def update_button_states(selected_position):
|
|
199 |
for pos in position_list
|
200 |
]
|
201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
position_list = [
|
203 |
"top-left", "top-left-center", "top-center", "top-right-center", "top-right",
|
204 |
"upper-left", "upper-left-center", "upper-center", "upper-right-center", "upper-right",
|
@@ -207,7 +227,6 @@ position_list = [
|
|
207 |
"bottom-left", "bottom-left-center", "bottom-center", "bottom-right-center", "bottom-right"
|
208 |
]
|
209 |
|
210 |
-
|
211 |
css = """
|
212 |
footer {
|
213 |
visibility: hidden;
|
@@ -247,14 +266,14 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as Kolors:
|
|
247 |
label="Prompt",
|
248 |
placeholder="Enter your prompt",
|
249 |
lines=2,
|
250 |
-
value="coffee in a cup bokeh --ar 85:128 --v 6.0 --style raw5, 4K, 리얼리티 사진"
|
251 |
)
|
252 |
with gr.Row():
|
253 |
text_for_image = gr.Textbox(
|
254 |
label="Text for Image Generation",
|
255 |
placeholder="Enter text to be converted into an image",
|
256 |
lines=3,
|
257 |
-
value="대한 萬世 GO"
|
258 |
)
|
259 |
with gr.Row():
|
260 |
with gr.Column():
|
@@ -273,6 +292,15 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as Kolors:
|
|
273 |
step=1,
|
274 |
value=72
|
275 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
with gr.Accordion("Advanced Settings", open=False):
|
277 |
negative_prompt = gr.Textbox(
|
278 |
label="Negative prompt",
|
@@ -329,18 +357,22 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as Kolors:
|
|
329 |
with gr.Row():
|
330 |
canny_button = gr.Button("Start", elem_id="button")
|
331 |
|
332 |
-
|
333 |
with gr.Column(elem_id="col-right"):
|
334 |
-
result = gr.Image(label="Result", show_label=False)
|
335 |
seed_used = gr.Number(label="Seed Used")
|
336 |
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
|
|
341 |
)
|
342 |
-
|
343 |
|
|
|
|
|
|
|
|
|
|
|
344 |
|
345 |
# Set initial button states
|
346 |
Kolors.load(update_button_states, inputs=[text_position], outputs=position_buttons)
|
|
|
85 |
new_h = int(h * ratio)
|
86 |
return image.resize((new_w, new_h), Image.LANCZOS)
|
87 |
|
88 |
+
def get_font_files():
|
89 |
+
return [f for f in os.listdir() if f.endswith('.ttf')]
|
90 |
+
|
91 |
+
def preview_font(font_name, preview_text):
|
92 |
+
font_size = 24
|
93 |
+
image = Image.new('RGB', (400, 100), color='white')
|
94 |
+
draw = ImageDraw.Draw(image)
|
95 |
+
try:
|
96 |
+
font = ImageFont.truetype(font_name, font_size)
|
97 |
+
draw.text((10, 10), f"{font_name}:", font=font, fill='black')
|
98 |
+
draw.text((10, 40), preview_text, font=font, fill='black')
|
99 |
+
except IOError:
|
100 |
+
draw.text((10, 10), f"Error loading font: {font_name}", fill='black')
|
101 |
+
return image
|
102 |
+
|
103 |
+
def text_to_image(text, size=72, position="middle-center", font_name="Arial_Unicode.ttf"):
|
104 |
width, height = 1024, 576
|
105 |
image = Image.new("RGB", (width, height), "white")
|
106 |
draw = ImageDraw.Draw(image)
|
107 |
|
108 |
+
try:
|
109 |
+
font = ImageFont.truetype(font_name, size=size)
|
110 |
+
except IOError:
|
111 |
+
print(f"Error loading font: {font_name}. Using default font.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
font = ImageFont.load_default()
|
113 |
|
114 |
lines = text.split('\n')
|
|
|
159 |
return image
|
160 |
|
161 |
@spaces.GPU
|
162 |
+
def infer_canny(prompt, text_for_image, text_position, font_size, font_name,
|
163 |
negative_prompt = "nsfw, facial shadows, low resolution, jpeg artifacts, blurry, bad quality, dark face, neon lights",
|
164 |
seed = 397886929,
|
165 |
randomize_seed = False,
|
|
|
177 |
seed = random.randint(0, MAX_SEED)
|
178 |
generator = torch.Generator().manual_seed(seed)
|
179 |
|
180 |
+
# Generate text image with selected font
|
181 |
+
init_image = text_to_image(text_for_image, size=font_size, position=text_position, font_name=font_name)
|
182 |
init_image = resize_image(init_image, MAX_IMAGE_SIZE)
|
183 |
|
184 |
pipe = pipe_canny.to("cuda")
|
|
|
197 |
num_images_per_prompt=1,
|
198 |
generator=generator,
|
199 |
).images[0]
|
200 |
+
return image, seed
|
201 |
|
202 |
def update_button_states(selected_position):
|
203 |
return [
|
|
|
205 |
for pos in position_list
|
206 |
]
|
207 |
|
208 |
+
|
209 |
+
def generate_font_preview(font_name):
|
210 |
+
font_size = 24
|
211 |
+
image = Image.new('RGB', (400, 100), color='white')
|
212 |
+
draw = ImageDraw.Draw(image)
|
213 |
+
try:
|
214 |
+
font = ImageFont.truetype(font_name, font_size)
|
215 |
+
draw.text((10, 10), f"{font_name}:", font=font, fill='black')
|
216 |
+
draw.text((10, 40), "AaBbCc 123 가나다", font=font, fill='black')
|
217 |
+
except IOError:
|
218 |
+
draw.text((10, 10), f"Error loading font: {font_name}", fill='black')
|
219 |
+
return image
|
220 |
+
|
221 |
+
|
222 |
position_list = [
|
223 |
"top-left", "top-left-center", "top-center", "top-right-center", "top-right",
|
224 |
"upper-left", "upper-left-center", "upper-center", "upper-right-center", "upper-right",
|
|
|
227 |
"bottom-left", "bottom-left-center", "bottom-center", "bottom-right-center", "bottom-right"
|
228 |
]
|
229 |
|
|
|
230 |
css = """
|
231 |
footer {
|
232 |
visibility: hidden;
|
|
|
266 |
label="Prompt",
|
267 |
placeholder="Enter your prompt",
|
268 |
lines=2,
|
269 |
+
value="coffee in a cup bokeh --ar 85:128 --v 6.0 --style raw5, 4K, 리얼리티 사진"
|
270 |
)
|
271 |
with gr.Row():
|
272 |
text_for_image = gr.Textbox(
|
273 |
label="Text for Image Generation",
|
274 |
placeholder="Enter text to be converted into an image",
|
275 |
lines=3,
|
276 |
+
value="대한 萬世 GO"
|
277 |
)
|
278 |
with gr.Row():
|
279 |
with gr.Column():
|
|
|
292 |
step=1,
|
293 |
value=72
|
294 |
)
|
295 |
+
with gr.Row():
|
296 |
+
font_dropdown = gr.Dropdown(choices=get_font_files(), label="Select Font", value="Arial_Unicode.ttf")
|
297 |
+
|
298 |
+
with gr.Row():
|
299 |
+
preview_text = gr.Textbox(label="Preview Text", value="Hello, World!")
|
300 |
+
preview_button = gr.Button("Preview Font")
|
301 |
+
|
302 |
+
font_preview = gr.Image(label="Font Preview")
|
303 |
+
|
304 |
with gr.Accordion("Advanced Settings", open=False):
|
305 |
negative_prompt = gr.Textbox(
|
306 |
label="Negative prompt",
|
|
|
357 |
with gr.Row():
|
358 |
canny_button = gr.Button("Start", elem_id="button")
|
359 |
|
|
|
360 |
with gr.Column(elem_id="col-right"):
|
361 |
+
result = gr.Image(label="Result", show_label=False)
|
362 |
seed_used = gr.Number(label="Seed Used")
|
363 |
|
364 |
+
# Update font preview when button is clicked
|
365 |
+
preview_button.click(
|
366 |
+
fn=preview_font,
|
367 |
+
inputs=[font_dropdown, preview_text],
|
368 |
+
outputs=font_preview
|
369 |
)
|
|
|
370 |
|
371 |
+
canny_button.click(
|
372 |
+
fn=infer_canny,
|
373 |
+
inputs=[prompt, text_for_image, text_position, font_size, font_dropdown, negative_prompt, seed, randomize_seed, guidance_scale, num_inference_steps, controlnet_conditioning_scale, control_guidance_end, strength],
|
374 |
+
outputs=[result, seed_used]
|
375 |
+
)
|
376 |
|
377 |
# Set initial button states
|
378 |
Kolors.load(update_button_states, inputs=[text_position], outputs=position_buttons)
|