ginipick commited on
Commit
90456d7
Β·
verified Β·
1 Parent(s): 289e506

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -52
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 add_text_overlay(image, title_ko, title_en, author_ko, author_en,
90
- title_position, author_position, text_color):
91
- """Add text overlay to the generated image"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- # Try to load a better font, fallback to default if not available
97
- try:
98
- # You may need to adjust the font path based on your system
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
- "Top": (img_width // 2, img_height // 10),
115
- "Center": (img_width // 2, img_height // 2),
116
- "Bottom": (img_width // 2, img_height * 9 // 10)
117
  }
118
 
119
- # Draw title
120
- if title_ko or title_en:
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), title_text, font=title_font)
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
- title_text, font=title_font, fill="black", align="center")
133
  draw.text((title_x - text_width // 2, title_y - text_height // 2),
134
- title_text, font=title_font, fill=text_color, align="center")
135
 
136
- # Draw author
137
- if author_ko or author_en:
138
- author_text = f"{author_ko}\n{author_en}" if author_ko and author_en else author_ko or author_en
 
 
 
 
 
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", align="center")
149
  draw.text((author_x - text_width // 2, author_y - text_height // 2),
150
- author_text, font=author_font, fill=text_color, align="center")
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 any([title_ko, title_en, author_ko, author_en]):
221
- image = add_text_overlay(image, title_ko, title_en, author_ko, author_en,
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="Title (Korean)", placeholder="ν•œκΈ€ 제λͺ©")
285
- title_en = gr.Textbox(label="Title (English)", placeholder="English Title")
286
  title_position = gr.Radio(
287
- label="Title Position",
288
- choices=["Top", "Center", "Bottom"],
289
- value="Top"
 
 
 
 
 
 
 
290
  )
291
  with gr.Column():
292
- author_ko = gr.Textbox(label="Author (Korean)", placeholder="지은이")
293
- author_en = gr.Textbox(label="Author (English)", placeholder="Author Name")
294
  author_position = gr.Radio(
295
- label="Author Position",
296
- choices=["Top", "Center", "Bottom"],
297
- value="Bottom"
 
 
 
 
 
 
 
298
  )
299
 
300
- text_color = gr.ColorPicker(
301
- label="Text Color",
302
- value="#FFFFFF"
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
  )