ysharma HF Staff commited on
Commit
74d4c28
·
verified ·
1 Parent(s): 791655b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -17
app.py CHANGED
@@ -13,7 +13,6 @@ class Config:
13
  HF_TOKEN = os.getenv('HF_TOKEN')
14
 
15
  # --- FONT DATA ---
16
-
17
  FONT_DATA = [
18
  {
19
  "name": "Playfair Display",
@@ -253,10 +252,13 @@ class FontMoodGenerator:
253
  return embeddings
254
 
255
  def _format_palette_as_html(self, top_hits: list[dict[str, any]]) -> str:
256
- """Formats the top font hits into a displayable HTML string."""
257
  if not top_hits:
258
  return "<p>Could not generate a font palette. Please try another mood.</p>"
259
 
 
 
 
260
  sample_texts = [
261
  "The Quick Brown Fox Jumps Over The Lazy Dog",
262
  "Sphinx of black quartz, judge my vow",
@@ -288,7 +290,32 @@ class FontMoodGenerator:
288
  </div>
289
  </div>
290
  """
291
- return f"<div class='font-palette-container'>{cards_html}</div>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
 
293
  def generate_palette(self, mood_text: str) -> tuple[str, list[dict[str, any]]]:
294
  """Generates font palette and returns both HTML and raw data."""
@@ -478,9 +505,6 @@ def create_ui(generator: FontMoodGenerator):
478
  outputs=palette_display
479
  )
480
 
481
- # Hidden CSS output for theming
482
- theme_css_hidden = gr.HTML(visible=False)
483
-
484
  def apply_theme_and_move(font_data_json):
485
  # Convert back to the format expected by apply_theme_css
486
  top_hits = [{"corpus_id": item["corpus_id"], "score": item["score"]} for item in font_data_json]
@@ -490,7 +514,7 @@ def create_ui(generator: FontMoodGenerator):
490
  apply_theme_btn.click(
491
  fn=apply_theme_and_move,
492
  inputs=font_data_hidden,
493
- outputs=[theme_css_hidden, walkthrough]
494
  )
495
 
496
  # STEP 3: Experience the Typography
@@ -500,15 +524,6 @@ def create_ui(generator: FontMoodGenerator):
500
  Notice how the entire interface has transformed to reflect your chosen aesthetic.
501
  """)
502
 
503
- # Apply CSS when entering this step
504
- theme_css_display = gr.HTML()
505
-
506
- theme_css_hidden.change(
507
- fn=lambda css: css,
508
- inputs=theme_css_hidden,
509
- outputs=theme_css_display
510
- )
511
-
512
  gr.Markdown("""
513
  **🎉 Your typography theme is now active!**
514
 
@@ -576,9 +591,12 @@ def create_ui(generator: FontMoodGenerator):
576
 
577
  start_over_btn.click(
578
  fn=restart,
579
- outputs=[palette_html_hidden, font_data_hidden, theme_css_hidden, walkthrough]
580
  )
581
 
 
 
 
582
  # Static CSS for font cards
583
  gr.HTML("""
584
  <style>
 
13
  HF_TOKEN = os.getenv('HF_TOKEN')
14
 
15
  # --- FONT DATA ---
 
16
  FONT_DATA = [
17
  {
18
  "name": "Playfair Display",
 
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._create_font_imports_css(top_hits)
261
+
262
  sample_texts = [
263
  "The Quick Brown Fox Jumps Over The Lazy Dog",
264
  "Sphinx of black quartz, judge my vow",
 
290
  </div>
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."""
 
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
  apply_theme_btn.click(
515
  fn=apply_theme_and_move,
516
  inputs=font_data_hidden,
517
+ outputs=[theme_css_display, walkthrough]
518
  )
519
 
520
  # STEP 3: Experience the Typography
 
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
 
 
591
 
592
  start_over_btn.click(
593
  fn=restart,
594
+ outputs=[palette_html_hidden, font_data_hidden, theme_css_display, walkthrough]
595
  )
596
 
597
+ # Global theme CSS display that persists across all steps
598
+ theme_css_display = gr.HTML()
599
+
600
  # Static CSS for font cards
601
  gr.HTML("""
602
  <style>