ysharma HF Staff commited on
Commit
2360382
·
verified ·
1 Parent(s): 115894d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -17
app.py CHANGED
@@ -4,7 +4,7 @@ import os
4
  from huggingface_hub import login
5
  from sentence_transformers import SentenceTransformer, util
6
 
7
- # --- 1. CONFIGURATION ---
8
  # Centralized place for all settings and constants.
9
 
10
  class Config:
@@ -15,7 +15,7 @@ class Config:
15
  HF_TOKEN = os.getenv('HF_TOKEN')
16
 
17
 
18
- # --- 2. FONT DATA ---
19
  # Comprehensive font dataset with descriptions for mood matching.
20
 
21
  FONT_DATA = [
@@ -214,7 +214,7 @@ FONT_DATA = [
214
  ]
215
 
216
 
217
- # --- 3. CORE LOGIC ---
218
  # Encapsulated in a class to manage state (model, embeddings) cleanly.
219
 
220
  class FontMoodGenerator:
@@ -328,21 +328,20 @@ class FontMoodGenerator:
328
 
329
  # Apply different fonts to different elements
330
  if len(top_hits) >= 1:
331
- primary_font = self.font_data[top_hits[0]['corpus_id']]['name']
332
- css_rules.append(f"h1, h2, h3, .gr-button-primary {{ font-family: '{primary_font}' !important; }}")
333
 
334
  if len(top_hits) >= 2:
335
- secondary_font = self.font_data[top_hits[1]['corpus_id']]['name']
336
- css_rules.append(f".gr-textbox input, .gr-textbox textarea {{ font-family: '{secondary_font}' !important; }}")
337
 
338
  if len(top_hits) >= 3:
339
- tertiary_font = self.font_data[top_hits[2]['corpus_id']]['name']
340
- css_rules.append(f".gr-button-secondary {{ font-family: '{tertiary_font}' !important; }}")
341
 
342
- css_rules_str = "\n".join(css_rules)
343
 
344
- css = f"""
345
- <style>
346
  {font_imports}
347
 
348
  {css_rules_str}
@@ -351,8 +350,7 @@ class FontMoodGenerator:
351
  * {{
352
  transition: font-family 0.3s ease-in-out;
353
  }}
354
- </style>
355
- """
356
 
357
  return css
358
 
@@ -380,7 +378,7 @@ class FontMoodGenerator:
380
  return "", ""
381
 
382
 
383
- # --- 4. GRADIO UI ---
384
  # Defines and launches the web interface.
385
 
386
  def create_ui(generator: FontMoodGenerator):
@@ -509,7 +507,6 @@ def create_ui(generator: FontMoodGenerator):
509
  4. **Palette Creation**: The fonts with the highest similarity scores are selected and presented to you as a complete typography palette.
510
 
511
  The Font Mood Generator demonstrates how embeddings can be used for creative applications in typography and design, going beyond simple text search to understand the emotional and aesthetic qualities of typefaces.
512
-
513
  """)
514
 
515
  return demo
@@ -519,4 +516,4 @@ if __name__ == "__main__":
519
  generator = FontMoodGenerator(config=Config(), font_data=FONT_DATA)
520
 
521
  demo = create_ui(generator)
522
- demo.launch(debug=True)
 
4
  from huggingface_hub import login
5
  from sentence_transformers import SentenceTransformer, util
6
 
7
+ # --- CONFIGURATION ---
8
  # Centralized place for all settings and constants.
9
 
10
  class Config:
 
15
  HF_TOKEN = os.getenv('HF_TOKEN')
16
 
17
 
18
+ # --- FONT DATA ---
19
  # Comprehensive font dataset with descriptions for mood matching.
20
 
21
  FONT_DATA = [
 
214
  ]
215
 
216
 
217
+ # --- CORE LOGIC ---
218
  # Encapsulated in a class to manage state (model, embeddings) cleanly.
219
 
220
  class FontMoodGenerator:
 
328
 
329
  # Apply different fonts to different elements
330
  if len(top_hits) >= 1:
331
+ primary_font = self.font_data[top_hits[0]['corpus_id']]['name'].replace("'", "\\'")
332
+ css_rules.append(f"h1, h2, h3, .gr-button-primary {{ font-family: '{primary_font}', sans-serif !important; }}")
333
 
334
  if len(top_hits) >= 2:
335
+ secondary_font = self.font_data[top_hits[1]['corpus_id']]['name'].replace("'", "\\'")
336
+ css_rules.append(f".gr-textbox input, .gr-textbox textarea {{ font-family: '{secondary_font}', sans-serif !important; }}")
337
 
338
  if len(top_hits) >= 3:
339
+ tertiary_font = self.font_data[top_hits[2]['corpus_id']]['name'].replace("'", "\\'")
340
+ css_rules.append(f".gr-button-secondary {{ font-family: '{tertiary_font}', sans-serif !important; }}")
341
 
342
+ css_rules_str = "\n ".join(css_rules)
343
 
344
+ css = f"""<style>
 
345
  {font_imports}
346
 
347
  {css_rules_str}
 
350
  * {{
351
  transition: font-family 0.3s ease-in-out;
352
  }}
353
+ </style>"""
 
354
 
355
  return css
356
 
 
378
  return "", ""
379
 
380
 
381
+ # --- GRADIO UI ---
382
  # Defines and launches the web interface.
383
 
384
  def create_ui(generator: FontMoodGenerator):
 
507
  4. **Palette Creation**: The fonts with the highest similarity scores are selected and presented to you as a complete typography palette.
508
 
509
  The Font Mood Generator demonstrates how embeddings can be used for creative applications in typography and design, going beyond simple text search to understand the emotional and aesthetic qualities of typefaces.
 
510
  """)
511
 
512
  return demo
 
516
  generator = FontMoodGenerator(config=Config(), font_data=FONT_DATA)
517
 
518
  demo = create_ui(generator)
519
+ demo.launch()