Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -86,17 +86,27 @@ def augment_prompt_with_llm(prompt):
|
|
86 |
print(f"Error calling LLM API: {e}")
|
87 |
return prompt
|
88 |
|
89 |
-
def get_korean_font(font_size):
|
90 |
-
"""Load
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
try:
|
92 |
-
# Try to load
|
93 |
-
|
94 |
-
return ImageFont.truetype(font_path, font_size)
|
95 |
except:
|
96 |
# If font file is not found, try alternative paths
|
97 |
alternative_paths = [
|
98 |
-
"./
|
99 |
-
os.path.join(os.path.dirname(__file__),
|
100 |
]
|
101 |
|
102 |
for path in alternative_paths:
|
@@ -105,12 +115,16 @@ def get_korean_font(font_size):
|
|
105 |
except:
|
106 |
continue
|
107 |
|
108 |
-
#
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
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,8 +132,8 @@ def add_text_overlay(image, title_ko, author_ko,
|
|
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,6 +231,7 @@ def inference(
|
|
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,7 +253,7 @@ def inference(
|
|
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
|
@@ -454,6 +469,7 @@ with gr.Blocks(theme=gr.themes.Soft(), analytics_enabled=False) as demo:
|
|
454 |
title_position,
|
455 |
author_position,
|
456 |
text_color,
|
|
|
457 |
title_size,
|
458 |
author_size,
|
459 |
],
|
|
|
86 |
print(f"Error calling LLM API: {e}")
|
87 |
return prompt
|
88 |
|
89 |
+
def get_korean_font(font_name, font_size):
|
90 |
+
"""Load Korean font from the same directory as app.py"""
|
91 |
+
font_files = {
|
92 |
+
"๋๋๊ณ ๋": "NanumGothic-Regular.ttf",
|
93 |
+
"ํ๋ฐฑ์ฌ์ง": "BlackAndWhitePicture-Regular.ttf",
|
94 |
+
"์น๋ก ์ฑ": "ChironSungHK-VariableFont_wght.ttf",
|
95 |
+
"๋
๋": "Dokdo-Regular.ttf",
|
96 |
+
"์ฑ๊ธ๋ฐ์ด": "SingleDay-Regular.ttf"
|
97 |
+
}
|
98 |
+
|
99 |
+
# Get the font file name
|
100 |
+
font_file = font_files.get(font_name, "NanumGothic-Regular.ttf")
|
101 |
+
|
102 |
try:
|
103 |
+
# Try to load font from the same directory
|
104 |
+
return ImageFont.truetype(font_file, font_size)
|
|
|
105 |
except:
|
106 |
# If font file is not found, try alternative paths
|
107 |
alternative_paths = [
|
108 |
+
f"./{font_file}",
|
109 |
+
os.path.join(os.path.dirname(__file__), font_file),
|
110 |
]
|
111 |
|
112 |
for path in alternative_paths:
|
|
|
115 |
except:
|
116 |
continue
|
117 |
|
118 |
+
# If specific font not found, try to load NanumGothic as fallback
|
119 |
+
try:
|
120 |
+
return ImageFont.truetype("NanumGothic-Regular.ttf", font_size)
|
121 |
+
except:
|
122 |
+
# Final fallback to default font
|
123 |
+
print(f"Warning: {font_file} not found. Using default font.")
|
124 |
+
return ImageFont.load_default()
|
125 |
|
126 |
def add_text_overlay(image, title_ko, author_ko,
|
127 |
+
title_position, author_position, text_color, font_name,
|
128 |
title_size, author_size):
|
129 |
"""Add Korean text overlay to the generated image"""
|
130 |
# Create a copy of the image to work with
|
|
|
132 |
draw = ImageDraw.Draw(img_with_text)
|
133 |
|
134 |
# Load Korean fonts with custom sizes
|
135 |
+
title_font = get_korean_font(font_name, title_size)
|
136 |
+
author_font = get_korean_font(font_name, author_size)
|
137 |
|
138 |
# Get image dimensions
|
139 |
img_width, img_height = img_with_text.size
|
|
|
231 |
title_position: str,
|
232 |
author_position: str,
|
233 |
text_color: str,
|
234 |
+
font_name: str,
|
235 |
title_size: int,
|
236 |
author_size: int,
|
237 |
progress: gr.Progress = gr.Progress(track_tqdm=True),
|
|
|
253 |
# Add text overlay if any Korean text is provided
|
254 |
if title_ko or author_ko:
|
255 |
image = add_text_overlay(image, title_ko, author_ko,
|
256 |
+
title_position, author_position, text_color, font_name,
|
257 |
title_size, author_size)
|
258 |
|
259 |
# Save the generated image
|
|
|
469 |
title_position,
|
470 |
author_position,
|
471 |
text_color,
|
472 |
+
font_name,
|
473 |
title_size,
|
474 |
author_size,
|
475 |
],
|