Spaces:
Sleeping
Sleeping
File size: 17,639 Bytes
a7b9475 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
"""
Template management for different infographic styles
"""
from typing import Dict, List, Tuple, Optional
from dataclasses import dataclass
from config import Config, TEMPLATE_COLORS
import logging
logger = logging.getLogger(__name__)
@dataclass
class TemplateStyle:
"""Template style configuration"""
name: str
colors: Dict[str, str]
fonts: Dict[str, Tuple[str, int, str]]
spacing: Dict[str, int]
layout_rules: Dict[str, any]
visual_style: Dict[str, any]
class TemplateManager:
"""Manage and apply infographic templates"""
def __init__(self):
"""Initialize template manager with predefined styles"""
self.templates = self._initialize_templates()
logger.info(f"Template manager initialized with {len(self.templates)} templates")
def get_template(self, template_name: str, custom_color: Optional[str] = None) -> TemplateStyle:
"""
Get template configuration
Args:
template_name: Name of the template
custom_color: Optional custom primary color
Returns:
TemplateStyle configuration
"""
template = self.templates.get(template_name, self.templates['Modern'])
# Apply custom color if provided
if custom_color:
template = self._apply_custom_color(template, custom_color)
return template
def apply_template_to_content(self, content: Dict, template_name: str,
custom_color: Optional[str] = None,
layout_type: str = "Vertical") -> Dict:
"""
Apply template styling to content
Args:
content: Analyzed content from TextProcessor and GeminiClient
template_name: Selected template name
custom_color: Optional custom color
layout_type: Layout arrangement
Returns:
Styled content ready for image generation
"""
template = self.get_template(template_name, custom_color)
styled_content = {
'template': template,
'layout_type': layout_type,
'title': self._style_title(content, template),
'sections': self._style_sections(content, template, layout_type),
'visual_elements': self._style_visual_elements(content, template),
'design_specs': self._create_design_specs(template, layout_type),
'color_scheme': template.colors,
'typography': template.fonts
}
return styled_content
def _initialize_templates(self) -> Dict[str, TemplateStyle]:
"""Initialize predefined templates"""
templates = {}
# Modern Template
templates['Modern'] = TemplateStyle(
name='Modern',
colors=TEMPLATE_COLORS['Modern'],
fonts={
'title': ('Arial', 32, 'bold'),
'heading': ('Arial', 24, 'bold'),
'body': ('Arial', 16, 'normal'),
'caption': ('Arial', 14, 'normal')
},
spacing={
'title_margin': 40,
'section_margin': 30,
'line_spacing': 8,
'padding': 60
},
layout_rules={
'max_sections': 6,
'title_position': 'top',
'alignment': 'left',
'use_icons': True,
'use_dividers': True
},
visual_style={
'border_radius': 12,
'shadow': True,
'gradient': False,
'icon_style': 'filled',
'emphasis': 'color'
}
)
# Corporate Template
templates['Corporate'] = TemplateStyle(
name='Corporate',
colors=TEMPLATE_COLORS['Corporate'],
fonts={
'title': ('Arial', 30, 'bold'),
'heading': ('Arial', 22, 'bold'),
'body': ('Arial', 15, 'normal'),
'caption': ('Arial', 13, 'normal')
},
spacing={
'title_margin': 35,
'section_margin': 25,
'line_spacing': 6,
'padding': 50
},
layout_rules={
'max_sections': 8,
'title_position': 'top',
'alignment': 'left',
'use_icons': False,
'use_dividers': True
},
visual_style={
'border_radius': 4,
'shadow': False,
'gradient': False,
'icon_style': 'outline',
'emphasis': 'typography'
}
)
# Creative Template (continued)
templates['Creative'] = TemplateStyle(
name='Creative',
colors=TEMPLATE_COLORS['Creative'],
fonts={
'title': ('Arial', 36, 'bold'),
'heading': ('Arial', 26, 'bold'),
'body': ('Arial', 17, 'normal'),
'caption': ('Arial', 15, 'italic')
},
spacing={
'title_margin': 50,
'section_margin': 35,
'line_spacing': 10,
'padding': 70
},
layout_rules={
'max_sections': 5,
'title_position': 'center',
'alignment': 'center',
'use_icons': True,
'use_dividers': False
},
visual_style={
'border_radius': 20,
'shadow': True,
'gradient': True,
'icon_style': 'colorful',
'emphasis': 'visual'
}
)
# Minimalist Template
templates['Minimalist'] = TemplateStyle(
name='Minimalist',
colors=TEMPLATE_COLORS['Minimalist'],
fonts={
'title': ('Arial', 28, 'normal'),
'heading': ('Arial', 20, 'bold'),
'body': ('Arial', 14, 'normal'),
'caption': ('Arial', 12, 'normal')
},
spacing={
'title_margin': 60,
'section_margin': 40,
'line_spacing': 12,
'padding': 80
},
layout_rules={
'max_sections': 4,
'title_position': 'top',
'alignment': 'left',
'use_icons': False,
'use_dividers': True
},
visual_style={
'border_radius': 0,
'shadow': False,
'gradient': False,
'icon_style': 'minimal',
'emphasis': 'whitespace'
}
)
# Academic Template
templates['Academic'] = TemplateStyle(
name='Academic',
colors=TEMPLATE_COLORS['Academic'],
fonts={
'title': ('Arial', 28, 'bold'),
'heading': ('Arial', 20, 'bold'),
'body': ('Arial', 14, 'normal'),
'caption': ('Arial', 12, 'italic')
},
spacing={
'title_margin': 30,
'section_margin': 20,
'line_spacing': 6,
'padding': 40
},
layout_rules={
'max_sections': 10,
'title_position': 'top',
'alignment': 'justify',
'use_icons': False,
'use_dividers': True
},
visual_style={
'border_radius': 0,
'shadow': False,
'gradient': False,
'icon_style': 'academic',
'emphasis': 'content'
}
)
return templates
def _apply_custom_color(self, template: TemplateStyle, custom_color: str) -> TemplateStyle:
"""Apply custom primary color to template"""
new_colors = template.colors.copy()
new_colors['primary'] = custom_color
# Create a new template with custom color
return TemplateStyle(
name=template.name,
colors=new_colors,
fonts=template.fonts,
spacing=template.spacing,
layout_rules=template.layout_rules,
visual_style=template.visual_style
)
def _style_title(self, content: Dict, template: TemplateStyle) -> Dict:
"""Style the title section"""
# Try to get AI-generated title first
title_text = ""
if 'ai_insights' in content and hasattr(content['ai_insights'], 'get'):
title_text = content['ai_insights'].get('title', '')
# Fallback to first line or generated title
if not title_text:
first_line = content.get('original_text', '').split('\n')[0].strip()
if len(first_line.split()) <= 10:
title_text = first_line
else:
title_text = ' '.join(content.get('original_text', '').split()[:8])
return {
'text': title_text,
'font': template.fonts['title'],
'color': template.colors['primary'],
'alignment': template.layout_rules['title_position'],
'margin': template.spacing['title_margin']
}
def _style_sections(self, content: Dict, template: TemplateStyle, layout_type: str) -> List[Dict]:
"""Style content sections"""
sections = content.get('sections', [])
max_sections = template.layout_rules['max_sections']
# Limit sections based on template
sections = sections[:max_sections]
styled_sections = []
for i, section in enumerate(sections):
styled_section = {
'id': section.get('id', i + 1),
'content': section.get('content', ''),
'condensed_text': section.get('condensed_text', section.get('content', '')[:100]),
'type': section.get('type', 'body'),
'priority': section.get('priority', 5),
'visual_treatment': section.get('visual_treatment', 'bullet'),
'color_suggestion': section.get('color_suggestion', 'primary'),
'styling': self._get_section_styling(section, template, i)
}
styled_sections.append(styled_section)
return styled_sections
def _get_section_styling(self, section: Dict, template: TemplateStyle, index: int) -> Dict:
"""Get styling for individual section"""
section_type = section.get('type', 'body')
if section_type == 'header':
font = template.fonts['heading']
color = template.colors['primary']
elif section_type == 'key_point':
font = template.fonts['body']
color = template.colors['accent']
elif section_type == 'data':
font = template.fonts['heading']
color = template.colors['secondary']
else:
font = template.fonts['body']
color = template.colors['text']
return {
'font': font,
'color': color,
'background_color': self._get_alternating_background(index, template),
'margin': template.spacing['section_margin'],
'padding': 20,
'border_radius': template.visual_style['border_radius'],
'alignment': template.layout_rules['alignment']
}
def _get_alternating_background(self, index: int, template: TemplateStyle) -> str:
"""Get alternating background colors"""
if template.name == 'Minimalist':
return 'transparent'
elif index % 2 == 0:
return template.colors['background']
else:
return self._lighten_color(template.colors['background'], 0.05)
def _lighten_color(self, hex_color: str, factor: float) -> str:
"""Lighten a hex color"""
# Simple color lightening (fallback)
return hex_color # For now, return original color
def _style_visual_elements(self, content: Dict, template: TemplateStyle) -> List[Dict]:
"""Style visual elements like icons and charts"""
visual_elements = content.get('visual_elements', [])
styled_elements = []
for element in visual_elements:
styled_element = {
'type': element.get('type', 'icon'),
'description': element.get('description', ''),
'placement': element.get('placement', 'body'),
'importance': element.get('importance', 5),
'styling': {
'color': template.colors['accent'],
'size': self._get_element_size(element.get('importance', 5)),
'style': template.visual_style.get('icon_style', 'filled')
}
}
styled_elements.append(styled_element)
return styled_elements
def _get_element_size(self, importance: int) -> int:
"""Get element size based on importance"""
size_map = {
1: 16, 2: 20, 3: 24, 4: 28, 5: 32,
6: 36, 7: 40, 8: 44, 9: 48, 10: 52
}
return size_map.get(importance, 32)
def _create_design_specs(self, template: TemplateStyle, layout_type: str) -> Dict:
"""Create comprehensive design specifications"""
return {
'canvas_size': self._get_canvas_size(layout_type),
'margins': {
'top': template.spacing['padding'],
'bottom': template.spacing['padding'],
'left': template.spacing['padding'],
'right': template.spacing['padding']
},
'grid': self._get_grid_specs(layout_type),
'typography': {
'line_height': template.spacing['line_spacing'],
'paragraph_spacing': template.spacing['section_margin']
},
'colors': template.colors,
'visual_effects': {
'shadows': template.visual_style['shadow'],
'gradients': template.visual_style['gradient'],
'border_radius': template.visual_style['border_radius']
},
'layout_rules': template.layout_rules
}
def _get_canvas_size(self, layout_type: str) -> Tuple[int, int]:
"""Get canvas size based on layout"""
sizes = {
'Vertical': (Config.DEFAULT_WIDTH, Config.DEFAULT_HEIGHT),
'Horizontal': (Config.DEFAULT_HEIGHT, Config.DEFAULT_WIDTH),
'Grid': (Config.DEFAULT_WIDTH, Config.DEFAULT_WIDTH),
'Flow': (Config.DEFAULT_WIDTH, int(Config.DEFAULT_HEIGHT * 0.75))
}
return sizes.get(layout_type, (Config.DEFAULT_WIDTH, Config.DEFAULT_HEIGHT))
def _get_grid_specs(self, layout_type: str) -> Dict:
"""Get grid specifications for layout"""
grid_specs = {
'Vertical': {'columns': 1, 'rows': 'auto', 'gap': 30},
'Horizontal': {'columns': 2, 'rows': 'auto', 'gap': 40},
'Grid': {'columns': 2, 'rows': 3, 'gap': 25},
'Flow': {'columns': 'auto', 'rows': 'auto', 'gap': 20}
}
return grid_specs.get(layout_type, grid_specs['Vertical'])
def get_available_templates(self) -> List[str]:
"""Get list of available template names"""
return list(self.templates.keys())
def get_template_preview_data(self, template_name: str) -> Dict:
"""Get template preview information"""
template = self.templates.get(template_name)
if not template:
return {}
return {
'name': template.name,
'colors': template.colors,
'description': self._get_template_description(template.name),
'best_for': self._get_template_use_cases(template.name)
}
def _get_template_description(self, template_name: str) -> str:
"""Get template description"""
descriptions = {
'Modern': 'Clean, contemporary design with vibrant colors and modern typography',
'Corporate': 'Professional business style with conservative colors and clean lines',
'Creative': 'Artistic and expressive with bold colors and creative elements',
'Minimalist': 'Simple, elegant design focusing on whitespace and essential elements',
'Academic': 'Scholarly presentation style optimized for educational content'
}
return descriptions.get(template_name, 'Professional infographic template')
def _get_template_use_cases(self, template_name: str) -> List[str]:
"""Get template use cases"""
use_cases = {
'Modern': ['Social Media', 'Marketing', 'Tech Content', 'Startups'],
'Corporate': ['Business Reports', 'Annual Reports', 'Corporate Communications', 'Finance'],
'Creative': ['Creative Industries', 'Art Projects', 'Entertainment', 'Personal Branding'],
'Minimalist': ['Luxury Brands', 'Architecture', 'Design Portfolios', 'Premium Products'],
'Academic': ['Research Papers', 'Educational Content', 'Scientific Reports', 'Publications']
}
return use_cases.get(template_name, ['General Purpose']) |