Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,47 @@ import requests
|
|
12 |
import json
|
13 |
import re
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Create permanent storage directory
|
16 |
SAVE_DIR = "saved_images" # Gradio will handle the persistence
|
17 |
if not os.path.exists(SAVE_DIR):
|
@@ -98,35 +139,46 @@ def get_korean_font(font_name, font_size):
|
|
98 |
|
99 |
# Get the font file name
|
100 |
font_file = font_files.get(font_name, "NanumGothic-Regular.ttf")
|
|
|
101 |
|
102 |
-
try
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
#
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
119 |
try:
|
120 |
-
|
|
|
|
|
121 |
except:
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
|
|
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
|
131 |
img_with_text = image.copy()
|
132 |
draw = ImageDraw.Draw(img_with_text)
|
@@ -150,9 +202,13 @@ def add_text_overlay(image, title_ko, author_ko,
|
|
150 |
title_x, title_y = position_coords[title_position]
|
151 |
|
152 |
# Get text bbox for centering
|
153 |
-
|
154 |
-
|
155 |
-
|
|
|
|
|
|
|
|
|
156 |
|
157 |
# Draw text with shadow for better visibility
|
158 |
shadow_offset = 2
|
@@ -172,9 +228,13 @@ def add_text_overlay(image, title_ko, author_ko,
|
|
172 |
author_x, author_y = position_coords[author_position]
|
173 |
|
174 |
# Get text bbox for centering
|
175 |
-
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
|
|
178 |
|
179 |
# Draw text with shadow
|
180 |
draw.text((author_x - text_width // 2 + shadow_offset, author_y - text_height // 2 + shadow_offset),
|
@@ -236,6 +296,8 @@ def inference(
|
|
236 |
author_size: int,
|
237 |
progress: gr.Progress = gr.Progress(track_tqdm=True),
|
238 |
):
|
|
|
|
|
239 |
if randomize_seed:
|
240 |
seed = random.randint(0, MAX_SEED)
|
241 |
generator = torch.Generator(device=device).manual_seed(seed)
|
@@ -252,6 +314,7 @@ def inference(
|
|
252 |
|
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)
|
@@ -302,6 +365,15 @@ with gr.Blocks(theme=gr.themes.Soft(), analytics_enabled=False) as demo:
|
|
302 |
max_lines=1,
|
303 |
placeholder="Enter your prompt",
|
304 |
container=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
305 |
)
|
306 |
augment_button = gr.Button("μ¦κ°", scale=0)
|
307 |
run_button = gr.Button("Run", scale=0)
|
|
|
12 |
import json
|
13 |
import re
|
14 |
|
15 |
+
# At the beginning of the script, after imports
|
16 |
+
def check_available_fonts():
|
17 |
+
"""Check which font files are available"""
|
18 |
+
font_files = {
|
19 |
+
"λλκ³ λ": "NanumGothic-Regular.ttf",
|
20 |
+
"νλ°±μ¬μ§": "BlackAndWhitePicture-Regular.ttf",
|
21 |
+
"μΉλ‘ μ±": "ChironSungHK-VariableFont_wght.ttf",
|
22 |
+
"λ
λ": "Dokdo-Regular.ttf",
|
23 |
+
"μ±κΈλ°μ΄": "SingleDay-Regular.ttf"
|
24 |
+
}
|
25 |
+
|
26 |
+
available_fonts = []
|
27 |
+
for font_name, font_file in font_files.items():
|
28 |
+
paths = [
|
29 |
+
font_file,
|
30 |
+
f"./{font_file}",
|
31 |
+
os.path.join(os.getcwd(), font_file),
|
32 |
+
os.path.join(os.path.dirname(os.path.abspath(__file__)), font_file)
|
33 |
+
]
|
34 |
+
|
35 |
+
font_found = False
|
36 |
+
for path in paths:
|
37 |
+
if os.path.exists(path):
|
38 |
+
available_fonts.append(font_name)
|
39 |
+
print(f"β Found {font_name} at: {path}")
|
40 |
+
font_found = True
|
41 |
+
break
|
42 |
+
|
43 |
+
if not font_found:
|
44 |
+
print(f"β {font_name} ({font_file}) not found")
|
45 |
+
|
46 |
+
return available_fonts
|
47 |
+
|
48 |
+
# Check fonts at startup
|
49 |
+
print("Checking available fonts...")
|
50 |
+
available_fonts = check_available_fonts()
|
51 |
+
if not available_fonts:
|
52 |
+
print("WARNING: No Korean fonts found! Please ensure font files are in the same directory as app.py")
|
53 |
+
else:
|
54 |
+
print(f"Available fonts: {', '.join(available_fonts)}")
|
55 |
+
|
56 |
# Create permanent storage directory
|
57 |
SAVE_DIR = "saved_images" # Gradio will handle the persistence
|
58 |
if not os.path.exists(SAVE_DIR):
|
|
|
139 |
|
140 |
# Get the font file name
|
141 |
font_file = font_files.get(font_name, "NanumGothic-Regular.ttf")
|
142 |
+
print(f"Trying to load font: {font_name} -> {font_file}") # Debug info
|
143 |
|
144 |
+
# List of paths to try
|
145 |
+
paths_to_try = [
|
146 |
+
font_file, # Current directory
|
147 |
+
f"./{font_file}", # Explicit current directory
|
148 |
+
os.path.join(os.getcwd(), font_file), # Full path from current working directory
|
149 |
+
os.path.join(os.path.dirname(os.path.abspath(__file__)), font_file), # Same directory as script
|
150 |
+
]
|
151 |
+
|
152 |
+
# Try each path
|
153 |
+
for path in paths_to_try:
|
154 |
+
try:
|
155 |
+
font = ImageFont.truetype(path, font_size)
|
156 |
+
print(f"Successfully loaded font from: {path}") # Debug info
|
157 |
+
return font
|
158 |
+
except Exception as e:
|
159 |
+
print(f"Failed to load font from {path}: {e}") # Debug info
|
160 |
+
continue
|
161 |
+
|
162 |
+
# If specific font not found, try to load NanumGothic as fallback
|
163 |
+
print(f"Warning: {font_file} not found. Trying NanumGothic fallback...") # Debug info
|
164 |
+
for path in ["NanumGothic-Regular.ttf", "./NanumGothic-Regular.ttf"]:
|
165 |
try:
|
166 |
+
font = ImageFont.truetype(path, font_size)
|
167 |
+
print(f"Loaded fallback font from: {path}") # Debug info
|
168 |
+
return font
|
169 |
except:
|
170 |
+
continue
|
171 |
+
|
172 |
+
# Final fallback to default font
|
173 |
+
print("Warning: No Korean fonts found. Using default font.") # Debug info
|
174 |
+
return ImageFont.load_default()
|
175 |
|
176 |
def add_text_overlay(image, title_ko, author_ko,
|
177 |
title_position, author_position, text_color, font_name,
|
178 |
title_size, author_size):
|
179 |
"""Add Korean text overlay to the generated image"""
|
180 |
+
print(f"add_text_overlay called with font_name: {font_name}") # Debug info
|
181 |
+
|
182 |
# Create a copy of the image to work with
|
183 |
img_with_text = image.copy()
|
184 |
draw = ImageDraw.Draw(img_with_text)
|
|
|
202 |
title_x, title_y = position_coords[title_position]
|
203 |
|
204 |
# Get text bbox for centering
|
205 |
+
try:
|
206 |
+
bbox = draw.textbbox((0, 0), title_ko, font=title_font)
|
207 |
+
text_width = bbox[2] - bbox[0]
|
208 |
+
text_height = bbox[3] - bbox[1]
|
209 |
+
except:
|
210 |
+
# Fallback for older PIL versions
|
211 |
+
text_width, text_height = draw.textsize(title_ko, font=title_font)
|
212 |
|
213 |
# Draw text with shadow for better visibility
|
214 |
shadow_offset = 2
|
|
|
228 |
author_x, author_y = position_coords[author_position]
|
229 |
|
230 |
# Get text bbox for centering
|
231 |
+
try:
|
232 |
+
bbox = draw.textbbox((0, 0), author_text, font=author_font)
|
233 |
+
text_width = bbox[2] - bbox[0]
|
234 |
+
text_height = bbox[3] - bbox[1]
|
235 |
+
except:
|
236 |
+
# Fallback for older PIL versions
|
237 |
+
text_width, text_height = draw.textsize(author_text, font=author_font)
|
238 |
|
239 |
# Draw text with shadow
|
240 |
draw.text((author_x - text_width // 2 + shadow_offset, author_y - text_height // 2 + shadow_offset),
|
|
|
296 |
author_size: int,
|
297 |
progress: gr.Progress = gr.Progress(track_tqdm=True),
|
298 |
):
|
299 |
+
print(f"inference called with font_name: {font_name}") # Debug info
|
300 |
+
|
301 |
if randomize_seed:
|
302 |
seed = random.randint(0, MAX_SEED)
|
303 |
generator = torch.Generator(device=device).manual_seed(seed)
|
|
|
314 |
|
315 |
# Add text overlay if any Korean text is provided
|
316 |
if title_ko or author_ko:
|
317 |
+
print(f"Adding text overlay with font: {font_name}") # Debug info
|
318 |
image = add_text_overlay(image, title_ko, author_ko,
|
319 |
title_position, author_position, text_color, font_name,
|
320 |
title_size, author_size)
|
|
|
365 |
max_lines=1,
|
366 |
placeholder="Enter your prompt",
|
367 |
container=False,
|
368 |
+
# Font change event handler for debugging
|
369 |
+
def on_font_change(font_name):
|
370 |
+
print(f"Font changed to: {font_name}")
|
371 |
+
return font_name
|
372 |
+
|
373 |
+
font_name.change(
|
374 |
+
fn=on_font_change,
|
375 |
+
inputs=[font_name],
|
376 |
+
outputs=[font_name]
|
377 |
)
|
378 |
augment_button = gr.Button("μ¦κ°", scale=0)
|
379 |
run_button = gr.Button("Run", scale=0)
|