ysharma HF Staff commited on
Commit
f629164
Β·
verified Β·
1 Parent(s): b6b5355

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -95
app.py CHANGED
@@ -182,68 +182,54 @@ class MoodTheme(Base):
182
  self._apply_mood_styling()
183
 
184
  def _map_hex_to_gradio_color(self, hex_color: str):
185
- """Map hex colors to the closest available Gradio color."""
186
- # Convert hex to RGB for better color matching
187
- hex_color = hex_color.lstrip('#')
188
- try:
189
- r, g, b = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
190
-
191
- # Enhanced color mapping based on RGB values
192
- if r > 200 and g < 100 and b < 100: # Red family
193
- if r > 220 and g < 50:
194
- return colors.red
195
- else:
196
- return colors.rose
197
- elif r > 150 and g > 100 and b < 100: # Orange family
198
- return colors.orange
199
- elif r > 200 and g > 150 and b < 100: # Yellow family
200
- if g > 200:
201
- return colors.yellow
202
- else:
203
- return colors.amber
204
- elif r < 150 and g > 150 and b < 150: # Green family
205
- if g > 200:
206
- return colors.lime
207
- elif r < 100:
208
- return colors.green
209
- else:
210
- return colors.emerald
211
- elif r < 150 and g > 100 and b > 150: # Blue-green family
212
- if g > b:
213
- return colors.teal
214
- else:
215
- return colors.cyan
216
- elif r < 150 and g < 150 and b > 150: # Blue family
217
- if b > 200:
218
- return colors.blue
219
- elif r < 100 and g < 100:
220
- return colors.indigo
221
- else:
222
- return colors.sky
223
- elif r > 100 and g < 150 and b > 150: # Purple family
224
- if r > b:
225
- return colors.pink
226
- elif b > 150:
227
- return colors.violet
228
- else:
229
- return colors.purple
230
- elif r > 150 and g < 150 and b > 100: # Pink/Magenta family
231
- return colors.fuchsia
232
- elif r < 100 and g < 100 and b < 100: # Dark colors
233
- return colors.gray
234
- elif r > 200 and g > 200 and b > 200: # Light colors
235
- return colors.neutral
236
- else:
237
- # Default fallback based on dominant color
238
- if r >= g and r >= b:
239
- return colors.red
240
- elif g >= r and g >= b:
241
- return colors.green
242
- else:
243
- return colors.blue
244
-
245
- except (ValueError, IndexError):
246
- return colors.blue
247
 
248
  def _apply_mood_styling(self):
249
  """Apply additional styling based on mood colors and fonts."""
@@ -537,56 +523,50 @@ class MoodThemeGenerator:
537
  def publish_theme(self, theme_name: str, version: str = "1.0.0") -> str:
538
  """Publish the current theme to HuggingFace Hub."""
539
  if not self.current_theme_data:
540
- return "❌ No theme data available. Please generate a theme first."
541
 
542
  if not self.config.HF_TOKEN:
543
- return "❌ HuggingFace token not found. Please set HF_TOKEN environment variable."
 
 
 
 
 
544
 
545
  try:
546
- # Debug: Print current theme data
547
- colors_info = [f"{c['name']} ({c['hex']})" for c in self.current_theme_data['colors']]
548
- fonts_info = [f['name'] for f in self.current_theme_data['fonts']]
549
-
550
- debug_info = f"Creating theme with:\n"
551
- debug_info += f"Colors: {', '.join(colors_info[:3])}\n"
552
- debug_info += f"Fonts: {', '.join(fonts_info[:2])}\n"
553
-
554
- # Create a proper theme instance with debug info
555
  mood_theme = MoodTheme(
556
- mood_colors=self.current_theme_data['colors'][:3], # Use only first 3 colors
557
- mood_fonts=self.current_theme_data['fonts'][:2], # Use only first 2 fonts
558
- theme_name=theme_name.replace('-', '_').replace(' ', '_') # Clean theme name
559
  )
560
 
561
- # Create a unique repo name with timestamp
 
 
 
 
 
 
 
 
 
 
 
 
562
  clean_name = theme_name.lower().replace(' ', '-').replace('_', '-')
563
  repo_name = f"{clean_name}-{int(time.time())}"
564
 
565
- # Push to hub with error handling
566
- result = mood_theme.push_to_hub(
567
  repo_name=repo_name,
568
  version=version,
569
  hf_token=self.config.HF_TOKEN
570
  )
571
 
572
- success_msg = f"βœ… Theme successfully published!\n\n"
573
- success_msg += debug_info
574
- success_msg += f"\nπŸ“ Repository: {repo_name}\n"
575
- success_msg += f"πŸ”— Use with: gr.Theme.from_hub('{repo_name}')\n"
576
- success_msg += f"🌐 Preview: https://huggingface.co/spaces/{repo_name}"
577
-
578
- return success_msg
579
 
580
  except Exception as e:
581
- error_msg = f"❌ Error publishing theme: {str(e)}\n\n"
582
- if "colors" in str(e).lower():
583
- error_msg += "πŸ’‘ This might be a color formatting issue. "
584
- elif "font" in str(e).lower():
585
- error_msg += "πŸ’‘ This might be a font loading issue. "
586
- else:
587
- error_msg += "πŸ’‘ Check your HuggingFace token and internet connection. "
588
-
589
- return error_msg
590
 
