Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,6 @@ from smolagents import CodeAgent, HfApiModel, tool
|
|
| 2 |
import os
|
| 3 |
import yaml
|
| 4 |
import time
|
| 5 |
-
import base64
|
| 6 |
-
from io import BytesIO
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
from PIL import Image, ImageDraw, ImageFont
|
| 9 |
|
|
@@ -11,6 +9,9 @@ from PIL import Image, ImageDraw, ImageFont
|
|
| 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 |
@tool
|
| 15 |
def generate_passover_coloring_pages(age: int) -> str:
|
| 16 |
"""Generates Jewish Passover/Pesach-themed coloring pages based on a child's age.
|
|
@@ -66,8 +67,6 @@ def generate_passover_coloring_pages(age: int) -> str:
|
|
| 66 |
|
| 67 |
# Create placeholder images with descriptions
|
| 68 |
image_paths = []
|
| 69 |
-
images_html = ""
|
| 70 |
-
|
| 71 |
for i, description in enumerate(selected_pages):
|
| 72 |
# Create a simple placeholder image with text
|
| 73 |
img = Image.new('RGB', (800, 600), color='white')
|
|
@@ -81,36 +80,157 @@ def generate_passover_coloring_pages(age: int) -> str:
|
|
| 81 |
draw.text((400, 350), "Jewish Passover Coloring Page", fill='black', anchor='mm')
|
| 82 |
draw.text((400, 400), f"For age: {age}", fill='black', anchor='mm')
|
| 83 |
|
| 84 |
-
# Save the image
|
| 85 |
timestamp = int(time.time())
|
| 86 |
-
|
|
|
|
| 87 |
img.save(img_path)
|
| 88 |
-
image_paths.append(img_path)
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
</div>
|
| 102 |
"""
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
# Create formatted response
|
| 105 |
response = f"## Jewish Passover (Pesach) Coloring Pages for a {age}-year-old:\n\n"
|
| 106 |
response += "\n".join([f"- {desc}" for desc in selected_pages])
|
| 107 |
|
| 108 |
-
# Add
|
| 109 |
-
response += f"\n\n
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
image_paths_str = "||".join(image_paths)
|
| 113 |
-
return f"{response}\n\nIMAGE_PATHS:{image_paths_str}"
|
| 114 |
|
| 115 |
# Setup agent
|
| 116 |
final_answer = FinalAnswerTool()
|
|
@@ -183,27 +303,8 @@ def run_with_simple_interface():
|
|
| 183 |
print(f"Generating coloring pages for age {age}...")
|
| 184 |
result = generate_passover_coloring_pages(age)
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
display_result = result.split("\n\n<div>")[0]
|
| 189 |
-
elif "IMAGE_PATHS:" in result:
|
| 190 |
-
display_result = result.split("\n\nIMAGE_PATHS:")[0]
|
| 191 |
-
else:
|
| 192 |
-
display_result = result
|
| 193 |
-
|
| 194 |
-
print("\n" + display_result)
|
| 195 |
-
|
| 196 |
-
# Extract image paths
|
| 197 |
-
image_paths = []
|
| 198 |
-
if "IMAGE_PATHS:" in result:
|
| 199 |
-
paths_part = result.split("IMAGE_PATHS:")[1]
|
| 200 |
-
image_paths = paths_part.split("||")
|
| 201 |
-
|
| 202 |
-
print(f"\nCreated {len(image_paths)} coloring pages:")
|
| 203 |
-
for path in image_paths:
|
| 204 |
-
print(f"- {path}")
|
| 205 |
-
|
| 206 |
-
print("\nOpen these image files to view and print the coloring pages.")
|
| 207 |
|
| 208 |
except ValueError:
|
| 209 |
print("Please enter a valid number for the child's age.")
|
|
@@ -213,10 +314,11 @@ def run_with_simple_interface():
|
|
| 213 |
# Launch Gradio UI if available, otherwise use simple interface
|
| 214 |
try:
|
| 215 |
from Gradio_UI import GradioUI
|
| 216 |
-
import gradio as gr
|
| 217 |
|
| 218 |
class SimpleGradioUI(GradioUI):
|
| 219 |
def launch(self, **kwargs):
|
|
|
|
|
|
|
| 220 |
with gr.Blocks() as demo:
|
| 221 |
gr.Markdown("# Jewish Passover (Pesach) Coloring Pages Generator")
|
| 222 |
|
|
@@ -226,7 +328,7 @@ try:
|
|
| 226 |
submit_btn = gr.Button("Generate Coloring Pages")
|
| 227 |
|
| 228 |
with gr.Row():
|
| 229 |
-
output_text = gr.
|
| 230 |
|
| 231 |
# Simple function to process input without complex logic
|
| 232 |
def process_input(age):
|
|
@@ -237,19 +339,13 @@ try:
|
|
| 237 |
# Call the tool directly
|
| 238 |
result = generate_passover_coloring_pages(int(age))
|
| 239 |
|
| 240 |
-
#
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
html_result += html_part
|
| 246 |
-
return html_result
|
| 247 |
-
else:
|
| 248 |
-
# Fallback if no HTML section
|
| 249 |
-
text_result = result.split("\n\nIMAGE_PATHS:")[0] if "IMAGE_PATHS:" in result else result
|
| 250 |
-
return f"<pre>{text_result}</pre><p>Images have been saved to the 'coloring_images' folder.</p>"
|
| 251 |
except Exception as e:
|
| 252 |
-
return f"
|
| 253 |
|
| 254 |
submit_btn.click(process_input, inputs=input_box, outputs=output_text)
|
| 255 |
|
|
|
|
| 2 |
import os
|
| 3 |
import yaml
|
| 4 |
import time
|
|
|
|
|
|
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
| 6 |
from PIL import Image, ImageDraw, ImageFont
|
| 7 |
|
|
|
|
| 9 |
IMAGES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "coloring_images")
|
| 10 |
os.makedirs(IMAGES_DIR, exist_ok=True)
|
| 11 |
|
| 12 |
+
# Path for the HTML viewer
|
| 13 |
+
HTML_VIEWER_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "coloring_viewer.html")
|
| 14 |
+
|
| 15 |
@tool
|
| 16 |
def generate_passover_coloring_pages(age: int) -> str:
|
| 17 |
"""Generates Jewish Passover/Pesach-themed coloring pages based on a child's age.
|
|
|
|
| 67 |
|
| 68 |
# Create placeholder images with descriptions
|
| 69 |
image_paths = []
|
|
|
|
|
|
|
| 70 |
for i, description in enumerate(selected_pages):
|
| 71 |
# Create a simple placeholder image with text
|
| 72 |
img = Image.new('RGB', (800, 600), color='white')
|
|
|
|
| 80 |
draw.text((400, 350), "Jewish Passover Coloring Page", fill='black', anchor='mm')
|
| 81 |
draw.text((400, 400), f"For age: {age}", fill='black', anchor='mm')
|
| 82 |
|
| 83 |
+
# Save the image
|
| 84 |
timestamp = int(time.time())
|
| 85 |
+
img_filename = f"passover_{age}_{i}_{timestamp}.png"
|
| 86 |
+
img_path = os.path.join(IMAGES_DIR, img_filename)
|
| 87 |
img.save(img_path)
|
| 88 |
+
image_paths.append((img_path, img_filename, description))
|
| 89 |
+
|
| 90 |
+
# Create a standalone HTML file to view images
|
| 91 |
+
html_content = """
|
| 92 |
+
<!DOCTYPE html>
|
| 93 |
+
<html>
|
| 94 |
+
<head>
|
| 95 |
+
<title>Jewish Passover Coloring Pages</title>
|
| 96 |
+
<style>
|
| 97 |
+
body {
|
| 98 |
+
font-family: Arial, sans-serif;
|
| 99 |
+
max-width: 1200px;
|
| 100 |
+
margin: 0 auto;
|
| 101 |
+
padding: 20px;
|
| 102 |
+
}
|
| 103 |
+
h1 {
|
| 104 |
+
color: #2c3e50;
|
| 105 |
+
text-align: center;
|
| 106 |
+
}
|
| 107 |
+
.gallery {
|
| 108 |
+
display: flex;
|
| 109 |
+
flex-wrap: wrap;
|
| 110 |
+
justify-content: center;
|
| 111 |
+
}
|
| 112 |
+
.coloring-page {
|
| 113 |
+
margin: 15px;
|
| 114 |
+
border: 1px solid #ddd;
|
| 115 |
+
border-radius: 8px;
|
| 116 |
+
padding: 15px;
|
| 117 |
+
width: 300px;
|
| 118 |
+
text-align: center;
|
| 119 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
| 120 |
+
}
|
| 121 |
+
.coloring-page img {
|
| 122 |
+
max-width: 100%;
|
| 123 |
+
height: auto;
|
| 124 |
+
border: 1px solid #eee;
|
| 125 |
+
}
|
| 126 |
+
.coloring-page h3 {
|
| 127 |
+
margin: 10px 0;
|
| 128 |
+
color: #3498db;
|
| 129 |
+
}
|
| 130 |
+
.coloring-page p {
|
| 131 |
+
color: #7f8c8d;
|
| 132 |
+
}
|
| 133 |
+
.print-button {
|
| 134 |
+
background-color: #3498db;
|
| 135 |
+
color: white;
|
| 136 |
+
border: none;
|
| 137 |
+
padding: 8px 15px;
|
| 138 |
+
border-radius: 4px;
|
| 139 |
+
cursor: pointer;
|
| 140 |
+
margin-top: 10px;
|
| 141 |
+
}
|
| 142 |
+
.print-button:hover {
|
| 143 |
+
background-color: #2980b9;
|
| 144 |
+
}
|
| 145 |
+
@media print {
|
| 146 |
+
.no-print {
|
| 147 |
+
display: none;
|
| 148 |
+
}
|
| 149 |
+
.coloring-page {
|
| 150 |
+
page-break-inside: avoid;
|
| 151 |
+
border: none;
|
| 152 |
+
box-shadow: none;
|
| 153 |
+
width: 100%;
|
| 154 |
+
}
|
| 155 |
+
}
|
| 156 |
+
</style>
|
| 157 |
+
</head>
|
| 158 |
+
<body>
|
| 159 |
+
<h1>Jewish Passover (Pesach) Coloring Pages for a """ + str(age) + """-year-old</h1>
|
| 160 |
+
<div class="no-print">
|
| 161 |
+
<p>Click on an image to print it individually, or use the button below to print all pages:</p>
|
| 162 |
+
<button class="print-button" onclick="window.print()">Print All Coloring Pages</button>
|
| 163 |
+
</div>
|
| 164 |
+
<div class="gallery">
|
| 165 |
+
"""
|
| 166 |
+
|
| 167 |
+
# Add each image to the HTML
|
| 168 |
+
for _, filename, description in image_paths:
|
| 169 |
+
html_content += f"""
|
| 170 |
+
<div class="coloring-page">
|
| 171 |
+
<img src="coloring_images/{filename}" alt="{description}">
|
| 172 |
+
<h3>{description}</h3>
|
| 173 |
+
<button class="print-button no-print" onclick="printSingle(this.parentNode)">Print This Page</button>
|
| 174 |
</div>
|
| 175 |
"""
|
| 176 |
|
| 177 |
+
# Add the closing HTML
|
| 178 |
+
html_content += """
|
| 179 |
+
</div>
|
| 180 |
+
<script>
|
| 181 |
+
function printSingle(element) {
|
| 182 |
+
const content = element.cloneNode(true);
|
| 183 |
+
const buttonsToRemove = content.querySelectorAll('.no-print');
|
| 184 |
+
buttonsToRemove.forEach(btn => btn.remove());
|
| 185 |
+
|
| 186 |
+
const printWindow = window.open('', '_blank');
|
| 187 |
+
printWindow.document.write(`
|
| 188 |
+
<!DOCTYPE html>
|
| 189 |
+
<html>
|
| 190 |
+
<head>
|
| 191 |
+
<title>Print Coloring Page</title>
|
| 192 |
+
<style>
|
| 193 |
+
body { margin: 0; display: flex; justify-content: center; }
|
| 194 |
+
.coloring-page {
|
| 195 |
+
max-width: 100%;
|
| 196 |
+
text-align: center;
|
| 197 |
+
padding: 20px;
|
| 198 |
+
}
|
| 199 |
+
img { max-width: 100%; height: auto; }
|
| 200 |
+
h3 { font-family: Arial, sans-serif; }
|
| 201 |
+
</style>
|
| 202 |
+
</head>
|
| 203 |
+
<body>
|
| 204 |
+
${content.outerHTML}
|
| 205 |
+
</body>
|
| 206 |
+
</html>
|
| 207 |
+
`);
|
| 208 |
+
printWindow.document.close();
|
| 209 |
+
setTimeout(() => {
|
| 210 |
+
printWindow.print();
|
| 211 |
+
}, 500);
|
| 212 |
+
}
|
| 213 |
+
</script>
|
| 214 |
+
</body>
|
| 215 |
+
</html>
|
| 216 |
+
"""
|
| 217 |
+
|
| 218 |
+
# Write the HTML to a file
|
| 219 |
+
with open(HTML_VIEWER_PATH, 'w') as f:
|
| 220 |
+
f.write(html_content)
|
| 221 |
+
|
| 222 |
# Create formatted response
|
| 223 |
response = f"## Jewish Passover (Pesach) Coloring Pages for a {age}-year-old:\n\n"
|
| 224 |
response += "\n".join([f"- {desc}" for desc in selected_pages])
|
| 225 |
|
| 226 |
+
# Add information about the HTML viewer
|
| 227 |
+
response += f"\n\n**Coloring pages have been generated!**\n\n"
|
| 228 |
+
response += f"To view and print the coloring pages, please open this file in your web browser:\n\n"
|
| 229 |
+
response += f"{HTML_VIEWER_PATH}\n\n"
|
| 230 |
+
response += f"The individual image files are also available in the 'coloring_images' folder:\n"
|
| 231 |
+
response += "\n".join([f"- {os.path.basename(path)}" for path, _, _ in image_paths])
|
| 232 |
|
| 233 |
+
return response
|
|
|
|
|
|
|
| 234 |
|
| 235 |
# Setup agent
|
| 236 |
final_answer = FinalAnswerTool()
|
|
|
|
| 303 |
print(f"Generating coloring pages for age {age}...")
|
| 304 |
result = generate_passover_coloring_pages(age)
|
| 305 |
|
| 306 |
+
print("\n" + result)
|
| 307 |
+
print(f"\nTo view the coloring pages, open this file in your web browser: {HTML_VIEWER_PATH}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
except ValueError:
|
| 310 |
print("Please enter a valid number for the child's age.")
|
|
|
|
| 314 |
# Launch Gradio UI if available, otherwise use simple interface
|
| 315 |
try:
|
| 316 |
from Gradio_UI import GradioUI
|
|
|
|
| 317 |
|
| 318 |
class SimpleGradioUI(GradioUI):
|
| 319 |
def launch(self, **kwargs):
|
| 320 |
+
import gradio as gr
|
| 321 |
+
|
| 322 |
with gr.Blocks() as demo:
|
| 323 |
gr.Markdown("# Jewish Passover (Pesach) Coloring Pages Generator")
|
| 324 |
|
|
|
|
| 328 |
submit_btn = gr.Button("Generate Coloring Pages")
|
| 329 |
|
| 330 |
with gr.Row():
|
| 331 |
+
output_text = gr.Markdown()
|
| 332 |
|
| 333 |
# Simple function to process input without complex logic
|
| 334 |
def process_input(age):
|
|
|
|
| 339 |
# Call the tool directly
|
| 340 |
result = generate_passover_coloring_pages(int(age))
|
| 341 |
|
| 342 |
+
# Add a clearer message
|
| 343 |
+
result += f"\n\n**Important: Gradio cannot display these images directly.**\n\n"
|
| 344 |
+
result += f"Please open the HTML file mentioned above in your web browser to view and print the coloring pages."
|
| 345 |
+
|
| 346 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
except Exception as e:
|
| 348 |
+
return f"Error: {str(e)}"
|
| 349 |
|
| 350 |
submit_btn.click(process_input, inputs=input_box, outputs=output_text)
|
| 351 |
|