Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 186 |
-
#
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
#
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 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 "
|
| 541 |
|
| 542 |
if not self.config.HF_TOKEN:
|
| 543 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 544 |
|
| 545 |
try:
|
| 546 |
-
#
|
| 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],
|
| 557 |
-
mood_fonts=self.current_theme_data['fonts'][:2],
|
| 558 |
-
theme_name=theme_name.replace('-', '_').replace(' ', '_')
|
| 559 |
)
|
| 560 |
|
| 561 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 562 |
clean_name = theme_name.lower().replace(' ', '-').replace('_', '-')
|
| 563 |
repo_name = f"{clean_name}-{int(time.time())}"
|
| 564 |
|
| 565 |
-
|
| 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 |
-
|
| 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 |
-
|
| 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."""
|