Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -86,56 +86,115 @@ def augment_prompt_with_llm(prompt):
|
|
| 86 |
print(f"Error calling LLM API: {e}")
|
| 87 |
return prompt
|
| 88 |
|
| 89 |
-
def
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
# Create a copy of the image to work with
|
| 93 |
img_with_text = image.copy()
|
| 94 |
draw = ImageDraw.Draw(img_with_text)
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
title_font_size = 48
|
| 100 |
-
author_font_size = 32
|
| 101 |
-
# For production, you'd want to include proper font files
|
| 102 |
-
title_font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf", title_font_size)
|
| 103 |
-
author_font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf", author_font_size)
|
| 104 |
-
except:
|
| 105 |
-
# Fallback to default font
|
| 106 |
-
title_font = ImageFont.load_default()
|
| 107 |
-
author_font = ImageFont.load_default()
|
| 108 |
|
| 109 |
# Get image dimensions
|
| 110 |
img_width, img_height = img_with_text.size
|
| 111 |
|
| 112 |
# Define position mappings
|
| 113 |
position_coords = {
|
| 114 |
-
"
|
| 115 |
-
"
|
| 116 |
-
"
|
| 117 |
}
|
| 118 |
|
| 119 |
-
# Draw title
|
| 120 |
-
if title_ko
|
| 121 |
-
title_text = f"{title_ko}\n{title_en}" if title_ko and title_en else title_ko or title_en
|
| 122 |
title_x, title_y = position_coords[title_position]
|
| 123 |
|
| 124 |
# Get text bbox for centering
|
| 125 |
-
bbox = draw.textbbox((0, 0),
|
| 126 |
text_width = bbox[2] - bbox[0]
|
| 127 |
text_height = bbox[3] - bbox[1]
|
| 128 |
|
| 129 |
# Draw text with shadow for better visibility
|
| 130 |
shadow_offset = 2
|
| 131 |
draw.text((title_x - text_width // 2 + shadow_offset, title_y - text_height // 2 + shadow_offset),
|
| 132 |
-
|
| 133 |
draw.text((title_x - text_width // 2, title_y - text_height // 2),
|
| 134 |
-
|
| 135 |
|
| 136 |
-
# Draw author
|
| 137 |
-
if author_ko
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
author_x, author_y = position_coords[author_position]
|
| 140 |
|
| 141 |
# Get text bbox for centering
|
|
@@ -145,9 +204,9 @@ def add_text_overlay(image, title_ko, title_en, author_ko, author_en,
|
|
| 145 |
|
| 146 |
# Draw text with shadow
|
| 147 |
draw.text((author_x - text_width // 2 + shadow_offset, author_y - text_height // 2 + shadow_offset),
|
| 148 |
-
author_text, font=author_font, fill="black"
|
| 149 |
draw.text((author_x - text_width // 2, author_y - text_height // 2),
|
| 150 |
-
author_text, font=author_font, fill=text_color
|
| 151 |
|
| 152 |
return img_with_text
|
| 153 |
|
|
@@ -194,12 +253,13 @@ def inference(
|
|
| 194 |
num_inference_steps: int,
|
| 195 |
lora_scale: float,
|
| 196 |
title_ko: str,
|
| 197 |
-
title_en: str,
|
| 198 |
author_ko: str,
|
| 199 |
-
author_en: str,
|
| 200 |
title_position: str,
|
| 201 |
author_position: str,
|
| 202 |
text_color: str,
|
|
|
|
|
|
|
|
|
|
| 203 |
progress: gr.Progress = gr.Progress(track_tqdm=True),
|
| 204 |
):
|
| 205 |
if randomize_seed:
|
|
@@ -216,10 +276,11 @@ def inference(
|
|
| 216 |
joint_attention_kwargs={"scale": lora_scale},
|
| 217 |
).images[0]
|
| 218 |
|
| 219 |
-
# Add text overlay if any text is provided
|
| 220 |
-
if
|
| 221 |
-
image = add_text_overlay(image, title_ko,
|
| 222 |
-
title_position, author_position, text_color
|
|
|
|
| 223 |
|
| 224 |
# Save the generated image
|
| 225 |
filepath = save_generated_image(image, prompt)
|
|
@@ -278,29 +339,47 @@ with gr.Blocks(theme=gr.themes.Soft(), analytics_enabled=False) as demo:
|
|
| 278 |
value=DEFAULT_IMAGE_PATH # Set the default image
|
| 279 |
)
|
| 280 |
|
| 281 |
-
with gr.Accordion("Text Overlay Settings", open=False):
|
| 282 |
with gr.Row():
|
| 283 |
with gr.Column():
|
| 284 |
-
title_ko = gr.Textbox(label="
|
| 285 |
-
title_en = gr.Textbox(label="Title (English)", placeholder="English Title")
|
| 286 |
title_position = gr.Radio(
|
| 287 |
-
label="
|
| 288 |
-
choices=["
|
| 289 |
-
value="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
)
|
| 291 |
with gr.Column():
|
| 292 |
-
author_ko = gr.Textbox(label="
|
| 293 |
-
author_en = gr.Textbox(label="Author (English)", placeholder="Author Name")
|
| 294 |
author_position = gr.Radio(
|
| 295 |
-
label="
|
| 296 |
-
choices=["
|
| 297 |
-
value="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
)
|
| 299 |
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
|
| 305 |
with gr.Accordion("Advanced Settings", open=False):
|
| 306 |
seed = gr.Slider(
|
|
@@ -412,12 +491,13 @@ with gr.Blocks(theme=gr.themes.Soft(), analytics_enabled=False) as demo:
|
|
| 412 |
num_inference_steps,
|
| 413 |
lora_scale,
|
| 414 |
title_ko,
|
| 415 |
-
title_en,
|
| 416 |
author_ko,
|
| 417 |
-
author_en,
|
| 418 |
title_position,
|
| 419 |
author_position,
|
| 420 |
text_color,
|
|
|
|
|
|
|
|
|
|
| 421 |
],
|
| 422 |
outputs=[result, seed, generated_gallery],
|
| 423 |
)
|
|
|
|
| 86 |
print(f"Error calling LLM API: {e}")
|
| 87 |
return prompt
|
| 88 |
|
| 89 |
+
def get_korean_font(font_name, font_size):
|
| 90 |
+
"""Get Korean font based on font name"""
|
| 91 |
+
font_paths = {
|
| 92 |
+
"λλκ³ λ": [
|
| 93 |
+
"/usr/share/fonts/truetype/nanum/NanumGothic.ttf",
|
| 94 |
+
"NanumGothic.ttf",
|
| 95 |
+
"/System/Library/Fonts/AppleSDGothicNeo.ttc", # macOS
|
| 96 |
+
"C:/Windows/Fonts/NanumGothic.ttf", # Windows
|
| 97 |
+
],
|
| 98 |
+
"λλλͺ
μ‘°": [
|
| 99 |
+
"/usr/share/fonts/truetype/nanum/NanumMyeongjo.ttf",
|
| 100 |
+
"NanumMyeongjo.ttf",
|
| 101 |
+
"C:/Windows/Fonts/NanumMyeongjo.ttf", # Windows
|
| 102 |
+
],
|
| 103 |
+
"λ§μ κ³ λ": [
|
| 104 |
+
"/usr/share/fonts/truetype/malgun/malgun.ttf",
|
| 105 |
+
"malgun.ttf",
|
| 106 |
+
"/System/Library/Fonts/AppleMyungjo.ttf", # macOS
|
| 107 |
+
"C:/Windows/Fonts/malgun.ttf", # Windows
|
| 108 |
+
],
|
| 109 |
+
"λ°ν": [
|
| 110 |
+
"/usr/share/fonts/truetype/batang/batang.ttf",
|
| 111 |
+
"batang.ttf",
|
| 112 |
+
"C:/Windows/Fonts/batang.ttc", # Windows
|
| 113 |
+
],
|
| 114 |
+
"λμ": [
|
| 115 |
+
"/usr/share/fonts/truetype/dotum/dotum.ttf",
|
| 116 |
+
"dotum.ttf",
|
| 117 |
+
"C:/Windows/Fonts/dotum.ttc", # Windows
|
| 118 |
+
],
|
| 119 |
+
"κΈ°λ³Έ": []
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
# Try to load the selected font
|
| 123 |
+
if font_name in font_paths:
|
| 124 |
+
for font_path in font_paths[font_name]:
|
| 125 |
+
try:
|
| 126 |
+
return ImageFont.truetype(font_path, font_size)
|
| 127 |
+
except:
|
| 128 |
+
continue
|
| 129 |
+
|
| 130 |
+
# Try common font directories
|
| 131 |
+
common_paths = [
|
| 132 |
+
f"/usr/share/fonts/truetype/{font_name.lower()}/{font_name}.ttf",
|
| 133 |
+
f"fonts/{font_name}.ttf",
|
| 134 |
+
f"{font_name}.ttf",
|
| 135 |
+
]
|
| 136 |
+
|
| 137 |
+
for path in common_paths:
|
| 138 |
+
try:
|
| 139 |
+
return ImageFont.truetype(path, font_size)
|
| 140 |
+
except:
|
| 141 |
+
continue
|
| 142 |
+
|
| 143 |
+
# Fallback to default font with larger size
|
| 144 |
+
try:
|
| 145 |
+
return ImageFont.truetype("DejaVuSans.ttf", font_size)
|
| 146 |
+
except:
|
| 147 |
+
# Use default font but try to make it larger
|
| 148 |
+
default = ImageFont.load_default()
|
| 149 |
+
# PIL default font is very small, so we might need to scale the text
|
| 150 |
+
return default
|
| 151 |
+
|
| 152 |
+
def add_text_overlay(image, title_ko, author_ko,
|
| 153 |
+
title_position, author_position, text_color, font_name,
|
| 154 |
+
title_size, author_size):
|
| 155 |
+
"""Add Korean text overlay to the generated image"""
|
| 156 |
# Create a copy of the image to work with
|
| 157 |
img_with_text = image.copy()
|
| 158 |
draw = ImageDraw.Draw(img_with_text)
|
| 159 |
|
| 160 |
+
# Load Korean fonts with custom sizes
|
| 161 |
+
title_font = get_korean_font(font_name, title_size)
|
| 162 |
+
author_font = get_korean_font(font_name, author_size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
# Get image dimensions
|
| 165 |
img_width, img_height = img_with_text.size
|
| 166 |
|
| 167 |
# Define position mappings
|
| 168 |
position_coords = {
|
| 169 |
+
"μλ¨": (img_width // 2, img_height // 10),
|
| 170 |
+
"μ€μ": (img_width // 2, img_height // 2),
|
| 171 |
+
"νλ¨": (img_width // 2, img_height * 9 // 10)
|
| 172 |
}
|
| 173 |
|
| 174 |
+
# Draw title (Korean only)
|
| 175 |
+
if title_ko:
|
|
|
|
| 176 |
title_x, title_y = position_coords[title_position]
|
| 177 |
|
| 178 |
# Get text bbox for centering
|
| 179 |
+
bbox = draw.textbbox((0, 0), title_ko, font=title_font)
|
| 180 |
text_width = bbox[2] - bbox[0]
|
| 181 |
text_height = bbox[3] - bbox[1]
|
| 182 |
|
| 183 |
# Draw text with shadow for better visibility
|
| 184 |
shadow_offset = 2
|
| 185 |
draw.text((title_x - text_width // 2 + shadow_offset, title_y - text_height // 2 + shadow_offset),
|
| 186 |
+
title_ko, font=title_font, fill="black")
|
| 187 |
draw.text((title_x - text_width // 2, title_y - text_height // 2),
|
| 188 |
+
title_ko, font=title_font, fill=text_color)
|
| 189 |
|
| 190 |
+
# Draw author (Korean only)
|
| 191 |
+
if author_ko:
|
| 192 |
+
# Add "μ§μμ΄: " prefix if not already present
|
| 193 |
+
if not author_ko.startswith("μ§μμ΄"):
|
| 194 |
+
author_text = f"μ§μμ΄: {author_ko}"
|
| 195 |
+
else:
|
| 196 |
+
author_text = author_ko
|
| 197 |
+
|
| 198 |
author_x, author_y = position_coords[author_position]
|
| 199 |
|
| 200 |
# Get text bbox for centering
|
|
|
|
| 204 |
|
| 205 |
# Draw text with shadow
|
| 206 |
draw.text((author_x - text_width // 2 + shadow_offset, author_y - text_height // 2 + shadow_offset),
|
| 207 |
+
author_text, font=author_font, fill="black")
|
| 208 |
draw.text((author_x - text_width // 2, author_y - text_height // 2),
|
| 209 |
+
author_text, font=author_font, fill=text_color)
|
| 210 |
|
| 211 |
return img_with_text
|
| 212 |
|
|
|
|
| 253 |
num_inference_steps: int,
|
| 254 |
lora_scale: float,
|
| 255 |
title_ko: str,
|
|
|
|
| 256 |
author_ko: str,
|
|
|
|
| 257 |
title_position: str,
|
| 258 |
author_position: str,
|
| 259 |
text_color: str,
|
| 260 |
+
font_name: str,
|
| 261 |
+
title_size: int,
|
| 262 |
+
author_size: int,
|
| 263 |
progress: gr.Progress = gr.Progress(track_tqdm=True),
|
| 264 |
):
|
| 265 |
if randomize_seed:
|
|
|
|
| 276 |
joint_attention_kwargs={"scale": lora_scale},
|
| 277 |
).images[0]
|
| 278 |
|
| 279 |
+
# Add text overlay if any Korean text is provided
|
| 280 |
+
if title_ko or author_ko:
|
| 281 |
+
image = add_text_overlay(image, title_ko, author_ko,
|
| 282 |
+
title_position, author_position, text_color, font_name,
|
| 283 |
+
title_size, author_size)
|
| 284 |
|
| 285 |
# Save the generated image
|
| 286 |
filepath = save_generated_image(image, prompt)
|
|
|
|
| 339 |
value=DEFAULT_IMAGE_PATH # Set the default image
|
| 340 |
)
|
| 341 |
|
| 342 |
+
with gr.Accordion("Text Overlay Settings (νκΈ)", open=False):
|
| 343 |
with gr.Row():
|
| 344 |
with gr.Column():
|
| 345 |
+
title_ko = gr.Textbox(label="μ λͺ©", placeholder="νκΈ μ λͺ©μ μ
λ ₯νμΈμ")
|
|
|
|
| 346 |
title_position = gr.Radio(
|
| 347 |
+
label="μ λͺ© μμΉ",
|
| 348 |
+
choices=["μλ¨", "μ€μ", "νλ¨"],
|
| 349 |
+
value="μλ¨"
|
| 350 |
+
)
|
| 351 |
+
title_size = gr.Slider(
|
| 352 |
+
label="μ λͺ© κΈμ ν¬κΈ°",
|
| 353 |
+
minimum=20,
|
| 354 |
+
maximum=100,
|
| 355 |
+
value=48,
|
| 356 |
+
step=2
|
| 357 |
)
|
| 358 |
with gr.Column():
|
| 359 |
+
author_ko = gr.Textbox(label="μ§μμ΄", placeholder="μ§μμ΄ μ΄λ¦μ μ
λ ₯νμΈμ")
|
|
|
|
| 360 |
author_position = gr.Radio(
|
| 361 |
+
label="μ§μμ΄ μμΉ",
|
| 362 |
+
choices=["μλ¨", "μ€μ", "νλ¨"],
|
| 363 |
+
value="νλ¨"
|
| 364 |
+
)
|
| 365 |
+
author_size = gr.Slider(
|
| 366 |
+
label="μ§μμ΄ κΈμ ν¬κΈ°",
|
| 367 |
+
minimum=16,
|
| 368 |
+
maximum=60,
|
| 369 |
+
value=32,
|
| 370 |
+
step=2
|
| 371 |
)
|
| 372 |
|
| 373 |
+
with gr.Row():
|
| 374 |
+
font_name = gr.Dropdown(
|
| 375 |
+
label="ν°νΈ μ ν",
|
| 376 |
+
choices=["λλκ³ λ", "λλλͺ
μ‘°", "λ§μ κ³ λ", "λ°ν", "λμ", "κΈ°λ³Έ"],
|
| 377 |
+
value="λλκ³ λ"
|
| 378 |
+
)
|
| 379 |
+
text_color = gr.ColorPicker(
|
| 380 |
+
label="κΈμ μμ",
|
| 381 |
+
value="#FFFFFF"
|
| 382 |
+
)
|
| 383 |
|
| 384 |
with gr.Accordion("Advanced Settings", open=False):
|
| 385 |
seed = gr.Slider(
|
|
|
|
| 491 |
num_inference_steps,
|
| 492 |
lora_scale,
|
| 493 |
title_ko,
|
|
|
|
| 494 |
author_ko,
|
|
|
|
| 495 |
title_position,
|
| 496 |
author_position,
|
| 497 |
text_color,
|
| 498 |
+
font_name,
|
| 499 |
+
title_size,
|
| 500 |
+
author_size,
|
| 501 |
],
|
| 502 |
outputs=[result, seed, generated_gallery],
|
| 503 |
)
|