RozzaCreat commited on
Commit
7d3bdd8
·
verified ·
1 Parent(s): 645e50d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -127
app.py CHANGED
@@ -6,54 +6,13 @@ import yaml
6
  import re
7
  import tempfile
8
  import os
9
- import random
10
- import io
11
- import base64
12
- from PIL import Image, ImageDraw, ImageFont
13
  from tools.final_answer import FinalAnswerTool
14
  from Gradio_UI import GradioUI
15
 
16
- # Alternative image generation function that doesn't rely on the paid API
17
- def generate_simple_image(prompt, width=800, height=800):
18
- """
19
- Generate a simple placeholder image based on the prompt.
20
- This is a fallback when the primary image generation service fails.
21
- """
22
- try:
23
- # Create a blank white image
24
- image = Image.new('RGB', (width, height), color='white')
25
- draw = ImageDraw.Draw(image)
26
-
27
- # Try to load a font, fall back to default if not available
28
- try:
29
- font = ImageFont.truetype("arial.ttf", 20)
30
- except IOError:
31
- font = ImageFont.load_default()
32
-
33
- # Draw a border
34
- draw.rectangle([(10, 10), (width-10, height-10)], outline='black', width=2)
35
-
36
- # Add text from the prompt (first 50 characters)
37
- short_prompt = prompt[:50] + "..." if len(prompt) > 50 else prompt
38
- text_position = (width//2, height//2)
39
-
40
- # Draw centered text
41
- draw.text(text_position, short_prompt, fill='black', font=font, anchor='mm')
42
-
43
- # Add a message that this is a placeholder
44
- draw.text((width//2, height-40), "Placeholder - Image generation unavailable",
45
- fill='red', font=font, anchor='mm')
46
-
47
- return image
48
- except Exception as e:
49
- # If even this fails, create an absolute minimum fallback
50
- image = Image.new('RGB', (400, 400), color='white')
51
- return image
52
-
53
  @tool
54
  def generate_passover_coloring_pages(age: int) -> str:
55
  """
56
- Generates Jewish Passover/Pesach-themed coloring pages based on a child's age.
57
  Args:
58
  age (int): The age of the child (1-12).
59
  Returns:
@@ -71,80 +30,47 @@ def generate_passover_coloring_pages(age: int) -> str:
71
 
72
  complexity = next(value for (low, high), value in complexity_levels.items() if low <= age <= high)
73
 
74
- # Enhanced prompts specifically for Jewish Passover/Pesach themes with anti-hallucination guidance
75
  passover_pages = {
76
  "simple": [
77
- "Matzah bread outline only for coloring page, simple flat black lines on white background, no humans, no hands",
78
- "Empty Seder plate outline with labeled sections for coloring, simple black lines on white, no people",
79
- "Afikoman illustration, folded matzah in napkin, simple black outline on white background",
80
- "Simple Kiddush cup outline drawing for coloring, plain black lines on white"
81
  ],
82
  "basic": [
83
- "Red Sea parting scene outline with simple waves and pathway, no detailed humans, black lines on white background for coloring",
84
- "Ten Plagues symbols in separate boxes (frog, locusts, etc.), simple black outlines for coloring, no human figures",
85
- "Four Questions illustrated as symbols with Hebrew letters, simple black outlines for coloring",
86
- "Simple Afikoman maze with straight lines, black outlines on white background"
87
  ],
88
  "intermediate": [
89
- "Seder table with symbolic foods outlined in black and white, overhead view only, no people",
90
- "Hebrew letters spelling 'Pesach' (פסח) in block style for coloring, black outlines on white",
91
- "Stylized exodus scene with distant simplified figures crossing sea, black outline only, no detailed humans",
92
- "Four Sons from Haggadah represented by simple symbols (book, question mark, etc.), no human figures"
93
  ],
94
  "advanced": [
95
- "Haggadah page layout with decorative borders and Hebrew text outlines, black lines on white, no figures",
96
- "Tambourines and musical notes pattern for coloring, black outlines representing celebration, no human figures",
97
- "Hebrew calligraphy 'Next year in Jerusalem' (לשנה הבאה בירושלים) with decorative elements, black outline",
98
- "Symbolic Passover journey map with simple icons (pyramid, sea, mountain), black outline for coloring, no detailed humans"
99
  ]
100
  }
101
 
102
- # Added anti-hallucination qualifiers
103
- anti_hallucination_qualifiers = [
104
- "simple black and white outlines only",
105
- "no anatomical errors",
106
- "avoid human figures with details",
107
- "correct proportions",
108
- "coloring book style with clear lines",
109
- "no shading or gradients",
110
- "anatomically correct if any animals appear",
111
- "simplified representations",
112
- "no realistic humans",
113
- "coloring page suitable for printing"
114
- ]
115
-
116
  selected_pages = passover_pages[complexity]
117
- descriptions = [page.split(',')[0] if ',' in page else page for page in selected_pages]
118
 
119
- # Try multiple image generation approaches with fallbacks
120
  try:
121
- # First attempt: Try to use the original tool if available
122
- try:
123
- # Try to load the original image generation tool
124
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
125
-
126
- # Use more specific prompts for coloring pages with anti-hallucination emphasis
127
- enhanced_prompts = []
128
- for page in selected_pages:
129
- # Add random anti-hallucination qualifiers to each prompt
130
- qualifiers = ", ".join(anti_hallucination_qualifiers)
131
- enhanced_prompt = f"{page}, Jewish traditional Passover symbols only, {qualifiers}, black and white coloring book page"
132
- enhanced_prompts.append(enhanced_prompt)
133
-
134
- # Generate images with the tool
135
- images = [image_generation_tool(prompt) for prompt in enhanced_prompts]
136
 
137
- except Exception as e:
138
- # If the primary approach fails, try an alternative API
139
- try:
140
- # This is a placeholder for an alternative API call
141
- # In a real implementation, you'd replace this with another API
142
- # For now, we'll use our fallback function
143
- raise Exception("Using fallback image generation")
144
-
145
- except Exception as alt_error:
146
- # If both approaches fail, use our simple fallback image generator
147
- images = [generate_simple_image(prompt) for prompt in selected_pages]
148
 
149
  # Save images to temporary files
150
  image_paths = []
@@ -156,16 +82,16 @@ def generate_passover_coloring_pages(age: int) -> str:
156
  image_paths.append(temp_path)
157
 
158
  # Create a formatted response with descriptions
159
- response = f"## Jewish Passover (Pesach) Coloring Pages for a {age}-year-old:\n\n"
160
- response += "\n".join([f"- {desc}" for desc in descriptions])
161
 
162
  # Return the response with image paths
163
  return f"{response}\n\nImages are saved and ready for display."
164
  except Exception as e:
165
- # If all image generation methods fail, return text description
166
- response = f"## Jewish Passover (Pesach) Coloring Pages for a {age}-year-old:\n\n"
167
- response += "\n".join([f"- {desc}" for desc in descriptions])
168
- return f"{response}\n\nWe couldn't generate images at this time, but here are descriptions of coloring pages you can create:\n\n" + "\n".join([f"- {desc}" for desc in descriptions])
169
 
170
  # Custom UI wrapper to handle image display
171
  class CustomGradioUI(GradioUI):
@@ -179,8 +105,7 @@ class CustomGradioUI(GradioUI):
179
  with gr.Blocks() as demo:
180
  with gr.Row():
181
  with gr.Column(scale=1):
182
- gr.Markdown("# Jewish Passover (Pesach) Coloring Pages Generator")
183
- gr.Markdown("Generate traditional Jewish Passover coloring pages for children")
184
 
185
  with gr.Row():
186
  with gr.Column(scale=1):
@@ -192,7 +117,7 @@ class CustomGradioUI(GradioUI):
192
  output_text = gr.Markdown(label="Generated Pages")
193
 
194
  with gr.Row():
195
- gallery = gr.Gallery(label="Jewish Passover Coloring Pages", show_label=True)
196
 
197
  def process_input(input_text):
198
  try:
@@ -213,17 +138,6 @@ class CustomGradioUI(GradioUI):
213
  # Display the most recent images (last 4 if there are more)
214
  image_paths = sorted(image_paths, key=os.path.getmtime, reverse=True)[:4]
215
 
216
- # If no images were found, generate placeholder images
217
- if not image_paths:
218
- placeholder_paths = []
219
- for i, desc in enumerate(result.split('\n')):
220
- if desc.startswith('- '):
221
- img = generate_simple_image(desc[2:])
222
- path = os.path.join(tempfile.gettempdir(), f"placeholder_{i}.png")
223
- img.save(path)
224
- placeholder_paths.append(path)
225
- image_paths = placeholder_paths[:4]
226
-
227
  # Return the result and image paths
228
  return result, image_paths
229
  except ValueError:
@@ -238,16 +152,13 @@ class CustomGradioUI(GradioUI):
238
 
239
  # Modified prompt templates to handle simple age input
240
  custom_prompt_templates = {
241
- "react-system": """You are a helpful agent that helps children learn about Jewish Passover/Pesach through coloring pages.
242
  You have access to the following tools:
243
 
244
  {tools_string}
245
 
246
- When a user enters a number between 1 and 12, interpret it as the age of a child and generate Jewish Passover coloring pages suitable for that age using the generate_passover_coloring_pages tool.
247
- If you're asked to generate coloring pages or if the input is just a number, assume the user wants Jewish Passover coloring pages for a child of that age.
248
-
249
- Important: Only generate authentic Jewish Passover (Pesach) symbols and imagery - including matzah, seder plate, haggadah, afikoman, four cups of wine, the exodus story, etc.
250
- Do NOT include any Christian symbols or imagery like crosses, Jesus, or other non-Jewish religious elements.
251
 
252
  To use a tool, output an action in this format:
253
  ```py
 
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
  """
15
+ Generates Passover-themed coloring pages based on a child's age.
16
  Args:
17
  age (int): The age of the child (1-12).
18
  Returns:
 
30
 
31
  complexity = next(value for (low, high), value in complexity_levels.items() if low <= age <= high)
32
 
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 = []
 
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):
 
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):
 
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:
 
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:
 
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