591
  def clear_theme(self) -> tuple[str, str, str]:
592
  """Clear the current theme."""
 
182
  self._apply_mood_styling()
183
 
184
  def _map_hex_to_gradio_color(self, hex_color: str):
185
+ """Map hex colors to deliberately different Gradio colors."""
186
+ # Create a more aggressive mapping to ensure variety
187
+ color_mapping = {
188
+ # Reds - map to different red variants
189
+ "#DC143C": colors.red, # Crimson
190
+ "#FF2400": colors.rose, # Scarlet
191
+ "#8B0000": colors.red, # Dark red
192
+
193
+ # Oranges
194
+ "#FF7F50": colors.orange, # Coral
195
+ "#F28500": colors.amber, # Tangerine
196
+ "#FF7518": colors.orange, # Pumpkin
197
+
198
+ # Yellows/Golds
199
+ "#FFD700": colors.yellow, # Gold
200
+ "#F7E7CE": colors.amber, # Champagne
201
+ "#FFFACD": colors.yellow, # Lemon Chiffon
202
+
203
+ # Greens
204
+ "#32CD32": colors.lime, # Lime Green
205
+ "#228B22": colors.green, # Forest Green
206
+ "#50C878": colors.emerald, # Emerald
207
+
208
+ # Blues
209
+ "#4169E1": colors.blue, # Royal Blue
210
+ "#87CEEB": colors.sky, # Sky Blue
211
+ "#0F52BA": colors.indigo, # Sapphire
212
+
213
+ # Purples
214
+ "#4B0082": colors.indigo, # Indigo
215
+ "#800080": colors.purple, # Purple
216
+ "#DDA0DD": colors.violet, # Plum
217
+
218
+ # Pinks
219
+ "#FF69B4": colors.pink, # Hot Pink
220
+ "#FF00FF": colors.fuchsia, # Magenta
221
+
222
+ # Teals/Cyans
223
+ "#008080": colors.teal, # Teal
224
+ "#40E0D0": colors.cyan, # Turquoise
225
+
226
+ # Grays/Browns
227
+ "#708090": colors.slate, # Slate Gray
228
+ "#36454F": colors.gray, # Charcoal
229
+ "#6F4E37": colors.stone, # Coffee
230
+ }
231
+
232
+ return color_mapping.get(hex_color, colors.blue)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
 
234
  def _apply_mood_styling(self):
235
  """Apply additional styling based on mood colors and fonts."""
 
523
  def publish_theme(self, theme_name: str, version: str = "1.0.0") -> str:
524
  """Publish the current theme to HuggingFace Hub."""
525
  if not self.current_theme_data:
526
+ return "No theme data available. Please generate a theme first."
527
 
528
  if not self.config.HF_TOKEN:
529
+ return "HuggingFace token not found. Please set HF_TOKEN environment variable."
530
+
531
+ # DEBUG: Print what we're actually working with
532
+ print("=== DEBUG INFO ===")
533
+ print(f"Colors: {[(c['name'], c['hex']) for c in self.current_theme_data['colors'][:3]]}")
534
+ print(f"Fonts: {[f['name'] for f in self.current_theme_data['fonts'][:2]]}")
535
 
536
  try:
537
+ # Create theme with more distinct differentiation
 
 
 
 
 
 
 
 
538
  mood_theme = MoodTheme(
539
+ mood_colors=self.current_theme_data['colors'][:3],
540
+ mood_fonts=self.current_theme_data['fonts'][:2],
541
+ theme_name=theme_name.replace('-', '_').replace(' ', '_')
542
  )
543
 
544
+ # Additional forced differentiation based on first color
545
+ first_color = self.current_theme_data['colors'][0]['hex']
546
+ if first_color in ['#DC143C', '#FF2400', '#8B0000']: # Red family
547
+ mood_theme.set(button_primary_background_fill="#8B0000")
548
+ elif first_color in ['#FFD700', '#F7E7CE', '#FFFACD']: # Yellow/Gold family
549
+ mood_theme.set(button_primary_background_fill="#DAA520")
550
+ elif first_color in ['#228B22', '#32CD32', '#50C878']: # Green family
551
+ mood_theme.set(button_primary_background_fill="#006400")
552
+ elif first_color in ['#4169E1', '#87CEEB', '#0F52BA']: # Blue family
553
+ mood_theme.set(button_primary_background_fill="#000080")
554
+ else:
555
+ mood_theme.set(button_primary_background_fill=first_color)
556
+
557
  clean_name = theme_name.lower().replace(' ', '-').replace('_', '-')
558
  repo_name = f"{clean_name}-{int(time.time())}"
559
 
560
+ mood_theme.push_to_hub(
 
561
  repo_name=repo_name,
562
  version=version,
563
  hf_token=self.config.HF_TOKEN
564
  )
565
 
566
+ return f"Theme published: {repo_name}\nFirst color: {first_color}\nFirst font: {self.current_theme_data['fonts'][0]['name']}"
 
 
 
 
 
 
567
 
568
  except Exception as e:
569
+ return f"Error publishing theme: {str(e)}"
 
 
 
 
 
 
 
 
570
 
571
  def clear_theme(self) -> tuple[str, str, str]:
572
  """Clear the current theme."""