Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,39 +6,9 @@ import yaml
|
|
| 6 |
import re
|
| 7 |
import tempfile
|
| 8 |
import os
|
| 9 |
-
import base64
|
| 10 |
-
from PIL import Image, ImageDraw, ImageFont, ImageFile
|
| 11 |
-
ImageFile.LOAD_TRUNCATED_IMAGES = True # Handle truncated images
|
| 12 |
from tools.final_answer import FinalAnswerTool
|
| 13 |
from Gradio_UI import GradioUI
|
| 14 |
|
| 15 |
-
# Function to generate images - separate from the tool to avoid complex return types
|
| 16 |
-
def _generate_coloring_images(age, prompts):
|
| 17 |
-
"""Helper function to generate images without complex return types."""
|
| 18 |
-
try:
|
| 19 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 20 |
-
images = [image_generation_tool(prompt) for prompt in prompts]
|
| 21 |
-
return images
|
| 22 |
-
except Exception as e:
|
| 23 |
-
print(f"Error generating images: {str(e)}")
|
| 24 |
-
return []
|
| 25 |
-
|
| 26 |
-
# Function to create placeholder images if generation fails
|
| 27 |
-
def _create_placeholder_image(text, age):
|
| 28 |
-
img = Image.new('RGB', (800, 600), color='white')
|
| 29 |
-
draw = ImageDraw.Draw(img)
|
| 30 |
-
draw.rectangle([(10, 10), (790, 590)], outline='black', width=2)
|
| 31 |
-
|
| 32 |
-
# Simple text positioning
|
| 33 |
-
text_x, text_y = 400, 300
|
| 34 |
-
age_text_x, age_text_y = 400, 400
|
| 35 |
-
|
| 36 |
-
# Write text
|
| 37 |
-
draw.text((text_x, text_y), text, fill='black')
|
| 38 |
-
draw.text((age_text_x, age_text_y), f"For age: {age}", fill='black')
|
| 39 |
-
|
| 40 |
-
return img
|
| 41 |
-
|
| 42 |
@tool
|
| 43 |
def generate_passover_coloring_pages(age: int) -> str:
|
| 44 |
"""
|
|
@@ -63,169 +33,144 @@ def generate_passover_coloring_pages(age: int) -> str:
|
|
| 63 |
# Enhanced prompts for better coloring pages
|
| 64 |
passover_pages = {
|
| 65 |
"simple": [
|
| 66 |
-
"Line drawing of a matzah for a simple coloring page, thick black outlines, white background",
|
| 67 |
-
"Simple black and white outline of a Seder plate with clear divisions, coloring book style",
|
| 68 |
-
"Black and white line art of baby Moses in a basket for a child's coloring book",
|
| 69 |
-
"Simple black and white outline of a frog (plague) for children's coloring book"
|
| 70 |
],
|
| 71 |
"basic": [
|
| 72 |
-
"Black and white outline of the Parting of the Red Sea scene for a coloring book",
|
| 73 |
-
"Coloring book page with Ten Plagues illustrations, black outline on white background",
|
| 74 |
"Black and white outline illustrations of the Four Questions for a Passover coloring book",
|
| 75 |
-
"Afikoman hunt maze in black and white outline for a coloring book"
|
| 76 |
],
|
| 77 |
"intermediate": [
|
| 78 |
-
"Detailed black and white outline of a Seder table for a coloring book",
|
| 79 |
-
"Black and white outline of Exodus story scenes for coloring book",
|
| 80 |
"Hebrew letter decorations for 'Pesach' in black and white outline style for coloring book",
|
| 81 |
"Black and white outline of Egyptian pyramids with Israelites for a coloring book"
|
| 82 |
],
|
| 83 |
"advanced": [
|
| 84 |
-
"Intricate black and white line art of Haggadah illustrations for a coloring book",
|
| 85 |
"Detailed symbolism scene in black and white outline for an advanced coloring book",
|
| 86 |
"Combined Passover story panels in black and white line art for coloring book",
|
| 87 |
-
"Hebrew calligraphy art in black and white outline for a coloring book"
|
| 88 |
]
|
| 89 |
}
|
| 90 |
|
| 91 |
selected_pages = passover_pages[complexity]
|
| 92 |
descriptions = [f"- {page.split(',')[0]}" for page in selected_pages]
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
IMAGES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "coloring_images")
|
| 96 |
-
os.makedirs(IMAGES_DIR, exist_ok=True)
|
| 97 |
-
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 98 |
-
|
| 99 |
-
# Generate enhanced prompts for coloring pages
|
| 100 |
-
enhanced_prompts = []
|
| 101 |
-
for page in selected_pages:
|
| 102 |
-
enhanced_prompts.append(f"{page}, black and white line drawing, coloring book style, thick lines, no shading")
|
| 103 |
-
|
| 104 |
-
# Generate images outside the tool to avoid complex return types
|
| 105 |
-
# Instead, save them to disk and return paths as strings
|
| 106 |
-
image_paths = []
|
| 107 |
-
|
| 108 |
try:
|
| 109 |
-
|
| 110 |
-
images = _generate_coloring_images(age, enhanced_prompts)
|
| 111 |
|
| 112 |
-
#
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
img.save(img_path)
|
| 117 |
-
image_paths.append(img_path)
|
| 118 |
-
else:
|
| 119 |
-
# Create placeholder if image generation failed
|
| 120 |
-
placeholder = _create_placeholder_image(desc, age)
|
| 121 |
-
img_path = os.path.join(IMAGES_DIR, f"passover_{age}_{i}_{timestamp}_placeholder.png")
|
| 122 |
-
placeholder.save(img_path)
|
| 123 |
-
image_paths.append(img_path)
|
| 124 |
-
except Exception as e:
|
| 125 |
-
print(f"Error processing images: {str(e)}")
|
| 126 |
-
# Create placeholder images if generation fails
|
| 127 |
-
for i, desc in enumerate(selected_pages):
|
| 128 |
-
placeholder = _create_placeholder_image(desc, age)
|
| 129 |
-
img_path = os.path.join(IMAGES_DIR, f"passover_{age}_{i}_{timestamp}_placeholder.png")
|
| 130 |
-
placeholder.save(img_path)
|
| 131 |
-
image_paths.append(img_path)
|
| 132 |
-
|
| 133 |
-
# Create HTML for viewing/printing
|
| 134 |
-
html_path = os.path.join(IMAGES_DIR, f"passover_coloring_pages_{age}_{timestamp}.html")
|
| 135 |
-
with open(html_path, 'w') as f:
|
| 136 |
-
f.write(f"""<!DOCTYPE html>
|
| 137 |
-
<html>
|
| 138 |
-
<head>
|
| 139 |
-
<title>Passover Coloring Pages - Age {age}</title>
|
| 140 |
-
<style>
|
| 141 |
-
body {{ font-family: Arial; max-width: 900px; margin: 0 auto; padding: 20px; }}
|
| 142 |
-
h1, h2 {{ color: #2c3e50; text-align: center; }}
|
| 143 |
-
.controls {{ background-color: #f8f9fa; padding: 15px; margin-bottom: 20px; text-align: center; }}
|
| 144 |
-
.page {{ page-break-after: always; margin-bottom: 30px; }}
|
| 145 |
-
img {{ max-width: 100%; margin: 0 auto; border: 1px solid #ddd; }}
|
| 146 |
-
.page-title {{ text-align: center; margin-top: 10px; font-weight: bold; }}
|
| 147 |
-
.print-button {{ background-color: #3498db; color: white; border: none; padding: 10px 20px;
|
| 148 |
-
border-radius: 4px; cursor: pointer; font-size: 16px; }}
|
| 149 |
-
@media print {{ .controls {{ display: none; }} .page {{ margin: 0; }} }}
|
| 150 |
-
</style>
|
| 151 |
-
</head>
|
| 152 |
-
<body>
|
| 153 |
-
<div class="controls">
|
| 154 |
-
<h1>Passover Coloring Pages</h1>
|
| 155 |
-
<h2>For Ages {age} Years</h2>
|
| 156 |
-
<p>Click the button below to print all coloring pages.</p>
|
| 157 |
-
<button class="print-button" onclick="window.print()">Print All Pages</button>
|
| 158 |
-
</div>
|
| 159 |
-
""")
|
| 160 |
|
| 161 |
-
|
| 162 |
-
for i, (img_path, desc) in enumerate(zip(image_paths, descriptions)):
|
| 163 |
-
img_filename = os.path.basename(img_path)
|
| 164 |
-
f.write(f"""
|
| 165 |
-
<div class="page">
|
| 166 |
-
<img src="{img_filename}" alt="Coloring Page {i+1}">
|
| 167 |
-
<div class="page-title">{desc}</div>
|
| 168 |
-
</div>
|
| 169 |
-
""")
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
-
# Custom UI wrapper
|
| 188 |
-
class
|
|
|
|
|
|
|
|
|
|
| 189 |
def launch(self, **kwargs):
|
| 190 |
import gradio as gr
|
| 191 |
|
| 192 |
-
# Create a
|
| 193 |
with gr.Blocks() as demo:
|
| 194 |
with gr.Row():
|
| 195 |
with gr.Column(scale=1):
|
| 196 |
gr.Markdown("# Passover Coloring Pages Generator")
|
| 197 |
-
gr.Markdown("Generate traditional Passover coloring pages for children")
|
| 198 |
|
| 199 |
with gr.Row():
|
| 200 |
with gr.Column(scale=1):
|
| 201 |
-
input_box = gr.
|
| 202 |
submit_btn = gr.Button("Generate Coloring Pages")
|
| 203 |
|
| 204 |
with gr.Row():
|
| 205 |
with gr.Column(scale=1):
|
| 206 |
-
output_text = gr.Markdown()
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
if not age or not (1 <= age <= 12):
|
| 211 |
-
return "Please enter a valid age between 1 and 12."
|
| 212 |
|
|
|
|
| 213 |
try:
|
| 214 |
-
#
|
| 215 |
-
|
|
|
|
|
|
|
| 216 |
|
| 217 |
-
#
|
| 218 |
-
result
|
| 219 |
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
except Exception as e:
|
| 222 |
-
return f"Error: {str(e)}"
|
| 223 |
|
| 224 |
-
submit_btn.click(process_input, inputs=input_box, outputs=output_text)
|
| 225 |
|
| 226 |
# Launch the demo
|
| 227 |
demo.launch(**kwargs)
|
| 228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
# Setup agent
|
| 230 |
final_answer = FinalAnswerTool()
|
| 231 |
|
|
@@ -239,8 +184,10 @@ model = HfApiModel(
|
|
| 239 |
try:
|
| 240 |
with open("prompts.yaml", 'r') as stream:
|
| 241 |
prompt_templates = yaml.safe_load(stream)
|
|
|
|
| 242 |
prompt_templates.update(custom_prompt_templates)
|
| 243 |
except:
|
|
|
|
| 244 |
prompt_templates = custom_prompt_templates
|
| 245 |
|
| 246 |
agent = CodeAgent(
|
|
@@ -251,5 +198,5 @@ agent = CodeAgent(
|
|
| 251 |
prompt_templates=prompt_templates
|
| 252 |
)
|
| 253 |
|
| 254 |
-
# Launch
|
| 255 |
-
|
|
|
|
| 6 |
import re
|
| 7 |
import tempfile
|
| 8 |
import os
|
|
|
|
|
|
|
|
|
|
| 9 |
from tools.final_answer import FinalAnswerTool
|
| 10 |
from Gradio_UI import GradioUI
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@tool
|
| 13 |
def generate_passover_coloring_pages(age: int) -> str:
|
| 14 |
"""
|
|
|
|
| 33 |
# Enhanced prompts for better coloring pages
|
| 34 |
passover_pages = {
|
| 35 |
"simple": [
|
| 36 |
+
"Line drawing of a matzah for a simple coloring page, thick black outlines, white background, perfect for a child's coloring book",
|
| 37 |
+
"Simple black and white outline of a Seder plate with clear divisions, coloring book style, thick lines",
|
| 38 |
+
"Black and white line art of baby Moses in a basket for a child's coloring book, simple outlines",
|
| 39 |
+
"Simple black and white outline of a frog (plague) for children's coloring book, thick lines, no shading"
|
| 40 |
],
|
| 41 |
"basic": [
|
| 42 |
+
"Black and white outline of the Parting of the Red Sea scene for a coloring book, clear distinct lines",
|
| 43 |
+
"Coloring book page with Ten Plagues illustrations, black outline on white background, no shading",
|
| 44 |
"Black and white outline illustrations of the Four Questions for a Passover coloring book",
|
| 45 |
+
"Afikoman hunt maze in black and white outline for a coloring book, thick lines"
|
| 46 |
],
|
| 47 |
"intermediate": [
|
| 48 |
+
"Detailed black and white outline of a Seder table for a coloring book, clear distinct elements",
|
| 49 |
+
"Black and white outline of Exodus story scenes for coloring book, clear lines",
|
| 50 |
"Hebrew letter decorations for 'Pesach' in black and white outline style for coloring book",
|
| 51 |
"Black and white outline of Egyptian pyramids with Israelites for a coloring book"
|
| 52 |
],
|
| 53 |
"advanced": [
|
| 54 |
+
"Intricate black and white line art of Haggadah illustrations for a coloring book, detailed outlines",
|
| 55 |
"Detailed symbolism scene in black and white outline for an advanced coloring book",
|
| 56 |
"Combined Passover story panels in black and white line art for coloring book",
|
| 57 |
+
"Hebrew calligraphy art in black and white outline for a coloring book, intricate details"
|
| 58 |
]
|
| 59 |
}
|
| 60 |
|
| 61 |
selected_pages = passover_pages[complexity]
|
| 62 |
descriptions = [f"- {page.split(',')[0]}" for page in selected_pages]
|
| 63 |
|
| 64 |
+
# Try to generate images with improved prompts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
try:
|
| 66 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
|
|
|
| 67 |
|
| 68 |
+
# Use more specific prompts for coloring pages
|
| 69 |
+
enhanced_prompts = []
|
| 70 |
+
for page in selected_pages:
|
| 71 |
+
enhanced_prompts.append(f"{page}, suitable for printing as a coloring page")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
images = [image_generation_tool(prompt) for prompt in enhanced_prompts]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# Save images to temporary files
|
| 76 |
+
image_paths = []
|
| 77 |
+
for i, img in enumerate(images):
|
| 78 |
+
# Create a temporary file with a .png extension
|
| 79 |
+
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp_file:
|
| 80 |
+
temp_path = temp_file.name
|
| 81 |
+
img.save(temp_path)
|
| 82 |
+
image_paths.append(temp_path)
|
| 83 |
+
|
| 84 |
+
# Create a formatted response with descriptions
|
| 85 |
+
response = f"## Passover Coloring Pages for a {age}-year-old:\n\n"
|
| 86 |
+
response += "\n".join(descriptions)
|
| 87 |
+
|
| 88 |
+
# Return the response with image paths
|
| 89 |
+
return f"{response}\n\nImages are saved and ready for display."
|
| 90 |
+
except Exception as e:
|
| 91 |
+
# If image generation fails, return text description
|
| 92 |
+
response = f"## Passover Coloring Pages for a {age}-year-old:\n\n"
|
| 93 |
+
response += "\n".join(descriptions)
|
| 94 |
+
return f"{response}\n\nError generating images: {str(e)}"
|
| 95 |
|
| 96 |
+
# Custom UI wrapper to handle image display
|
| 97 |
+
class CustomGradioUI(GradioUI):
|
| 98 |
+
def __init__(self, agent):
|
| 99 |
+
super().__init__(agent)
|
| 100 |
+
|
| 101 |
def launch(self, **kwargs):
|
| 102 |
import gradio as gr
|
| 103 |
|
| 104 |
+
# Create a custom interface that can display images
|
| 105 |
with gr.Blocks() as demo:
|
| 106 |
with gr.Row():
|
| 107 |
with gr.Column(scale=1):
|
| 108 |
gr.Markdown("# Passover Coloring Pages Generator")
|
|
|
|
| 109 |
|
| 110 |
with gr.Row():
|
| 111 |
with gr.Column(scale=1):
|
| 112 |
+
input_box = gr.Textbox(label="Enter child's age (1-12)")
|
| 113 |
submit_btn = gr.Button("Generate Coloring Pages")
|
| 114 |
|
| 115 |
with gr.Row():
|
| 116 |
with gr.Column(scale=1):
|
| 117 |
+
output_text = gr.Markdown(label="Generated Pages")
|
| 118 |
+
|
| 119 |
+
with gr.Row():
|
| 120 |
+
gallery = gr.Gallery(label="Coloring Pages", show_label=True)
|
|
|
|
|
|
|
| 121 |
|
| 122 |
+
def process_input(input_text):
|
| 123 |
try:
|
| 124 |
+
# Try to convert the input to an integer
|
| 125 |
+
age = int(input_text.strip())
|
| 126 |
+
if not (1 <= age <= 12):
|
| 127 |
+
return "Please enter a valid age between 1 and 12.", []
|
| 128 |
|
| 129 |
+
# Generate the coloring pages
|
| 130 |
+
result = generate_passover_coloring_pages(age)
|
| 131 |
|
| 132 |
+
# Get the image paths from the temporary files
|
| 133 |
+
image_paths = []
|
| 134 |
+
for file in os.listdir(tempfile.gettempdir()):
|
| 135 |
+
if file.endswith('.png'):
|
| 136 |
+
image_paths.append(os.path.join(tempfile.gettempdir(), file))
|
| 137 |
+
|
| 138 |
+
# Display the most recent images (last 4 if there are more)
|
| 139 |
+
image_paths = sorted(image_paths, key=os.path.getmtime, reverse=True)[:4]
|
| 140 |
+
|
| 141 |
+
# Return the result and image paths
|
| 142 |
+
return result, image_paths
|
| 143 |
+
except ValueError:
|
| 144 |
+
return "Please enter a valid number for the child's age.", []
|
| 145 |
except Exception as e:
|
| 146 |
+
return f"Error: {str(e)}", []
|
| 147 |
|
| 148 |
+
submit_btn.click(process_input, inputs=input_box, outputs=[output_text, gallery])
|
| 149 |
|
| 150 |
# Launch the demo
|
| 151 |
demo.launch(**kwargs)
|
| 152 |
|
| 153 |
+
# Modified prompt templates to handle simple age input
|
| 154 |
+
custom_prompt_templates = {
|
| 155 |
+
"react-system": """You are an helpful agent that helps children learn about Passover through coloring pages.
|
| 156 |
+
You have access to the following tools:
|
| 157 |
+
|
| 158 |
+
{tools_string}
|
| 159 |
+
|
| 160 |
+
When a user enters a number between 1 and 12, interpret it as the age of a child and generate Passover coloring pages suitable for that age using the generate_passover_coloring_pages tool.
|
| 161 |
+
If you're asked to generate coloring pages or if the input is just a number, assume the user wants Passover coloring pages for a child of that age.
|
| 162 |
+
|
| 163 |
+
To use a tool, output an action in this format:
|
| 164 |
+
```py
|
| 165 |
+
action = tool_name(arg1=value1, arg2=value2, ...)
|
| 166 |
+
```
|
| 167 |
+
|
| 168 |
+
Always use Python syntax for calling tools. After using a tool, analyze the result to determine what to do next. Use final_answer to return your answer when complete.
|
| 169 |
+
""",
|
| 170 |
+
"react-human": "{input_string}",
|
| 171 |
+
"react-assistant": "{agent_response}",
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
# Setup agent
|
| 175 |
final_answer = FinalAnswerTool()
|
| 176 |
|
|
|
|
| 184 |
try:
|
| 185 |
with open("prompts.yaml", 'r') as stream:
|
| 186 |
prompt_templates = yaml.safe_load(stream)
|
| 187 |
+
# Merge the templates, giving priority to our custom ones
|
| 188 |
prompt_templates.update(custom_prompt_templates)
|
| 189 |
except:
|
| 190 |
+
# If file doesn't exist or has issues, use our custom templates
|
| 191 |
prompt_templates = custom_prompt_templates
|
| 192 |
|
| 193 |
agent = CodeAgent(
|
|
|
|
| 198 |
prompt_templates=prompt_templates
|
| 199 |
)
|
| 200 |
|
| 201 |
+
# Launch custom Gradio UI
|
| 202 |
+
CustomGradioUI(agent).launch()
|