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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import json
|
| 4 |
+
import base64
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
+
import os
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 9 |
+
import tempfile
|
| 10 |
+
from fpdf import FPDF
|
| 11 |
+
import random
|
| 12 |
+
|
| 13 |
+
# Initialize AI model
|
| 14 |
+
try:
|
| 15 |
+
text_generator = pipeline("text-generation", model="microsoft/DialoGPT-medium", max_length=512)
|
| 16 |
+
except:
|
| 17 |
+
print("Using mock AI for development")
|
| 18 |
+
text_generator = None
|
| 19 |
+
|
| 20 |
+
# Template presets
|
| 21 |
+
TEMPLATE_PRESETS = {
|
| 22 |
+
"Weekly Planner": "A4 weekly planner, portrait orientation, Monday start, 7 columns with time slots from 6 AM to 10 PM, notes section on right side, minimalist design",
|
| 23 |
+
"Monthly Calendar": "A4 monthly calendar, landscape orientation, Sunday start, large date boxes, space for notes, clean modern style",
|
| 24 |
+
"Daily Schedule": "A4 daily time-block planner, portrait, hourly slots from 5 AM to 11 PM, priority tasks section, habit tracker, productivity focused",
|
| 25 |
+
"Habit Tracker": "A4 habit tracker, 30-day grid, colorful design, progress bars, motivational quotes, wellness focused",
|
| 26 |
+
"Meal Planner": "A4 weekly meal planner, 7 days layout, breakfast/lunch/dinner/snacks, shopping list section, healthy lifestyle theme",
|
| 27 |
+
"Study Planner": "A4 study schedule, subject blocks, assignment tracker, exam dates, academic calendar, student-friendly design",
|
| 28 |
+
"Workout Planner": "A4 fitness tracker, weekly workout schedule, exercise log, progress tracking, motivational fitness theme",
|
| 29 |
+
"Budget Planner": "A4 monthly budget tracker, income/expense categories, savings goals, bill reminders, financial planning layout"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
# Font options
|
| 33 |
+
FONT_STYLES = {
|
| 34 |
+
"Modern": "Arial, sans-serif",
|
| 35 |
+
"Classic": "Times New Roman, serif",
|
| 36 |
+
"Handwritten": "Comic Sans MS, cursive",
|
| 37 |
+
"Professional": "Helvetica, sans-serif",
|
| 38 |
+
"Creative": "Impact, sans-serif"
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
# Color schemes
|
| 42 |
+
COLOR_SCHEMES = {
|
| 43 |
+
"Professional Blue": {"primary": "#2563EB", "secondary": "#1E40AF", "accent": "#3B82F6"},
|
| 44 |
+
"Forest Green": {"primary": "#059669", "secondary": "#047857", "accent": "#10B981"},
|
| 45 |
+
"Sunset Orange": {"primary": "#EA580C", "secondary": "#C2410C", "accent": "#FB923C"},
|
| 46 |
+
"Royal Purple": {"primary": "#7C3AED", "secondary": "#5B21B6", "accent": "#8B5CF6"},
|
| 47 |
+
"Rose Pink": {"primary": "#E11D48", "secondary": "#BE185D", "accent": "#F43F5E"},
|
| 48 |
+
"Charcoal Black": {"primary": "#374151", "secondary": "#1F2937", "accent": "#6B7280"}
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
class EnhancedTemplatePDF(FPDF):
|
| 52 |
+
def __init__(self, title="", logo_path=None, colors=None, font_style="Modern"):
|
| 53 |
+
super().__init__()
|
| 54 |
+
self.title_text = title
|
| 55 |
+
self.logo_path = logo_path
|
| 56 |
+
self.colors = colors or COLOR_SCHEMES["Professional Blue"]
|
| 57 |
+
self.font_style = font_style
|
| 58 |
+
|
| 59 |
+
def header(self):
|
| 60 |
+
# Add logo if provided
|
| 61 |
+
if self.logo_path and os.path.exists(self.logo_path):
|
| 62 |
+
try:
|
| 63 |
+
self.image(self.logo_path, 10, 8, 20)
|
| 64 |
+
logo_offset = 35
|
| 65 |
+
except:
|
| 66 |
+
logo_offset = 10
|
| 67 |
+
else:
|
| 68 |
+
logo_offset = 10
|
| 69 |
+
|
| 70 |
+
# Title
|
| 71 |
+
if self.title_text:
|
| 72 |
+
self.set_font('Arial', 'B', 16)
|
| 73 |
+
# Convert hex to RGB
|
| 74 |
+
primary_rgb = tuple(int(self.colors["primary"][1:][i:i+2], 16) for i in (0, 2, 4))
|
| 75 |
+
self.set_text_color(*primary_rgb)
|
| 76 |
+
self.set_x(logo_offset)
|
| 77 |
+
self.cell(0, 10, self.title_text, 0, 1, 'L')
|
| 78 |
+
self.ln(5)
|
| 79 |
+
|
| 80 |
+
def footer(self):
|
| 81 |
+
self.set_y(-15)
|
| 82 |
+
self.set_font('Arial', 'I', 8)
|
| 83 |
+
self.set_text_color(128, 128, 128)
|
| 84 |
+
self.cell(0, 10, f'Generated on {datetime.now().strftime("%Y-%m-%d")} | Page {self.page_no()}', 0, 0, 'C')
|
| 85 |
+
|
| 86 |
+
def process_logo_upload(logo_file):
|
| 87 |
+
"""Process uploaded logo and return path"""
|
| 88 |
+
if logo_file is None:
|
| 89 |
+
return None
|
| 90 |
+
|
| 91 |
+
try:
|
| 92 |
+
# Create temp directory if it doesn't exist
|
| 93 |
+
temp_dir = "temp_uploads"
|
| 94 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 95 |
+
|
| 96 |
+
# Save uploaded file
|
| 97 |
+
logo_path = os.path.join(temp_dir, f"logo_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png")
|
| 98 |
+
|
| 99 |
+
# Handle different file types
|
| 100 |
+
if hasattr(logo_file, 'name'):
|
| 101 |
+
# Gradio file object
|
| 102 |
+
with open(logo_file.name, 'rb') as src:
|
| 103 |
+
image = Image.open(src)
|
| 104 |
+
# Resize logo to reasonable size
|
| 105 |
+
image = image.resize((100, 100), Image.Resampling.LANCZOS)
|
| 106 |
+
image.save(logo_path, 'PNG')
|
| 107 |
+
else:
|
| 108 |
+
# Direct file path
|
| 109 |
+
image = Image.open(logo_file)
|
| 110 |
+
image = image.resize((100, 100), Image.Resampling.LANCZOS)
|
| 111 |
+
image.save(logo_path, 'PNG')
|
| 112 |
+
|
| 113 |
+
return logo_path
|
| 114 |
+
except Exception as e:
|
| 115 |
+
print(f"Logo processing error: {e}")
|
| 116 |
+
return None
|
| 117 |
+
|
| 118 |
+
def generate_ai_structure(prompt, template_type="General"):
|
| 119 |
+
"""Generate template structure using AI"""
|
| 120 |
+
enhanced_prompt = f"""
|
| 121 |
+
Create a detailed structure for a {template_type} template based on this description: {prompt}
|
| 122 |
+
|
| 123 |
+
Return a JSON structure with these elements:
|
| 124 |
+
- layout_type: (weekly/monthly/daily/custom)
|
| 125 |
+
- columns: number of columns
|
| 126 |
+
- rows: number of rows
|
| 127 |
+
- sections: list of content sections
|
| 128 |
+
- time_format: (12h/24h)
|
| 129 |
+
- date_range: start and end dates if applicable
|
| 130 |
+
- special_features: any unique elements
|
| 131 |
+
|
| 132 |
+
Keep the response concise and structured.
|
| 133 |
+
"""
|
| 134 |
+
|
| 135 |
+
try:
|
| 136 |
+
if text_generator:
|
| 137 |
+
response = text_generator(enhanced_prompt, max_length=200, num_return_sequences=1)
|
| 138 |
+
ai_text = response[0]['generated_text']
|
| 139 |
+
else:
|
| 140 |
+
# Mock AI response for development
|
| 141 |
+
ai_text = f"Generated structure for {template_type}: 7-column weekly layout with time slots, notes section, and date headers."
|
| 142 |
+
|
| 143 |
+
# Create structured response
|
| 144 |
+
structure = {
|
| 145 |
+
"layout_type": "weekly" if "week" in prompt.lower() else "monthly" if "month" in prompt.lower() else "daily",
|
| 146 |
+
"columns": 7 if "week" in prompt.lower() else 5 if "daily" in prompt.lower() else 6,
|
| 147 |
+
"rows": 24 if "hour" in prompt.lower() else 6,
|
| 148 |
+
"sections": ["header", "main_content", "notes", "footer"],
|
| 149 |
+
"time_format": "12h",
|
| 150 |
+
"features": ["date_headers", "time_slots", "notes_area"]
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
return structure, ai_text
|
| 154 |
+
|
| 155 |
+
except Exception as e:
|
| 156 |
+
return {"error": f"AI processing failed: {str(e)}"}, "Error generating structure"
|
| 157 |
+
|
| 158 |
+
def create_html_template(structure, customization):
|
| 159 |
+
"""Create HTML template with customization"""
|
| 160 |
+
colors = customization.get('colors', COLOR_SCHEMES["Professional Blue"])
|
| 161 |
+
font_family = FONT_STYLES.get(customization.get('font_style', 'Modern'), 'Arial, sans-serif')
|
| 162 |
+
title = customization.get('title', 'My Planner')
|
| 163 |
+
|
| 164 |
+
html = f"""
|
| 165 |
+
<!DOCTYPE html>
|
| 166 |
+
<html>
|
| 167 |
+
<head>
|
| 168 |
+
<meta charset="UTF-8">
|
| 169 |
+
<title>{title}</title>
|
| 170 |
+
<style>
|
| 171 |
+
@page {{
|
| 172 |
+
size: A4;
|
| 173 |
+
margin: 0.5in;
|
| 174 |
+
}}
|
| 175 |
+
body {{
|
| 176 |
+
font-family: {font_family};
|
| 177 |
+
margin: 0;
|
| 178 |
+
padding: 20px;
|
| 179 |
+
color: #333;
|
| 180 |
+
background: white;
|
| 181 |
+
}}
|
| 182 |
+
.header {{
|
| 183 |
+
display: flex;
|
| 184 |
+
align-items: center;
|
| 185 |
+
margin-bottom: 30px;
|
| 186 |
+
padding-bottom: 15px;
|
| 187 |
+
border-bottom: 3px solid {colors['primary']};
|
| 188 |
+
}}
|
| 189 |
+
.logo {{
|
| 190 |
+
width: 60px;
|
| 191 |
+
height: 60px;
|
| 192 |
+
margin-right: 20px;
|
| 193 |
+
border-radius: 50%;
|
| 194 |
+
}}
|
| 195 |
+
.title {{
|
| 196 |
+
font-size: 28px;
|
| 197 |
+
font-weight: bold;
|
| 198 |
+
color: {colors['primary']};
|
| 199 |
+
margin: 0;
|
| 200 |
+
}}
|
| 201 |
+
.subtitle {{
|
| 202 |
+
font-size: 14px;
|
| 203 |
+
color: {colors['secondary']};
|
| 204 |
+
margin: 5px 0 0 0;
|
| 205 |
+
}}
|
| 206 |
+
.planner-grid {{
|
| 207 |
+
display: grid;
|
| 208 |
+
grid-template-columns: repeat({structure.get('columns', 7)}, 1fr);
|
| 209 |
+
gap: 2px;
|
| 210 |
+
margin: 20px 0;
|
| 211 |
+
border: 2px solid {colors['primary']};
|
| 212 |
+
}}
|
| 213 |
+
.grid-header {{
|
| 214 |
+
background: {colors['primary']};
|
| 215 |
+
color: white;
|
| 216 |
+
padding: 10px;
|
| 217 |
+
text-align: center;
|
| 218 |
+
font-weight: bold;
|
| 219 |
+
font-size: 14px;
|
| 220 |
+
}}
|
| 221 |
+
.grid-cell {{
|
| 222 |
+
border: 1px solid {colors['accent']};
|
| 223 |
+
min-height: 100px;
|
| 224 |
+
padding: 8px;
|
| 225 |
+
background: #fafafa;
|
| 226 |
+
}}
|
| 227 |
+
.date-number {{
|
| 228 |
+
font-weight: bold;
|
| 229 |
+
color: {colors['secondary']};
|
| 230 |
+
font-size: 16px;
|
| 231 |
+
margin-bottom: 5px;
|
| 232 |
+
}}
|
| 233 |
+
.notes-section {{
|
| 234 |
+
margin-top: 30px;
|
| 235 |
+
padding: 20px;
|
| 236 |
+
background: #f8f9fa;
|
| 237 |
+
border-left: 5px solid {colors['accent']};
|
| 238 |
+
}}
|
| 239 |
+
.notes-title {{
|
| 240 |
+
font-weight: bold;
|
| 241 |
+
color: {colors['primary']};
|
| 242 |
+
margin-bottom: 15px;
|
| 243 |
+
font-size: 18px;
|
| 244 |
+
}}
|
| 245 |
+
.notes-lines {{
|
| 246 |
+
line-height: 24px;
|
| 247 |
+
min-height: 120px;
|
| 248 |
+
background-image: repeating-linear-gradient(
|
| 249 |
+
transparent,
|
| 250 |
+
transparent 23px,
|
| 251 |
+
{colors['accent']} 23px,
|
| 252 |
+
{colors['accent']} 24px
|
| 253 |
+
);
|
| 254 |
+
}}
|
| 255 |
+
.footer {{
|
| 256 |
+
margin-top: 30px;
|
| 257 |
+
text-align: center;
|
| 258 |
+
font-size: 12px;
|
| 259 |
+
color: #666;
|
| 260 |
+
border-top: 1px solid #ddd;
|
| 261 |
+
padding-top: 15px;
|
| 262 |
+
}}
|
| 263 |
+
</style>
|
| 264 |
+
</head>
|
| 265 |
+
<body>
|
| 266 |
+
<div class="header">
|
| 267 |
+
<div>
|
| 268 |
+
<h1 class="title">{title}</h1>
|
| 269 |
+
<p class="subtitle">Generated on {datetime.now().strftime('%B %d, %Y')}</p>
|
| 270 |
+
</div>
|
| 271 |
+
</div>
|
| 272 |
+
|
| 273 |
+
<div class="planner-grid">
|
| 274 |
+
"""
|
| 275 |
+
|
| 276 |
+
# Generate grid headers
|
| 277 |
+
if structure.get('layout_type') == 'weekly':
|
| 278 |
+
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
|
| 279 |
+
for day in days:
|
| 280 |
+
html += f'<div class="grid-header">{day}</div>'
|
| 281 |
+
else:
|
| 282 |
+
for i in range(structure.get('columns', 7)):
|
| 283 |
+
html += f'<div class="grid-header">Column {i+1}</div>'
|
| 284 |
+
|
| 285 |
+
# Generate grid cells
|
| 286 |
+
total_cells = structure.get('columns', 7) * structure.get('rows', 6)
|
| 287 |
+
for i in range(total_cells):
|
| 288 |
+
date_num = (i % 31) + 1 if structure.get('layout_type') == 'monthly' else ''
|
| 289 |
+
html += f'''
|
| 290 |
+
<div class="grid-cell">
|
| 291 |
+
<div class="date-number">{date_num}</div>
|
| 292 |
+
<div style="height: 70px;"></div>
|
| 293 |
+
</div>
|
| 294 |
+
'''
|
| 295 |
+
|
| 296 |
+
html += f"""
|
| 297 |
+
</div>
|
| 298 |
+
|
| 299 |
+
<div class="notes-section">
|
| 300 |
+
<div class="notes-title">Notes & Important Reminders</div>
|
| 301 |
+
<div class="notes-lines"></div>
|
| 302 |
+
</div>
|
| 303 |
+
|
| 304 |
+
<div class="footer">
|
| 305 |
+
Created with AI Template Generator | {datetime.now().strftime('%Y')}
|
| 306 |
+
</div>
|
| 307 |
+
</body>
|
| 308 |
+
</html>
|
| 309 |
+
"""
|
| 310 |
+
|
| 311 |
+
return html
|
| 312 |
+
|
| 313 |
+
def generate_pdf_from_html(html_content, customization):
|
| 314 |
+
"""Generate PDF from HTML content"""
|
| 315 |
+
try:
|
| 316 |
+
# Create PDF using FPDF
|
| 317 |
+
colors = customization.get('colors', COLOR_SCHEMES["Professional Blue"])
|
| 318 |
+
title = customization.get('title', 'My Planner')
|
| 319 |
+
logo_path = customization.get('logo_path')
|
| 320 |
+
|
| 321 |
+
pdf = EnhancedTemplatePDF(title=title, logo_path=logo_path, colors=colors)
|
| 322 |
+
pdf.add_page()
|
| 323 |
+
|
| 324 |
+
# Add main content (simplified for demo)
|
| 325 |
+
pdf.set_font('Arial', '', 12)
|
| 326 |
+
pdf.set_text_color(0, 0, 0)
|
| 327 |
+
|
| 328 |
+
# Add planner grid simulation
|
| 329 |
+
y_start = 50
|
| 330 |
+
cell_width = 25
|
| 331 |
+
cell_height = 20
|
| 332 |
+
|
| 333 |
+
# Draw grid
|
| 334 |
+
for row in range(6):
|
| 335 |
+
for col in range(7):
|
| 336 |
+
x = 10 + (col * cell_width)
|
| 337 |
+
y = y_start + (row * cell_height)
|
| 338 |
+
pdf.rect(x, y, cell_width, cell_height)
|
| 339 |
+
|
| 340 |
+
# Add day labels for first row
|
| 341 |
+
if row == 0:
|
| 342 |
+
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
|
| 343 |
+
pdf.set_xy(x + 2, y + 2)
|
| 344 |
+
pdf.cell(cell_width-4, cell_height-4, days[col], 0, 0, 'C')
|
| 345 |
+
|
| 346 |
+
# Add notes section
|
| 347 |
+
pdf.set_xy(10, y_start + 140)
|
| 348 |
+
pdf.set_font('Arial', 'B', 14)
|
| 349 |
+
primary_rgb = tuple(int(colors["primary"][1:][i:i+2], 16) for i in (0, 2, 4))
|
| 350 |
+
pdf.set_text_color(*primary_rgb)
|
| 351 |
+
pdf.cell(0, 10, 'Notes & Reminders', 0, 1, 'L')
|
| 352 |
+
|
| 353 |
+
# Add lines for notes
|
| 354 |
+
pdf.set_text_color(0, 0, 0)
|
| 355 |
+
for i in range(8):
|
| 356 |
+
pdf.ln(6)
|
| 357 |
+
pdf.cell(0, 0, '_' * 80, 0, 1, 'L')
|
| 358 |
+
|
| 359 |
+
# Save to temp file
|
| 360 |
+
temp_dir = "temp_outputs"
|
| 361 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 362 |
+
pdf_path = os.path.join(temp_dir, f"template_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf")
|
| 363 |
+
pdf.output(pdf_path)
|
| 364 |
+
|
| 365 |
+
return pdf_path
|
| 366 |
+
|
| 367 |
+
except Exception as e:
|
| 368 |
+
print(f"PDF generation error: {e}")
|
| 369 |
+
return None
|
| 370 |
+
|
| 371 |
+
def generate_template(prompt, template_preset, title, logo_file, primary_color, secondary_color, accent_color, font_style, color_scheme, include_logo, include_notes, page_size):
|
| 372 |
+
"""Main function to generate customized template"""
|
| 373 |
+
|
| 374 |
+
try:
|
| 375 |
+
# Use preset if selected
|
| 376 |
+
if template_preset and template_preset != "Custom":
|
| 377 |
+
prompt = TEMPLATE_PRESETS[template_preset]
|
| 378 |
+
|
| 379 |
+
# Process customization
|
| 380 |
+
if color_scheme and color_scheme != "Custom":
|
| 381 |
+
colors = COLOR_SCHEMES[color_scheme]
|
| 382 |
+
else:
|
| 383 |
+
colors = {
|
| 384 |
+
"primary": primary_color,
|
| 385 |
+
"secondary": secondary_color,
|
| 386 |
+
"accent": accent_color
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
# Process logo
|
| 390 |
+
logo_path = None
|
| 391 |
+
if include_logo and logo_file:
|
| 392 |
+
logo_path = process_logo_upload(logo_file)
|
| 393 |
+
|
| 394 |
+
customization = {
|
| 395 |
+
'title': title or 'My Custom Planner',
|
| 396 |
+
'colors': colors,
|
| 397 |
+
'font_style': font_style,
|
| 398 |
+
'logo_path': logo_path,
|
| 399 |
+
'include_notes': include_notes,
|
| 400 |
+
'page_size': page_size
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
# Generate AI structure
|
| 404 |
+
template_type = template_preset if template_preset != "Custom" else "General"
|
| 405 |
+
structure, ai_response = generate_ai_structure(prompt, template_type)
|
| 406 |
+
|
| 407 |
+
if "error" in structure:
|
| 408 |
+
return f"<div style='color: red;'>Error: {structure['error']}</div>", None, f"AI Response: {ai_response}"
|
| 409 |
+
|
| 410 |
+
# Create HTML preview
|
| 411 |
+
html_content = create_html_template(structure, customization)
|
| 412 |
+
|
| 413 |
+
# Generate PDF
|
| 414 |
+
pdf_path = generate_pdf_from_html(html_content, customization)
|
| 415 |
+
|
| 416 |
+
# Create success message
|
| 417 |
+
success_msg = f"""
|
| 418 |
+
<div style='padding: 20px; background: #f0f9ff; border-left: 4px solid {colors['primary']};'>
|
| 419 |
+
<h3 style='color: {colors['primary']}; margin-top: 0;'>â
Template Generated Successfully!</h3>
|
| 420 |
+
<p><strong>Title:</strong> {customization['title']}</p>
|
| 421 |
+
<p><strong>Type:</strong> {structure.get('layout_type', 'custom').title()}</p>
|
| 422 |
+
<p><strong>Dimensions:</strong> {structure.get('columns', 'N/A')} columns à {structure.get('rows', 'N/A')} rows</p>
|
| 423 |
+
<p><strong>Features:</strong> {', '.join(structure.get('features', []))}</p>
|
| 424 |
+
<p><strong>Font:</strong> {font_style}</p>
|
| 425 |
+
<p><strong>Colors:</strong> {color_scheme if color_scheme != 'Custom' else 'Custom Colors'}</p>
|
| 426 |
+
</div>
|
| 427 |
+
"""
|
| 428 |
+
|
| 429 |
+
# Return preview, PDF file, and AI response
|
| 430 |
+
return html_content, pdf_path, f"AI Analysis: {ai_response}\n\nStructure: {json.dumps(structure, indent=2)}"
|
| 431 |
+
|
| 432 |
+
except Exception as e:
|
| 433 |
+
error_msg = f"<div style='color: red; padding: 20px;'>â Error generating template: {str(e)}</div>"
|
| 434 |
+
return error_msg, None, f"Error details: {str(e)}"
|
| 435 |
+
|
| 436 |
+
# Create Gradio interface
|
| 437 |
+
with gr.Blocks(title="ð¨ AI Template Generator Pro", theme=gr.themes.Soft()) as demo:
|
| 438 |
+
gr.Markdown("""
|
| 439 |
+
# ð¨ AI Template Generator Pro
|
| 440 |
+
### Create Beautiful, Customizable Planners & Templates with AI
|
| 441 |
+
Simply describe what you need, customize the design, and download your professional template!
|
| 442 |
+
""")
|
| 443 |
+
|
| 444 |
+
with gr.Tab("ð Create Template"):
|
| 445 |
+
with gr.Row():
|
| 446 |
+
with gr.Column(scale=2):
|
| 447 |
+
gr.Markdown("### ð Template Description")
|
| 448 |
+
|
| 449 |
+
template_preset = gr.Dropdown(
|
| 450 |
+
choices=["Custom"] + list(TEMPLATE_PRESETS.keys()),
|
| 451 |
+
value="Weekly Planner",
|
| 452 |
+
label="ð Quick Presets",
|
| 453 |
+
info="Choose a preset or select 'Custom' for your own description"
|
| 454 |
+
)
|
| 455 |
+
|
| 456 |
+
prompt_input = gr.Textbox(
|
| 457 |
+
label="ð Describe Your Template",
|
| 458 |
+
placeholder="Example: A4 weekly planner, portrait, Monday start, colorful design with habit tracker",
|
| 459 |
+
lines=3,
|
| 460 |
+
value=TEMPLATE_PRESETS["Weekly Planner"]
|
| 461 |
+
)
|
| 462 |
+
|
| 463 |
+
def update_prompt(preset):
|
| 464 |
+
if preset and preset != "Custom":
|
| 465 |
+
return TEMPLATE_PRESETS[preset]
|
| 466 |
+
return ""
|
| 467 |
+
|
| 468 |
+
template_preset.change(update_prompt, template_preset, prompt_input)
|
| 469 |
+
|
| 470 |
+
with gr.Column(scale=1):
|
| 471 |
+
gr.Markdown("### ð¨ Customization")
|
| 472 |
+
|
| 473 |
+
title_input = gr.Textbox(
|
| 474 |
+
label="ð Template Title",
|
| 475 |
+
placeholder="My Personal Planner",
|
| 476 |
+
value="My Weekly Planner"
|
| 477 |
+
)
|
| 478 |
+
|
| 479 |
+
include_logo = gr.Checkbox(label="ð¼ï¸ Include Logo", value=False)
|
| 480 |
+
|
| 481 |
+
logo_upload = gr.File(
|
| 482 |
+
label="ð Upload Logo",
|
| 483 |
+
file_types=[".png", ".jpg", ".jpeg"],
|
| 484 |
+
visible=False
|
| 485 |
+
)
|
| 486 |
+
|
| 487 |
+
include_logo.change(lambda x: gr.update(visible=x), include_logo, logo_upload)
|
| 488 |
+
|
| 489 |
+
with gr.Row():
|
| 490 |
+
with gr.Column():
|
| 491 |
+
gr.Markdown("### ð¨ Design Options")
|
| 492 |
+
|
| 493 |
+
color_scheme = gr.Dropdown(
|
| 494 |
+
choices=["Custom"] + list(COLOR_SCHEMES.keys()),
|
| 495 |
+
value="Professional Blue",
|
| 496 |
+
label="ð Color Scheme"
|
| 497 |
+
)
|
| 498 |
+
|
| 499 |
+
with gr.Row():
|
| 500 |
+
primary_color = gr.ColorPicker(label="Primary Color", value="#2563EB", visible=False)
|
| 501 |
+
secondary_color = gr.ColorPicker(label="Secondary Color", value="#1E40AF", visible=False)
|
| 502 |
+
accent_color = gr.ColorPicker(label="Accent Color", value="#3B82F6", visible=False)
|
| 503 |
+
|
| 504 |
+
def toggle_custom_colors(scheme):
|
| 505 |
+
is_custom = scheme == "Custom"
|
| 506 |
+
return [gr.update(visible=is_custom)] * 3
|
| 507 |
+
|
| 508 |
+
color_scheme.change(toggle_custom_colors, color_scheme, [primary_color, secondary_color, accent_color])
|
| 509 |
+
|
| 510 |
+
font_style = gr.Dropdown(
|
| 511 |
+
choices=list(FONT_STYLES.keys()),
|
| 512 |
+
value="Modern",
|
| 513 |
+
label="ð¤ Font Style"
|
| 514 |
+
)
|
| 515 |
+
|
| 516 |
+
with gr.Column():
|
| 517 |
+
gr.Markdown("### âï¸ Additional Options")
|
| 518 |
+
|
| 519 |
+
include_notes = gr.Checkbox(label="ð Include Notes Section", value=True)
|
| 520 |
+
|
| 521 |
+
page_size = gr.Dropdown(
|
| 522 |
+
choices=["A4", "Letter", "A3", "Legal"],
|
| 523 |
+
value="A4",
|
| 524 |
+
label="ð Page Size"
|
| 525 |
+
)
|
| 526 |
+
|
| 527 |
+
generate_btn = gr.Button("ð Generate Template", variant="primary", size="lg")
|
| 528 |
+
|
| 529 |
+
with gr.Tab("ð Preview & Download"):
|
| 530 |
+
with gr.Row():
|
| 531 |
+
with gr.Column(scale=2):
|
| 532 |
+
preview_output = gr.HTML(label="ð Template Preview")
|
| 533 |
+
|
| 534 |
+
with gr.Column(scale=1):
|
| 535 |
+
download_output = gr.File(label="ð¥ Download PDF")
|
| 536 |
+
ai_output = gr.Textbox(label="ð¤ AI Analysis", lines=10)
|
| 537 |
+
|
| 538 |
+
# Connect the generate button
|
| 539 |
+
generate_btn.click(
|
| 540 |
+
generate_template,
|
| 541 |
+
inputs=[
|
| 542 |
+
prompt_input, template_preset, title_input, logo_upload,
|
| 543 |
+
primary_color, secondary_color, accent_color, font_style,
|
| 544 |
+
color_scheme, include_logo, include_notes, page_size
|
| 545 |
+
],
|
| 546 |
+
outputs=[preview_output, download_output, ai_output]
|
| 547 |
+
)
|
| 548 |
+
|
| 549 |
+
gr.Markdown("""
|
| 550 |
+
---
|
| 551 |
+
### ð¡ Tips for Best Results:
|
| 552 |
+
- **Be Specific**: Mention orientation (portrait/landscape), size (A4/Letter), and layout preferences
|
| 553 |
+
- **Include Details**: Specify time ranges, number of columns, special sections needed
|
| 554 |
+
- **Try Presets**: Use quick presets as starting points and modify as needed
|
| 555 |
+
- **Experiment**: Try different color schemes and fonts to match your style
|
| 556 |
+
|
| 557 |
+
### ð¯ Example Descriptions:
|
| 558 |
+
- "A4 monthly calendar, landscape, Sunday start, large date boxes, pastel colors, space for appointments"
|
| 559 |
+
- "Portrait daily planner, hourly time blocks 6 AM to 10 PM, habit tracker, meal planning section"
|
| 560 |
+
- "Weekly meal prep planner, grocery list section, nutritional goals tracker, clean modern design"
|
| 561 |
+
""")
|
| 562 |
+
|
| 563 |
+
if __name__ == "__main__":
|
| 564 |
+
demo.launch(
|
| 565 |
+
server_name="0.0.0.0",
|
| 566 |
+
server_port=7860,
|
| 567 |
+
share=True
|
| 568 |
+
)
|