Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,41 +2,16 @@ from smolagents import CodeAgent, HfApiModel, tool
|
|
| 2 |
import os
|
| 3 |
import yaml
|
| 4 |
import time
|
| 5 |
-
import
|
|
|
|
| 6 |
from PIL import Image, ImageDraw, ImageFont
|
| 7 |
-
from io import BytesIO
|
| 8 |
from tools.final_answer import FinalAnswerTool
|
| 9 |
|
| 10 |
-
#
|
| 11 |
IMAGES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "coloring_images")
|
|
|
|
| 12 |
os.makedirs(IMAGES_DIR, exist_ok=True)
|
| 13 |
-
|
| 14 |
-
# Функция для генерации изображения через Hugging Face Stable Diffusion API
|
| 15 |
-
def generate_image_hf(prompt):
|
| 16 |
-
try:
|
| 17 |
-
# Используем Stable Diffusion XL модель через Hugging Face Inference API
|
| 18 |
-
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
| 19 |
-
headers = {"Authorization": "Bearer hf_your_huggingface_token"} # Замените на свой токен
|
| 20 |
-
|
| 21 |
-
payload = {
|
| 22 |
-
"inputs": prompt,
|
| 23 |
-
"parameters": {
|
| 24 |
-
"negative_prompt": "color, colored, shading, realistic, photorealistic, detailed, 3d",
|
| 25 |
-
"guidance_scale": 9.0
|
| 26 |
-
}
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
| 30 |
-
|
| 31 |
-
if response.status_code == 200:
|
| 32 |
-
image = Image.open(BytesIO(response.content))
|
| 33 |
-
return image
|
| 34 |
-
else:
|
| 35 |
-
print(f"Error from HF API: {response.status_code} - {response.text}")
|
| 36 |
-
return None
|
| 37 |
-
except Exception as e:
|
| 38 |
-
print(f"Exception in HF image generation: {str(e)}")
|
| 39 |
-
return None
|
| 40 |
|
| 41 |
@tool
|
| 42 |
def generate_passover_coloring_pages(age: int) -> str:
|
|
@@ -60,95 +35,76 @@ def generate_passover_coloring_pages(age: int) -> str:
|
|
| 60 |
|
| 61 |
complexity = next(value for (low, high), value in complexity_levels.items() if low <= age <= high)
|
| 62 |
|
| 63 |
-
# Enhanced prompts specifically for Jewish Passover/Pesach themes
|
| 64 |
passover_pages = {
|
| 65 |
"simple": [
|
| 66 |
-
"
|
| 67 |
-
"
|
| 68 |
-
"
|
| 69 |
-
"
|
| 70 |
],
|
| 71 |
"basic": [
|
| 72 |
-
"
|
| 73 |
-
"
|
| 74 |
-
"
|
| 75 |
-
"
|
| 76 |
],
|
| 77 |
"intermediate": [
|
| 78 |
-
"
|
| 79 |
-
"Hebrew letters spelling 'Pesach'
|
| 80 |
-
"
|
| 81 |
-
"
|
| 82 |
],
|
| 83 |
"advanced": [
|
| 84 |
-
"
|
| 85 |
-
"
|
| 86 |
-
"
|
| 87 |
-
"
|
| 88 |
]
|
| 89 |
}
|
| 90 |
|
| 91 |
# Get descriptions for selected age group
|
| 92 |
selected_pages = passover_pages[complexity]
|
| 93 |
-
descriptions = [prompt.split(',')[0] for prompt in selected_pages]
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
# Create a cover page
|
| 99 |
cover_img = Image.new('RGB', (2480, 3508), color='white') # A4 size at 300 DPI
|
| 100 |
draw = ImageDraw.Draw(cover_img)
|
| 101 |
|
| 102 |
-
# Add title to cover page
|
| 103 |
try:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
| 122 |
|
| 123 |
-
# Create
|
| 124 |
-
|
|
|
|
| 125 |
try:
|
| 126 |
-
#
|
| 127 |
-
|
| 128 |
-
full_prompt = prompt + ", coloring page style, black and white outlines only, no color, no shading, thick clean lines, suitable for children to color"
|
| 129 |
-
|
| 130 |
-
# Attempt to generate image through HF API
|
| 131 |
-
img = generate_image_hf(full_prompt)
|
| 132 |
-
|
| 133 |
-
if img is None:
|
| 134 |
-
raise Exception("Image generation failed")
|
| 135 |
-
|
| 136 |
-
# Convert to black and white line art if needed
|
| 137 |
-
img = img.convert('L') # Convert to grayscale
|
| 138 |
-
img = img.point(lambda x: 0 if x < 128 else 255, '1') # Convert to black and white
|
| 139 |
-
|
| 140 |
-
# Save the generated image
|
| 141 |
-
img_filename = f"passover_{age}_{i}_{timestamp}.png"
|
| 142 |
-
img_path = os.path.join(IMAGES_DIR, img_filename)
|
| 143 |
-
img.save(img_path)
|
| 144 |
-
image_paths.append((img_path, img_filename, description))
|
| 145 |
-
print(f"Generated image {i+1}/{len(selected_pages)}")
|
| 146 |
-
|
| 147 |
-
except Exception as e:
|
| 148 |
-
print(f"Error generating image {i}: {str(e)}")
|
| 149 |
-
|
| 150 |
-
# Create a placeholder with text if generation fails
|
| 151 |
-
img = Image.new('RGB', (2480, 3508), color='white') # A4 size
|
| 152 |
draw = ImageDraw.Draw(img)
|
| 153 |
|
| 154 |
# Add a border
|
|
@@ -161,25 +117,102 @@ def generate_passover_coloring_pages(age: int) -> str:
|
|
| 161 |
draw.rectangle([(300, 400), (2180, 2800)], outline='black', width=2)
|
| 162 |
|
| 163 |
# Add description text in the content area
|
| 164 |
-
draw.text((1240, 1600), description, fill='black', anchor='mm',
|
| 165 |
-
draw.text((1240, 1700), "Jewish Passover Coloring Page", fill='black', anchor='mm',
|
| 166 |
-
draw.text((1240, 1800), f"For age: {age}", fill='black', anchor='mm',
|
| 167 |
|
| 168 |
# Add footer
|
| 169 |
-
draw.text((1240, 3300), f"Page {i+1} of {len(selected_pages)}", fill='black', anchor='mm',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
-
#
|
| 172 |
-
|
| 173 |
-
img_path
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
<html>
|
| 184 |
<head>
|
| 185 |
<title>Jewish Passover Coloring Pages - Age {age}</title>
|
|
@@ -204,41 +237,62 @@ def generate_passover_coloring_pages(age: int) -> str:
|
|
| 204 |
<div class="controls">
|
| 205 |
<h1>Jewish Passover (Pesach) Coloring Pages</h1>
|
| 206 |
<h2>For Ages {age} Years</h2>
|
| 207 |
-
<p>
|
| 208 |
<button class="print-button" onclick="window.print()">Print All Pages</button>
|
| 209 |
</div>
|
| 210 |
""")
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
|
|
|
| 215 |
<div class="page">
|
| 216 |
-
<img src="{img_filename}" alt="{
|
| 217 |
-
<div class="page-title">{
|
| 218 |
</div>
|
| 219 |
""")
|
| 220 |
-
|
| 221 |
-
|
| 222 |
</body>
|
| 223 |
</html>
|
| 224 |
""")
|
| 225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
# Create formatted response
|
| 227 |
response = f"## Jewish Passover (Pesach) Coloring Pages for a {age}-year-old:\n\n"
|
| 228 |
-
response += "\n".join([f"- {desc}" for desc in
|
| 229 |
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
# Add printing instructions
|
| 237 |
response += f"\n\n**Printing Instructions:**\n"
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
response += f"You can also print individual pages by opening the PNG files directly."
|
| 243 |
|
| 244 |
return response
|
|
@@ -331,12 +385,12 @@ try:
|
|
| 331 |
|
| 332 |
with gr.Blocks() as demo:
|
| 333 |
gr.Markdown("# Jewish Passover (Pesach) Coloring Pages Generator")
|
| 334 |
-
gr.Markdown("### Generates print-ready coloring pages
|
| 335 |
|
| 336 |
with gr.Row():
|
| 337 |
with gr.Column():
|
| 338 |
input_box = gr.Number(label="Enter child's age (1-12)", minimum=1, maximum=12, step=1)
|
| 339 |
-
submit_btn = gr.Button("Generate Coloring Pages")
|
| 340 |
|
| 341 |
with gr.Row():
|
| 342 |
output_text = gr.Markdown()
|
|
@@ -351,7 +405,7 @@ try:
|
|
| 351 |
result = generate_passover_coloring_pages(int(age))
|
| 352 |
|
| 353 |
# Add a clearer message
|
| 354 |
-
result += f"\n\n**Note:** Gradio cannot display
|
| 355 |
|
| 356 |
return result
|
| 357 |
except Exception as e:
|
|
|
|
| 2 |
import os
|
| 3 |
import yaml
|
| 4 |
import time
|
| 5 |
+
import subprocess
|
| 6 |
+
import shutil
|
| 7 |
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
| 8 |
from tools.final_answer import FinalAnswerTool
|
| 9 |
|
| 10 |
+
# Create directories for images and PDFs
|
| 11 |
IMAGES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "coloring_images")
|
| 12 |
+
PDF_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "coloring_pdf")
|
| 13 |
os.makedirs(IMAGES_DIR, exist_ok=True)
|
| 14 |
+
os.makedirs(PDF_DIR, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@tool
|
| 17 |
def generate_passover_coloring_pages(age: int) -> str:
|
|
|
|
| 35 |
|
| 36 |
complexity = next(value for (low, high), value in complexity_levels.items() if low <= age <= high)
|
| 37 |
|
| 38 |
+
# Enhanced prompts specifically for Jewish Passover/Pesach themes
|
| 39 |
passover_pages = {
|
| 40 |
"simple": [
|
| 41 |
+
"Matzah for Passover",
|
| 42 |
+
"Seder plate outline",
|
| 43 |
+
"Afikoman hidden in fabric",
|
| 44 |
+
"Kiddush cup for Passover"
|
| 45 |
],
|
| 46 |
"basic": [
|
| 47 |
+
"Moses parting the Red Sea",
|
| 48 |
+
"Ten Plagues illustrations",
|
| 49 |
+
"Four Questions from Haggadah",
|
| 50 |
+
"Afikoman hunt maze"
|
| 51 |
],
|
| 52 |
"intermediate": [
|
| 53 |
+
"Jewish Seder table with symbolic foods",
|
| 54 |
+
"Hebrew letters spelling 'Pesach'",
|
| 55 |
+
"Israelites leaving Egypt scene",
|
| 56 |
+
"Four Sons from the Haggadah"
|
| 57 |
],
|
| 58 |
"advanced": [
|
| 59 |
+
"Haggadah page with Seder order",
|
| 60 |
+
"Miriam and women celebrating after Red Sea",
|
| 61 |
+
"Hebrew calligraphy 'Next year in Jerusalem'",
|
| 62 |
+
"Passover journey from slavery to freedom"
|
| 63 |
]
|
| 64 |
}
|
| 65 |
|
| 66 |
# Get descriptions for selected age group
|
| 67 |
selected_pages = passover_pages[complexity]
|
|
|
|
| 68 |
|
| 69 |
+
# Generate and save images
|
| 70 |
+
timestamp = int(time.time())
|
| 71 |
+
|
| 72 |
+
# Create a temporary directory for images that will be combined into PDF
|
| 73 |
+
temp_dir = os.path.join(IMAGES_DIR, f"temp_{timestamp}")
|
| 74 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 75 |
|
| 76 |
+
# Create a cover page
|
| 77 |
cover_img = Image.new('RGB', (2480, 3508), color='white') # A4 size at 300 DPI
|
| 78 |
draw = ImageDraw.Draw(cover_img)
|
| 79 |
|
|
|
|
| 80 |
try:
|
| 81 |
+
# Try to load a font, use default if not available
|
| 82 |
+
try:
|
| 83 |
+
font = ImageFont.truetype("Arial", 80)
|
| 84 |
+
small_font = ImageFont.truetype("Arial", 40)
|
| 85 |
+
except:
|
| 86 |
+
font = ImageFont.load_default()
|
| 87 |
+
small_font = ImageFont.load_default()
|
| 88 |
+
|
| 89 |
+
# Add title to cover page
|
| 90 |
+
draw.text((1240, 800), "Jewish Passover (Pesach)", fill='black', anchor='mm', font=font)
|
| 91 |
+
draw.text((1240, 1000), "Coloring Pages", fill='black', anchor='mm', font=font)
|
| 92 |
+
draw.text((1240, 1200), f"For Ages {age} Years", fill='black', anchor='mm', font=font)
|
| 93 |
+
draw.text((1240, 1500), "Created for Jewish children and families", fill='black', anchor='mm', small_font)
|
| 94 |
+
draw.text((1240, 1600), "to celebrate the story of Passover", fill='black', anchor='mm', small_font)
|
| 95 |
+
|
| 96 |
+
# Save cover page with a sequential number for proper ordering
|
| 97 |
+
cover_path = os.path.join(temp_dir, f"01_cover.png")
|
| 98 |
+
cover_img.save(cover_path)
|
| 99 |
+
except Exception as e:
|
| 100 |
+
print(f"Error creating cover page: {str(e)}")
|
| 101 |
|
| 102 |
+
# Create individual coloring pages
|
| 103 |
+
page_paths = []
|
| 104 |
+
for i, description in enumerate(selected_pages):
|
| 105 |
try:
|
| 106 |
+
# Create an A4 sized image
|
| 107 |
+
img = Image.new('RGB', (2480, 3508), color='white')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
draw = ImageDraw.Draw(img)
|
| 109 |
|
| 110 |
# Add a border
|
|
|
|
| 117 |
draw.rectangle([(300, 400), (2180, 2800)], outline='black', width=2)
|
| 118 |
|
| 119 |
# Add description text in the content area
|
| 120 |
+
draw.text((1240, 1600), description, fill='black', anchor='mm', small_font)
|
| 121 |
+
draw.text((1240, 1700), "Jewish Passover Coloring Page", fill='black', anchor='mm', small_font)
|
| 122 |
+
draw.text((1240, 1800), f"For age: {age}", fill='black', anchor='mm', small_font)
|
| 123 |
|
| 124 |
# Add footer
|
| 125 |
+
draw.text((1240, 3300), f"Page {i+1} of {len(selected_pages)}", fill='black', anchor='mm', small_font)
|
| 126 |
+
|
| 127 |
+
# Save with sequential numbering for proper PDF ordering
|
| 128 |
+
seq_num = i + 2 # Start from 2 after the cover page
|
| 129 |
+
page_path = os.path.join(temp_dir, f"{seq_num:02d}_{i+1}.png")
|
| 130 |
+
img.save(page_path)
|
| 131 |
+
page_paths.append(page_path)
|
| 132 |
+
|
| 133 |
+
# Also save a copy in the main images directory
|
| 134 |
+
main_path = os.path.join(IMAGES_DIR, f"passover_{age}_{i}_{timestamp}.png")
|
| 135 |
+
img.save(main_path)
|
| 136 |
+
except Exception as e:
|
| 137 |
+
print(f"Error creating page {i+1}: {str(e)}")
|
| 138 |
+
|
| 139 |
+
# Now create a PDF using one of several methods
|
| 140 |
+
|
| 141 |
+
# Method 1: Try using img2pdf (requires img2pdf to be installed)
|
| 142 |
+
pdf_filename = f"passover_coloring_pages_age_{age}_{timestamp}.pdf"
|
| 143 |
+
pdf_path = os.path.join(PDF_DIR, pdf_filename)
|
| 144 |
+
|
| 145 |
+
# Get all image files in the temporary directory in correct order
|
| 146 |
+
all_images = sorted([os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if f.endswith('.png')])
|
| 147 |
+
|
| 148 |
+
pdf_created = False
|
| 149 |
+
|
| 150 |
+
# Try different methods to create the PDF
|
| 151 |
+
|
| 152 |
+
# Method 1: Try using img2pdf via Python
|
| 153 |
+
try:
|
| 154 |
+
import img2pdf
|
| 155 |
+
with open(pdf_path, "wb") as f:
|
| 156 |
+
f.write(img2pdf.convert(all_images))
|
| 157 |
+
pdf_created = True
|
| 158 |
+
print(f"PDF created with img2pdf Python module: {pdf_path}")
|
| 159 |
+
except ImportError:
|
| 160 |
+
print("img2pdf Python module not available, trying alternative method...")
|
| 161 |
+
|
| 162 |
+
# Method 2: Try using ImageMagick convert command
|
| 163 |
+
if not pdf_created:
|
| 164 |
+
try:
|
| 165 |
+
convert_cmd = ["convert"]
|
| 166 |
+
convert_cmd.extend(all_images)
|
| 167 |
+
convert_cmd.append(pdf_path)
|
| 168 |
+
subprocess.run(convert_cmd, check=True)
|
| 169 |
+
pdf_created = True
|
| 170 |
+
print(f"PDF created with ImageMagick: {pdf_path}")
|
| 171 |
+
except (subprocess.SubprocessError, FileNotFoundError):
|
| 172 |
+
print("ImageMagick not available, trying alternative method...")
|
| 173 |
+
|
| 174 |
+
# Method 3: Try using the img2pdf command-line tool
|
| 175 |
+
if not pdf_created:
|
| 176 |
+
try:
|
| 177 |
+
img2pdf_cmd = ["img2pdf"]
|
| 178 |
+
img2pdf_cmd.extend(all_images)
|
| 179 |
+
img2pdf_cmd.extend(["-o", pdf_path])
|
| 180 |
+
subprocess.run(img2pdf_cmd, check=True)
|
| 181 |
+
pdf_created = True
|
| 182 |
+
print(f"PDF created with img2pdf command-line: {pdf_path}")
|
| 183 |
+
except (subprocess.SubprocessError, FileNotFoundError):
|
| 184 |
+
print("img2pdf command-line tool not available, trying alternative method...")
|
| 185 |
+
|
| 186 |
+
# Method 4: Try using Python Pillow
|
| 187 |
+
if not pdf_created:
|
| 188 |
+
try:
|
| 189 |
+
# Open the first image to get the size
|
| 190 |
+
first_img = Image.open(all_images[0])
|
| 191 |
|
| 192 |
+
# Create a list of images
|
| 193 |
+
images = []
|
| 194 |
+
for img_path in all_images[1:]: # Skip the first one as we already opened it
|
| 195 |
+
images.append(Image.open(img_path))
|
| 196 |
+
|
| 197 |
+
# Save as PDF
|
| 198 |
+
first_img.save(
|
| 199 |
+
pdf_path,
|
| 200 |
+
"PDF",
|
| 201 |
+
resolution=100.0,
|
| 202 |
+
save_all=True,
|
| 203 |
+
append_images=images
|
| 204 |
+
)
|
| 205 |
+
pdf_created = True
|
| 206 |
+
print(f"PDF created with Pillow: {pdf_path}")
|
| 207 |
+
except Exception as e:
|
| 208 |
+
print(f"Pillow PDF creation failed: {str(e)}")
|
| 209 |
+
|
| 210 |
+
# If PDF creation failed, use the HTML approach as a fallback
|
| 211 |
+
if not pdf_created:
|
| 212 |
+
# Create a simple HTML for viewing/printing
|
| 213 |
+
html_path = os.path.join(IMAGES_DIR, f"passover_coloring_pages_{age}_{timestamp}.html")
|
| 214 |
+
with open(html_path, 'w') as f:
|
| 215 |
+
f.write(f"""<!DOCTYPE html>
|
| 216 |
<html>
|
| 217 |
<head>
|
| 218 |
<title>Jewish Passover Coloring Pages - Age {age}</title>
|
|
|
|
| 237 |
<div class="controls">
|
| 238 |
<h1>Jewish Passover (Pesach) Coloring Pages</h1>
|
| 239 |
<h2>For Ages {age} Years</h2>
|
| 240 |
+
<p>PDF creation failed. Use this page to print instead.</p>
|
| 241 |
<button class="print-button" onclick="window.print()">Print All Pages</button>
|
| 242 |
</div>
|
| 243 |
""")
|
| 244 |
+
|
| 245 |
+
# Add each image from the main directory to the HTML
|
| 246 |
+
for i, _ in enumerate(selected_pages):
|
| 247 |
+
img_filename = f"passover_{age}_{i}_{timestamp}.png"
|
| 248 |
+
f.write(f"""
|
| 249 |
<div class="page">
|
| 250 |
+
<img src="{img_filename}" alt="Coloring Page {i+1}">
|
| 251 |
+
<div class="page-title">{selected_pages[i]}</div>
|
| 252 |
</div>
|
| 253 |
""")
|
| 254 |
+
|
| 255 |
+
f.write("""
|
| 256 |
</body>
|
| 257 |
</html>
|
| 258 |
""")
|
| 259 |
|
| 260 |
+
# Clean up temp directory
|
| 261 |
+
try:
|
| 262 |
+
shutil.rmtree(temp_dir)
|
| 263 |
+
except Exception as e:
|
| 264 |
+
print(f"Error cleaning up temp directory: {str(e)}")
|
| 265 |
+
|
| 266 |
# Create formatted response
|
| 267 |
response = f"## Jewish Passover (Pesach) Coloring Pages for a {age}-year-old:\n\n"
|
| 268 |
+
response += "\n".join([f"- {desc}" for desc in selected_pages])
|
| 269 |
|
| 270 |
+
if pdf_created:
|
| 271 |
+
# PDF was successfully created
|
| 272 |
+
response += f"\n\n**PDF coloring pages have been generated!**\n\n"
|
| 273 |
+
response += f"To view and print the coloring pages, please open this PDF file:\n\n"
|
| 274 |
+
response += f"{pdf_path}\n\n"
|
| 275 |
+
response += f"The individual image files are also available in the 'coloring_images' folder."
|
| 276 |
+
else:
|
| 277 |
+
# Fallback to HTML
|
| 278 |
+
response += f"\n\n**Coloring pages have been generated! (PDF creation failed)**\n\n"
|
| 279 |
+
response += f"To view and print the coloring pages, please open this HTML file in your web browser:\n\n"
|
| 280 |
+
response += f"{html_path}\n\n"
|
| 281 |
+
response += f"The individual image files are also available in the 'coloring_images' folder."
|
| 282 |
|
| 283 |
# Add printing instructions
|
| 284 |
response += f"\n\n**Printing Instructions:**\n"
|
| 285 |
+
|
| 286 |
+
if pdf_created:
|
| 287 |
+
response += f"1. Open the PDF file in Adobe Reader or any PDF viewer\n"
|
| 288 |
+
response += f"2. Use the print function of your PDF viewer\n"
|
| 289 |
+
response += f"3. Select your printer and print\n\n"
|
| 290 |
+
else:
|
| 291 |
+
response += f"1. Open the HTML file in a web browser\n"
|
| 292 |
+
response += f"2. Click the 'Print All Pages' button\n"
|
| 293 |
+
response += f"3. In the print dialog, make sure 'Background Graphics' is enabled\n"
|
| 294 |
+
response += f"4. Select your printer and print\n\n"
|
| 295 |
+
|
| 296 |
response += f"You can also print individual pages by opening the PNG files directly."
|
| 297 |
|
| 298 |
return response
|
|
|
|
| 385 |
|
| 386 |
with gr.Blocks() as demo:
|
| 387 |
gr.Markdown("# Jewish Passover (Pesach) Coloring Pages Generator")
|
| 388 |
+
gr.Markdown("### Generates print-ready coloring pages as PDF")
|
| 389 |
|
| 390 |
with gr.Row():
|
| 391 |
with gr.Column():
|
| 392 |
input_box = gr.Number(label="Enter child's age (1-12)", minimum=1, maximum=12, step=1)
|
| 393 |
+
submit_btn = gr.Button("Generate PDF Coloring Pages")
|
| 394 |
|
| 395 |
with gr.Row():
|
| 396 |
output_text = gr.Markdown()
|
|
|
|
| 405 |
result = generate_passover_coloring_pages(int(age))
|
| 406 |
|
| 407 |
# Add a clearer message
|
| 408 |
+
result += f"\n\n**Note:** Gradio cannot display or download the PDF directly. Please navigate to the location shown above to open the PDF file."
|
| 409 |
|
| 410 |
return result
|
| 411 |
except Exception as e:
|