ginipick commited on
Commit
ff70eba
ยท
verified ยท
1 Parent(s): 4d7b88b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -67
app.py CHANGED
@@ -86,71 +86,31 @@ def augment_prompt_with_llm(prompt):
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
@@ -158,8 +118,8 @@ def add_text_overlay(image, title_ko, author_ko,
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
@@ -257,7 +217,6 @@ def inference(
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),
@@ -279,7 +238,7 @@ def inference(
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
@@ -352,7 +311,7 @@ with gr.Blocks(theme=gr.themes.Soft(), analytics_enabled=False) as demo:
352
  label="์ œ๋ชฉ ๊ธ€์ž ํฌ๊ธฐ",
353
  minimum=20,
354
  maximum=100,
355
- value=100,
356
  step=2
357
  )
358
  with gr.Column():
@@ -366,7 +325,7 @@ with gr.Blocks(theme=gr.themes.Soft(), analytics_enabled=False) as demo:
366
  label="์ง€์€์ด ๊ธ€์ž ํฌ๊ธฐ",
367
  minimum=16,
368
  maximum=60,
369
- value=24,
370
  step=2
371
  )
372
 
@@ -495,7 +454,6 @@ with gr.Blocks(theme=gr.themes.Soft(), analytics_enabled=False) as demo:
495
  title_position,
496
  author_position,
497
  text_color,
498
- font_name,
499
  title_size,
500
  author_size,
501
  ],
 
86
  print(f"Error calling LLM API: {e}")
87
  return prompt
88
 
89
+ def get_korean_font(font_size):
90
+ """Load NanumGothic font from the same directory as app.py"""
91
+ try:
92
+ # Try to load NanumGothic-Regular.ttf from the same directory
93
+ font_path = "NanumGothic-Regular.ttf"
94
+ return ImageFont.truetype(font_path, font_size)
95
+ except:
96
+ # If font file is not found, try alternative paths
97
+ alternative_paths = [
98
+ "./NanumGothic-Regular.ttf",
99
+ os.path.join(os.path.dirname(__file__), "NanumGothic-Regular.ttf"),
100
+ ]
101
+
102
+ for path in alternative_paths:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  try:
104
+ return ImageFont.truetype(path, font_size)
105
  except:
106
  continue
107
+
108
+ # Final fallback to default font
109
+ print("Warning: NanumGothic-Regular.ttf not found. Using default font.")
110
+ return ImageFont.load_default()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
  def add_text_overlay(image, title_ko, author_ko,
113
+ title_position, author_position, text_color,
114
  title_size, author_size):
115
  """Add Korean text overlay to the generated image"""
116
  # Create a copy of the image to work with
 
118
  draw = ImageDraw.Draw(img_with_text)
119
 
120
  # Load Korean fonts with custom sizes
121
+ title_font = get_korean_font(title_size)
122
+ author_font = get_korean_font(author_size)
123
 
124
  # Get image dimensions
125
  img_width, img_height = img_with_text.size
 
217
  title_position: str,
218
  author_position: str,
219
  text_color: str,
 
220
  title_size: int,
221
  author_size: int,
222
  progress: gr.Progress = gr.Progress(track_tqdm=True),
 
238
  # Add text overlay if any Korean text is provided
239
  if title_ko or author_ko:
240
  image = add_text_overlay(image, title_ko, author_ko,
241
+ title_position, author_position, text_color,
242
  title_size, author_size)
243
 
244
  # Save the generated image
 
311
  label="์ œ๋ชฉ ๊ธ€์ž ํฌ๊ธฐ",
312
  minimum=20,
313
  maximum=100,
314
+ value=48,
315
  step=2
316
  )
317
  with gr.Column():
 
325
  label="์ง€์€์ด ๊ธ€์ž ํฌ๊ธฐ",
326
  minimum=16,
327
  maximum=60,
328
+ value=32,
329
  step=2
330
  )
331
 
 
454
  title_position,
455
  author_position,
456
  text_color,
 
457
  title_size,
458
  author_size,
459
  ],