import gradio as gr
import json
from datetime import datetime
import base64
import tempfile
import os
from fpdf import FPDF
from PIL import Image
# Simple template presets
TEMPLATE_PRESETS = {
"Weekly Planner": "A4 weekly planner, portrait orientation, Monday start, 7 columns, notes section",
"Monthly Calendar": "A4 monthly calendar, landscape orientation, large date boxes, clean design",
"Daily Schedule": "A4 daily planner, hourly time slots, priority tasks section",
"Habit Tracker": "A4 habit tracker, 30-day grid, progress tracking",
"Study Planner": "A4 study schedule, subject blocks, assignment tracker"
}
# Color schemes
COLOR_SCHEMES = {
"Professional Blue": {"primary": "#2563EB", "secondary": "#1E40AF", "accent": "#3B82F6"},
"Forest Green": {"primary": "#059669", "secondary": "#047857", "accent": "#10B981"},
"Sunset Orange": {"primary": "#EA580C", "secondary": "#C2410C", "accent": "#FB923C"},
"Royal Purple": {"primary": "#7C3AED", "secondary": "#5B21B6", "accent": "#8B5CF6"}
}
class SimplePDF(FPDF):
def __init__(self, title="My Planner", colors=None):
super().__init__()
self.title_text = title
self.colors = colors or COLOR_SCHEMES["Professional Blue"]
def header(self):
self.set_font('Arial', 'B', 16)
# Convert hex to RGB
primary_rgb = tuple(int(self.colors["primary"][1:][i:i+2], 16) for i in (0, 2, 4))
self.set_text_color(*primary_rgb)
self.cell(0, 10, self.title_text, 0, 1, 'C')
self.ln(10)
def footer(self):
self.set_y(-15)
self.set_font('Arial', 'I', 8)
self.set_text_color(128, 128, 128)
self.cell(0, 10, f'Generated on {datetime.now().strftime("%Y-%m-%d")}', 0, 0, 'C')
def create_simple_template(prompt, title, color_scheme):
"""Create a simple template based on user input"""
try:
# Get colors
colors = COLOR_SCHEMES.get(color_scheme, COLOR_SCHEMES["Professional Blue"])
# Create HTML preview
html_content = f"""
{title}
"""
# Add grid headers and cells
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
for day in days:
html_content += f''
# Add grid cells (4 weeks)
for i in range(28):
html_content += f'
{i+1}
'
html_content += f"""
"""
# Create PDF
pdf = SimplePDF(title=title, colors=colors)
pdf.add_page()
# Add content
pdf.set_font('Arial', '', 12)
pdf.set_text_color(0, 0, 0)
# Simple grid for PDF
y_start = 50
cell_width = 25
cell_height = 20
# Draw weekly grid
for row in range(5):
for col in range(7):
x = 10 + (col * cell_width)
y = y_start + (row * cell_height)
pdf.rect(x, y, cell_width, cell_height)
if row == 0: # Headers
pdf.set_xy(x + 1, y + 1)
pdf.set_font('Arial', 'B', 8)
pdf.cell(cell_width-2, cell_height-2, days[col][:3], 0, 0, 'C')
pdf.set_font('Arial', '', 8)
# Add notes section
pdf.set_xy(10, y_start + 120)
pdf.set_font('Arial', 'B', 14)
primary_rgb = tuple(int(colors["primary"][1:][i:i+2], 16) for i in (0, 2, 4))
pdf.set_text_color(*primary_rgb)
pdf.cell(0, 10, 'Notes & Important Reminders', 0, 1, 'L')
# Save PDF
temp_dir = "temp_outputs"
os.makedirs(temp_dir, exist_ok=True)
pdf_path = os.path.join(temp_dir, f"template_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf")
pdf.output(pdf_path)
success_msg = f"""
â
Template Generated Successfully!
Title: {title}
Description: {prompt}
Color Scheme: {color_scheme}
Generated at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
"""
return html_content, pdf_path, success_msg
except Exception as e:
error_msg = f"â Error: {str(e)}
"
return error_msg, None, f"Error details: {str(e)}"
# Create Gradio interface
with gr.Blocks(title="ð¨ AI Template Generator", theme=gr.themes.Default()) as demo:
gr.Markdown("""
# ð¨ AI Template Generator
### Create Beautiful Planners & Templates with AI
""")
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("### ð Template Description")
template_preset = gr.Dropdown(
choices=["Custom"] + list(TEMPLATE_PRESETS.keys()),
value="Weekly Planner",
label="ð§ Quick Presets"
)
prompt_input = gr.Textbox(
label="ð Describe Your Template",
placeholder="Describe what kind of planner you need...",
lines=3,
value=TEMPLATE_PRESETS["Weekly Planner"]
)
def update_prompt(preset):
if preset and preset != "Custom":
return TEMPLATE_PRESETS[preset]
return ""
template_preset.change(update_prompt, template_preset, prompt_input)
with gr.Column(scale=1):
gr.Markdown("### ð¨ Customization")
title_input = gr.Textbox(
label="ð Template Title",
placeholder="My Planner",
value="My Weekly Planner"
)
color_scheme = gr.Dropdown(
choices=list(COLOR_SCHEMES.keys()),
value="Professional Blue",
label="ð Color Scheme"
)
generate_btn = gr.Button("ð Generate Template", variant="primary", size="lg")
with gr.Row():
with gr.Column(scale=2):
preview_output = gr.HTML(label="ð Template Preview")
with gr.Column(scale=1):
download_output = gr.File(label="ð¥ Download PDF")
status_output = gr.HTML(label="ð Generation Status")
# Connect the generate button
generate_btn.click(
create_simple_template,
inputs=[prompt_input, title_input, color_scheme],
outputs=[preview_output, download_output, status_output]
)
gr.Markdown("""
---
### ð¡ Tips:
- **Be specific**: Mention size (A4), orientation (portrait/landscape)
- **Include details**: Time ranges, number of columns, special sections
- **Try presets**: Use quick presets as starting points
""")
if __name__ == "__main__":
demo.launch()