Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -251,13 +251,31 @@ class FontMoodGenerator:
|
|
251 |
print("Embeddings computed successfully.")
|
252 |
return embeddings
|
253 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
def _format_palette_as_html(self, top_hits: list[dict[str, any]]) -> str:
|
255 |
"""Formats the top font hits into a displayable HTML string with embedded font imports."""
|
256 |
if not top_hits:
|
257 |
return "<p>Could not generate a font palette. Please try another mood.</p>"
|
258 |
|
259 |
# Get font imports for the selected fonts
|
260 |
-
font_imports = self.
|
261 |
|
262 |
sample_texts = [
|
263 |
"The Quick Brown Fox Jumps Over The Lazy Dog",
|
@@ -291,32 +309,14 @@ class FontMoodGenerator:
|
|
291 |
</div>
|
292 |
"""
|
293 |
|
294 |
-
# Include font imports directly in the HTML
|
295 |
return f"""
|
296 |
-
<style>
|
297 |
{font_imports}
|
298 |
</style>
|
299 |
<div class='font-palette-container'>{cards_html}</div>
|
300 |
"""
|
301 |
|
302 |
-
def _create_font_imports_css(self, top_hits: list[dict[str, any]]) -> str:
|
303 |
-
"""Generates CSS imports for the selected fonts."""
|
304 |
-
if not top_hits:
|
305 |
-
return ""
|
306 |
-
|
307 |
-
imports = []
|
308 |
-
seen_urls = set()
|
309 |
-
|
310 |
-
for hit in top_hits:
|
311 |
-
font_info = self.font_data[hit['corpus_id']]
|
312 |
-
google_fonts_url = font_info['google_fonts_url']
|
313 |
-
|
314 |
-
if google_fonts_url not in seen_urls:
|
315 |
-
imports.append(f"@import url('{google_fonts_url}');")
|
316 |
-
seen_urls.add(google_fonts_url)
|
317 |
-
|
318 |
-
return "\n".join(imports)
|
319 |
-
|
320 |
def generate_palette(self, mood_text: str) -> tuple[str, list[dict[str, any]]]:
|
321 |
"""Generates font palette and returns both HTML and raw data."""
|
322 |
if not mood_text or not mood_text.strip():
|
@@ -338,18 +338,7 @@ class FontMoodGenerator:
|
|
338 |
if not top_hits:
|
339 |
return "/* No fonts generated yet */"
|
340 |
|
341 |
-
|
342 |
-
seen_urls = set()
|
343 |
-
|
344 |
-
for hit in top_hits:
|
345 |
-
font_info = self.font_data[hit['corpus_id']]
|
346 |
-
google_fonts_url = font_info['google_fonts_url']
|
347 |
-
|
348 |
-
if google_fonts_url not in seen_urls:
|
349 |
-
imports.append(f"@import url('{google_fonts_url}');")
|
350 |
-
seen_urls.add(google_fonts_url)
|
351 |
-
|
352 |
-
font_imports = "\n".join(imports)
|
353 |
|
354 |
css_code = f"""/* Generated Font Palette CSS */
|
355 |
{font_imports}
|
@@ -374,28 +363,17 @@ class FontMoodGenerator:
|
|
374 |
return css_code
|
375 |
|
376 |
def apply_theme_css(self, top_hits: list[dict[str, any]]) -> str:
|
377 |
-
"""Generates CSS to apply fonts to the UI."""
|
378 |
if not top_hits:
|
379 |
return ""
|
380 |
|
381 |
-
|
382 |
-
seen_urls = set()
|
383 |
-
|
384 |
-
for hit in top_hits:
|
385 |
-
font_info = self.font_data[hit['corpus_id']]
|
386 |
-
google_fonts_url = font_info['google_fonts_url']
|
387 |
-
|
388 |
-
if google_fonts_url not in seen_urls:
|
389 |
-
imports.append(f"@import url('{google_fonts_url}');")
|
390 |
-
seen_urls.add(google_fonts_url)
|
391 |
-
|
392 |
-
font_imports = "\n".join(imports)
|
393 |
|
394 |
css_rules = []
|
395 |
|
396 |
if len(top_hits) >= 1:
|
397 |
primary_font = self.font_data[top_hits[0]['corpus_id']]['name'].replace("'", "\\'")
|
398 |
-
css_rules.append(f"h1, h2, h3, .gr-button-primary {{ font-family: '{primary_font}', sans-serif !important; }}")
|
399 |
|
400 |
if len(top_hits) >= 2:
|
401 |
secondary_font = self.font_data[top_hits[1]['corpus_id']]['name'].replace("'", "\\'")
|
@@ -407,11 +385,16 @@ class FontMoodGenerator:
|
|
407 |
|
408 |
css_rules_str = "\n ".join(css_rules)
|
409 |
|
410 |
-
css = f"""<style>
|
411 |
{font_imports}
|
412 |
|
413 |
{css_rules_str}
|
414 |
|
|
|
|
|
|
|
|
|
|
|
415 |
* {{
|
416 |
transition: font-family 0.3s ease-in-out;
|
417 |
}}
|
@@ -442,7 +425,7 @@ def create_ui(generator: FontMoodGenerator):
|
|
442 |
""")
|
443 |
|
444 |
mood_input = gr.Textbox(
|
445 |
-
value="
|
446 |
label="Enter Your Mood or Scene",
|
447 |
info="Examples: 'Modern tech startup', 'Playful children's book', 'Gothic horror movie'",
|
448 |
lines=3
|
@@ -505,6 +488,9 @@ def create_ui(generator: FontMoodGenerator):
|
|
505 |
outputs=palette_display
|
506 |
)
|
507 |
|
|
|
|
|
|
|
508 |
def apply_theme_and_move(font_data_json):
|
509 |
# Convert back to the format expected by apply_theme_css
|
510 |
top_hits = [{"corpus_id": item["corpus_id"], "score": item["score"]} for item in font_data_json]
|
@@ -514,7 +500,7 @@ def create_ui(generator: FontMoodGenerator):
|
|
514 |
apply_theme_btn.click(
|
515 |
fn=apply_theme_and_move,
|
516 |
inputs=font_data_hidden,
|
517 |
-
outputs=[
|
518 |
)
|
519 |
|
520 |
# STEP 3: Experience the Typography
|
@@ -524,6 +510,15 @@ def create_ui(generator: FontMoodGenerator):
|
|
524 |
Notice how the entire interface has transformed to reflect your chosen aesthetic.
|
525 |
""")
|
526 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
527 |
gr.Markdown("""
|
528 |
**🎉 Your typography theme is now active!**
|
529 |
|
@@ -587,17 +582,14 @@ def create_ui(generator: FontMoodGenerator):
|
|
587 |
start_over_btn = gr.Button("🔄 Start Over", variant="secondary", size="lg")
|
588 |
|
589 |
def restart():
|
590 |
-
return "", [], "", gr.Walkthrough(selected=0)
|
591 |
|
592 |
start_over_btn.click(
|
593 |
fn=restart,
|
594 |
-
outputs=[palette_html_hidden, font_data_hidden, theme_css_display, walkthrough]
|
595 |
)
|
596 |
|
597 |
-
#
|
598 |
-
theme_css_display = gr.HTML()
|
599 |
-
|
600 |
-
# Static CSS for font cards
|
601 |
gr.HTML("""
|
602 |
<style>
|
603 |
.font-palette-container {
|
|
|
251 |
print("Embeddings computed successfully.")
|
252 |
return embeddings
|
253 |
|
254 |
+
def _create_persistent_font_imports(self, top_hits: list[dict[str, any]]) -> str:
|
255 |
+
"""Creates persistent font imports that work across all steps."""
|
256 |
+
if not top_hits:
|
257 |
+
return ""
|
258 |
+
|
259 |
+
imports = []
|
260 |
+
seen_urls = set()
|
261 |
+
|
262 |
+
for hit in top_hits:
|
263 |
+
font_info = self.font_data[hit['corpus_id']]
|
264 |
+
google_fonts_url = font_info['google_fonts_url']
|
265 |
+
|
266 |
+
if google_fonts_url not in seen_urls:
|
267 |
+
imports.append(f"@import url('{google_fonts_url}');")
|
268 |
+
seen_urls.add(google_fonts_url)
|
269 |
+
|
270 |
+
return "\n".join(imports)
|
271 |
+
|
272 |
def _format_palette_as_html(self, top_hits: list[dict[str, any]]) -> str:
|
273 |
"""Formats the top font hits into a displayable HTML string with embedded font imports."""
|
274 |
if not top_hits:
|
275 |
return "<p>Could not generate a font palette. Please try another mood.</p>"
|
276 |
|
277 |
# Get font imports for the selected fonts
|
278 |
+
font_imports = self._create_persistent_font_imports(top_hits)
|
279 |
|
280 |
sample_texts = [
|
281 |
"The Quick Brown Fox Jumps Over The Lazy Dog",
|
|
|
309 |
</div>
|
310 |
"""
|
311 |
|
312 |
+
# Include font imports directly in the HTML with unique ID to prevent conflicts
|
313 |
return f"""
|
314 |
+
<style id="palette-fonts">
|
315 |
{font_imports}
|
316 |
</style>
|
317 |
<div class='font-palette-container'>{cards_html}</div>
|
318 |
"""
|
319 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
320 |
def generate_palette(self, mood_text: str) -> tuple[str, list[dict[str, any]]]:
|
321 |
"""Generates font palette and returns both HTML and raw data."""
|
322 |
if not mood_text or not mood_text.strip():
|
|
|
338 |
if not top_hits:
|
339 |
return "/* No fonts generated yet */"
|
340 |
|
341 |
+
font_imports = self._create_persistent_font_imports(top_hits)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
342 |
|
343 |
css_code = f"""/* Generated Font Palette CSS */
|
344 |
{font_imports}
|
|
|
363 |
return css_code
|
364 |
|
365 |
def apply_theme_css(self, top_hits: list[dict[str, any]]) -> str:
|
366 |
+
"""Generates CSS to apply fonts to the UI while preserving palette fonts."""
|
367 |
if not top_hits:
|
368 |
return ""
|
369 |
|
370 |
+
font_imports = self._create_persistent_font_imports(top_hits)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
371 |
|
372 |
css_rules = []
|
373 |
|
374 |
if len(top_hits) >= 1:
|
375 |
primary_font = self.font_data[top_hits[0]['corpus_id']]['name'].replace("'", "\\'")
|
376 |
+
css_rules.append(f"h1, h2, h3:not(.font-card h3), .gr-button-primary {{ font-family: '{primary_font}', sans-serif !important; }}")
|
377 |
|
378 |
if len(top_hits) >= 2:
|
379 |
secondary_font = self.font_data[top_hits[1]['corpus_id']]['name'].replace("'", "\\'")
|
|
|
385 |
|
386 |
css_rules_str = "\n ".join(css_rules)
|
387 |
|
388 |
+
css = f"""<style id="theme-fonts">
|
389 |
{font_imports}
|
390 |
|
391 |
{css_rules_str}
|
392 |
|
393 |
+
/* Preserve palette fonts */
|
394 |
+
.font-sample {{
|
395 |
+
font-family: inherit !important;
|
396 |
+
}}
|
397 |
+
|
398 |
* {{
|
399 |
transition: font-family 0.3s ease-in-out;
|
400 |
}}
|
|
|
425 |
""")
|
426 |
|
427 |
mood_input = gr.Textbox(
|
428 |
+
value="Horror movie poster with scary atmosphere",
|
429 |
label="Enter Your Mood or Scene",
|
430 |
info="Examples: 'Modern tech startup', 'Playful children's book', 'Gothic horror movie'",
|
431 |
lines=3
|
|
|
488 |
outputs=palette_display
|
489 |
)
|
490 |
|
491 |
+
# Hidden CSS output for theming
|
492 |
+
theme_css_hidden = gr.HTML(visible=False)
|
493 |
+
|
494 |
def apply_theme_and_move(font_data_json):
|
495 |
# Convert back to the format expected by apply_theme_css
|
496 |
top_hits = [{"corpus_id": item["corpus_id"], "score": item["score"]} for item in font_data_json]
|
|
|
500 |
apply_theme_btn.click(
|
501 |
fn=apply_theme_and_move,
|
502 |
inputs=font_data_hidden,
|
503 |
+
outputs=[theme_css_hidden, walkthrough]
|
504 |
)
|
505 |
|
506 |
# STEP 3: Experience the Typography
|
|
|
510 |
Notice how the entire interface has transformed to reflect your chosen aesthetic.
|
511 |
""")
|
512 |
|
513 |
+
# Apply CSS when entering this step
|
514 |
+
theme_css_display = gr.HTML()
|
515 |
+
|
516 |
+
theme_css_hidden.change(
|
517 |
+
fn=lambda css: css,
|
518 |
+
inputs=theme_css_hidden,
|
519 |
+
outputs=theme_css_display
|
520 |
+
)
|
521 |
+
|
522 |
gr.Markdown("""
|
523 |
**🎉 Your typography theme is now active!**
|
524 |
|
|
|
582 |
start_over_btn = gr.Button("🔄 Start Over", variant="secondary", size="lg")
|
583 |
|
584 |
def restart():
|
585 |
+
return "", [], "", "", gr.Walkthrough(selected=0)
|
586 |
|
587 |
start_over_btn.click(
|
588 |
fn=restart,
|
589 |
+
outputs=[palette_html_hidden, font_data_hidden, theme_css_hidden, theme_css_display, walkthrough]
|
590 |
)
|
591 |
|
592 |
+
# Static CSS for font cards (always loaded)
|
|
|
|
|
|
|
593 |
gr.HTML("""
|
594 |
<style>
|
595 |
.font-palette-container {
|