Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,19 @@
|
|
| 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
|
| 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 |
-
#
|
| 21 |
TEMPLATE_PRESETS = {
|
| 22 |
-
"Weekly Planner": "A4 weekly planner, portrait orientation, Monday start, 7 columns
|
| 23 |
-
"Monthly Calendar": "A4 monthly calendar, landscape orientation,
|
| 24 |
-
"Daily Schedule": "A4 daily
|
| 25 |
-
"Habit Tracker": "A4 habit tracker, 30-day grid,
|
| 26 |
-
"
|
| 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
|
|
@@ -43,526 +21,247 @@ 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
|
| 52 |
-
def __init__(self, title="",
|
| 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 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 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")}
|
| 85 |
|
| 86 |
-
def
|
| 87 |
-
"""
|
| 88 |
-
if logo_file is None:
|
| 89 |
-
return None
|
| 90 |
-
|
| 91 |
try:
|
| 92 |
-
#
|
| 93 |
-
|
| 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 |
-
|
| 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 |
-
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 |
-
|
|
|
|
|
|
|
| 272 |
|
| 273 |
-
|
| 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 |
-
|
| 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 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
title = customization.get('title', 'My Planner')
|
| 319 |
-
logo_path = customization.get('logo_path')
|
| 320 |
|
| 321 |
-
|
|
|
|
| 322 |
pdf.add_page()
|
| 323 |
|
| 324 |
-
# Add
|
| 325 |
pdf.set_font('Arial', '', 12)
|
| 326 |
pdf.set_text_color(0, 0, 0)
|
| 327 |
|
| 328 |
-
#
|
| 329 |
y_start = 50
|
| 330 |
cell_width = 25
|
| 331 |
cell_height = 20
|
| 332 |
|
| 333 |
-
# Draw grid
|
| 334 |
-
for row in range(
|
| 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 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
pdf.
|
| 344 |
-
pdf.
|
| 345 |
|
| 346 |
# Add notes section
|
| 347 |
-
pdf.set_xy(10, y_start +
|
| 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
|
| 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> {
|
| 421 |
-
<p><strong>
|
| 422 |
-
<p><strong>
|
| 423 |
-
<p><strong>
|
| 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 |
-
|
| 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
|
| 434 |
return error_msg, None, f"Error details: {str(e)}"
|
| 435 |
|
| 436 |
# Create Gradio interface
|
| 437 |
-
with gr.Blocks(title="ð¨ AI Template Generator
|
| 438 |
gr.Markdown("""
|
| 439 |
-
# ð¨ AI Template Generator
|
| 440 |
-
### Create Beautiful
|
| 441 |
-
Simply describe what you need, customize the design, and download your professional template!
|
| 442 |
""")
|
| 443 |
|
| 444 |
-
with gr.
|
| 445 |
-
with gr.
|
| 446 |
-
|
| 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 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 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 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
|
|
|
|
|
|
|
|
|
| 526 |
|
| 527 |
-
|
| 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 |
-
|
| 535 |
-
|
| 536 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 537 |
|
| 538 |
# Connect the generate button
|
| 539 |
generate_btn.click(
|
| 540 |
-
|
| 541 |
-
inputs=[
|
| 542 |
-
|
| 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
|
| 552 |
-
- **Be
|
| 553 |
-
- **Include
|
| 554 |
-
- **Try
|
| 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 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import json
|
| 3 |
+
from datetime import datetime
|
| 4 |
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import tempfile
|
| 6 |
+
import os
|
| 7 |
from fpdf import FPDF
|
| 8 |
+
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Simple template presets
|
| 11 |
TEMPLATE_PRESETS = {
|
| 12 |
+
"Weekly Planner": "A4 weekly planner, portrait orientation, Monday start, 7 columns, notes section",
|
| 13 |
+
"Monthly Calendar": "A4 monthly calendar, landscape orientation, large date boxes, clean design",
|
| 14 |
+
"Daily Schedule": "A4 daily planner, hourly time slots, priority tasks section",
|
| 15 |
+
"Habit Tracker": "A4 habit tracker, 30-day grid, progress tracking",
|
| 16 |
+
"Study Planner": "A4 study schedule, subject blocks, assignment tracker"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
# Color schemes
|
|
|
|
| 21 |
"Professional Blue": {"primary": "#2563EB", "secondary": "#1E40AF", "accent": "#3B82F6"},
|
| 22 |
"Forest Green": {"primary": "#059669", "secondary": "#047857", "accent": "#10B981"},
|
| 23 |
"Sunset Orange": {"primary": "#EA580C", "secondary": "#C2410C", "accent": "#FB923C"},
|
| 24 |
+
"Royal Purple": {"primary": "#7C3AED", "secondary": "#5B21B6", "accent": "#8B5CF6"}
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
+
class SimplePDF(FPDF):
|
| 28 |
+
def __init__(self, title="My Planner", colors=None):
|
| 29 |
super().__init__()
|
| 30 |
self.title_text = title
|
|
|
|
| 31 |
self.colors = colors or COLOR_SCHEMES["Professional Blue"]
|
|
|
|
| 32 |
|
| 33 |
def header(self):
|
| 34 |
+
self.set_font('Arial', 'B', 16)
|
| 35 |
+
# Convert hex to RGB
|
| 36 |
+
primary_rgb = tuple(int(self.colors["primary"][1:][i:i+2], 16) for i in (0, 2, 4))
|
| 37 |
+
self.set_text_color(*primary_rgb)
|
| 38 |
+
self.cell(0, 10, self.title_text, 0, 1, 'C')
|
| 39 |
+
self.ln(10)
|
| 40 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def footer(self):
|
| 42 |
self.set_y(-15)
|
| 43 |
self.set_font('Arial', 'I', 8)
|
| 44 |
self.set_text_color(128, 128, 128)
|
| 45 |
+
self.cell(0, 10, f'Generated on {datetime.now().strftime("%Y-%m-%d")}', 0, 0, 'C')
|
| 46 |
|
| 47 |
+
def create_simple_template(prompt, title, color_scheme):
|
| 48 |
+
"""Create a simple template based on user input"""
|
|
|
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
+
# Get colors
|
| 51 |
+
colors = COLOR_SCHEMES.get(color_scheme, COLOR_SCHEMES["Professional Blue"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# Create HTML preview
|
| 54 |
+
html_content = f"""
|
| 55 |
+
<!DOCTYPE html>
|
| 56 |
+
<html>
|
| 57 |
+
<head>
|
| 58 |
+
<meta charset="UTF-8">
|
| 59 |
+
<title>{title}</title>
|
| 60 |
+
<style>
|
| 61 |
+
body {{
|
| 62 |
+
font-family: Arial, sans-serif;
|
| 63 |
+
margin: 20px;
|
| 64 |
+
color: #333;
|
| 65 |
+
}}
|
| 66 |
+
.header {{
|
| 67 |
+
text-align: center;
|
| 68 |
+
color: {colors['primary']};
|
| 69 |
+
border-bottom: 3px solid {colors['primary']};
|
| 70 |
+
padding-bottom: 15px;
|
| 71 |
+
margin-bottom: 30px;
|
| 72 |
+
}}
|
| 73 |
+
.title {{
|
| 74 |
+
font-size: 24px;
|
| 75 |
+
font-weight: bold;
|
| 76 |
+
margin: 0;
|
| 77 |
+
}}
|
| 78 |
+
.subtitle {{
|
| 79 |
+
font-size: 14px;
|
| 80 |
+
color: {colors['secondary']};
|
| 81 |
+
margin: 5px 0;
|
| 82 |
+
}}
|
| 83 |
+
.grid {{
|
| 84 |
+
display: grid;
|
| 85 |
+
grid-template-columns: repeat(7, 1fr);
|
| 86 |
+
gap: 2px;
|
| 87 |
+
border: 2px solid {colors['primary']};
|
| 88 |
+
margin: 20px 0;
|
| 89 |
+
}}
|
| 90 |
+
.grid-header {{
|
| 91 |
+
background: {colors['primary']};
|
| 92 |
+
color: white;
|
| 93 |
+
padding: 10px;
|
| 94 |
+
text-align: center;
|
| 95 |
+
font-weight: bold;
|
| 96 |
+
}}
|
| 97 |
+
.grid-cell {{
|
| 98 |
+
border: 1px solid {colors['accent']};
|
| 99 |
+
min-height: 80px;
|
| 100 |
+
padding: 8px;
|
| 101 |
+
background: #fafafa;
|
| 102 |
+
}}
|
| 103 |
+
.notes {{
|
| 104 |
+
margin-top: 30px;
|
| 105 |
+
padding: 20px;
|
| 106 |
+
background: #f8f9fa;
|
| 107 |
+
border-left: 5px solid {colors['accent']};
|
| 108 |
+
}}
|
| 109 |
+
</style>
|
| 110 |
+
</head>
|
| 111 |
+
<body>
|
| 112 |
+
<div class="header">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
<h1 class="title">{title}</h1>
|
| 114 |
<p class="subtitle">Generated on {datetime.now().strftime('%B %d, %Y')}</p>
|
| 115 |
</div>
|
| 116 |
+
|
| 117 |
+
<div class="grid">
|
| 118 |
+
"""
|
| 119 |
|
| 120 |
+
# Add grid headers and cells
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
|
| 122 |
for day in days:
|
| 123 |
+
html_content += f'<div class="grid-header">{day}</div>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
# Add grid cells (4 weeks)
|
| 126 |
+
for i in range(28):
|
| 127 |
+
html_content += f'<div class="grid-cell"><strong>{i+1}</strong></div>'
|
| 128 |
+
|
| 129 |
+
html_content += f"""
|
| 130 |
+
</div>
|
| 131 |
+
|
| 132 |
+
<div class="notes">
|
| 133 |
+
<h3 style="color: {colors['primary']}; margin-top: 0;">Notes & Reminders</h3>
|
| 134 |
+
<div style="height: 100px; background: white; border: 1px solid #ddd; padding: 10px;"></div>
|
| 135 |
+
</div>
|
| 136 |
+
</body>
|
| 137 |
+
</html>
|
| 138 |
+
"""
|
|
|
|
|
|
|
| 139 |
|
| 140 |
+
# Create PDF
|
| 141 |
+
pdf = SimplePDF(title=title, colors=colors)
|
| 142 |
pdf.add_page()
|
| 143 |
|
| 144 |
+
# Add content
|
| 145 |
pdf.set_font('Arial', '', 12)
|
| 146 |
pdf.set_text_color(0, 0, 0)
|
| 147 |
|
| 148 |
+
# Simple grid for PDF
|
| 149 |
y_start = 50
|
| 150 |
cell_width = 25
|
| 151 |
cell_height = 20
|
| 152 |
|
| 153 |
+
# Draw weekly grid
|
| 154 |
+
for row in range(5):
|
| 155 |
for col in range(7):
|
| 156 |
x = 10 + (col * cell_width)
|
| 157 |
y = y_start + (row * cell_height)
|
| 158 |
pdf.rect(x, y, cell_width, cell_height)
|
| 159 |
|
| 160 |
+
if row == 0: # Headers
|
| 161 |
+
pdf.set_xy(x + 1, y + 1)
|
| 162 |
+
pdf.set_font('Arial', 'B', 8)
|
| 163 |
+
pdf.cell(cell_width-2, cell_height-2, days[col][:3], 0, 0, 'C')
|
| 164 |
+
pdf.set_font('Arial', '', 8)
|
| 165 |
|
| 166 |
# Add notes section
|
| 167 |
+
pdf.set_xy(10, y_start + 120)
|
| 168 |
pdf.set_font('Arial', 'B', 14)
|
| 169 |
primary_rgb = tuple(int(colors["primary"][1:][i:i+2], 16) for i in (0, 2, 4))
|
| 170 |
pdf.set_text_color(*primary_rgb)
|
| 171 |
+
pdf.cell(0, 10, 'Notes & Important Reminders', 0, 1, 'L')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
+
# Save PDF
|
| 174 |
temp_dir = "temp_outputs"
|
| 175 |
os.makedirs(temp_dir, exist_ok=True)
|
| 176 |
pdf_path = os.path.join(temp_dir, f"template_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf")
|
| 177 |
pdf.output(pdf_path)
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
success_msg = f"""
|
| 180 |
<div style='padding: 20px; background: #f0f9ff; border-left: 4px solid {colors['primary']};'>
|
| 181 |
<h3 style='color: {colors['primary']}; margin-top: 0;'>â
Template Generated Successfully!</h3>
|
| 182 |
+
<p><strong>Title:</strong> {title}</p>
|
| 183 |
+
<p><strong>Description:</strong> {prompt}</p>
|
| 184 |
+
<p><strong>Color Scheme:</strong> {color_scheme}</p>
|
| 185 |
+
<p><strong>Generated at:</strong> {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
|
|
|
|
|
|
|
| 186 |
</div>
|
| 187 |
"""
|
| 188 |
|
| 189 |
+
return html_content, pdf_path, success_msg
|
|
|
|
| 190 |
|
| 191 |
except Exception as e:
|
| 192 |
+
error_msg = f"<div style='color: red; padding: 20px;'>â Error: {str(e)}</div>"
|
| 193 |
return error_msg, None, f"Error details: {str(e)}"
|
| 194 |
|
| 195 |
# Create Gradio interface
|
| 196 |
+
with gr.Blocks(title="ð¨ AI Template Generator", theme=gr.themes.Default()) as demo:
|
| 197 |
gr.Markdown("""
|
| 198 |
+
# ð¨ AI Template Generator
|
| 199 |
+
### Create Beautiful Planners & Templates with AI
|
|
|
|
| 200 |
""")
|
| 201 |
|
| 202 |
+
with gr.Row():
|
| 203 |
+
with gr.Column(scale=2):
|
| 204 |
+
gr.Markdown("### ð Template Description")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
+
template_preset = gr.Dropdown(
|
| 207 |
+
choices=["Custom"] + list(TEMPLATE_PRESETS.keys()),
|
| 208 |
+
value="Weekly Planner",
|
| 209 |
+
label="ð§ Quick Presets"
|
| 210 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
+
prompt_input = gr.Textbox(
|
| 213 |
+
label="ð Describe Your Template",
|
| 214 |
+
placeholder="Describe what kind of planner you need...",
|
| 215 |
+
lines=3,
|
| 216 |
+
value=TEMPLATE_PRESETS["Weekly Planner"]
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
def update_prompt(preset):
|
| 220 |
+
if preset and preset != "Custom":
|
| 221 |
+
return TEMPLATE_PRESETS[preset]
|
| 222 |
+
return ""
|
| 223 |
+
|
| 224 |
+
template_preset.change(update_prompt, template_preset, prompt_input)
|
| 225 |
|
| 226 |
+
with gr.Column(scale=1):
|
| 227 |
+
gr.Markdown("### ð¨ Customization")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
+
title_input = gr.Textbox(
|
| 230 |
+
label="ð Template Title",
|
| 231 |
+
placeholder="My Planner",
|
| 232 |
+
value="My Weekly Planner"
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
color_scheme = gr.Dropdown(
|
| 236 |
+
choices=list(COLOR_SCHEMES.keys()),
|
| 237 |
+
value="Professional Blue",
|
| 238 |
+
label="ð Color Scheme"
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
generate_btn = gr.Button("ð Generate Template", variant="primary", size="lg")
|
| 242 |
+
|
| 243 |
+
with gr.Row():
|
| 244 |
+
with gr.Column(scale=2):
|
| 245 |
+
preview_output = gr.HTML(label="ð Template Preview")
|
| 246 |
+
|
| 247 |
+
with gr.Column(scale=1):
|
| 248 |
+
download_output = gr.File(label="ð¥ Download PDF")
|
| 249 |
+
status_output = gr.HTML(label="ð Generation Status")
|
| 250 |
|
| 251 |
# Connect the generate button
|
| 252 |
generate_btn.click(
|
| 253 |
+
create_simple_template,
|
| 254 |
+
inputs=[prompt_input, title_input, color_scheme],
|
| 255 |
+
outputs=[preview_output, download_output, status_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
)
|
| 257 |
|
| 258 |
gr.Markdown("""
|
| 259 |
---
|
| 260 |
+
### ð¡ Tips:
|
| 261 |
+
- **Be specific**: Mention size (A4), orientation (portrait/landscape)
|
| 262 |
+
- **Include details**: Time ranges, number of columns, special sections
|
| 263 |
+
- **Try presets**: Use quick presets as starting points
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
""")
|
| 265 |
|
| 266 |
if __name__ == "__main__":
|
| 267 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|