RozzaCreat commited on
Commit
02c0069
·
verified ·
1 Parent(s): d1a1972

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -14
app.py CHANGED
@@ -2,6 +2,8 @@ from smolagents import CodeAgent, HfApiModel, tool
2
  import os
3
  import yaml
4
  import time
 
 
5
  from tools.final_answer import FinalAnswerTool
6
  from PIL import Image, ImageDraw, ImageFont
7
 
@@ -64,6 +66,8 @@ def generate_passover_coloring_pages(age: int) -> str:
64
 
65
  # Create placeholder images with descriptions
66
  image_paths = []
 
 
67
  for i, description in enumerate(selected_pages):
68
  # Create a simple placeholder image with text
69
  img = Image.new('RGB', (800, 600), color='white')
@@ -77,17 +81,34 @@ def generate_passover_coloring_pages(age: int) -> str:
77
  draw.text((400, 350), "Jewish Passover Coloring Page", fill='black', anchor='mm')
78
  draw.text((400, 400), f"For age: {age}", fill='black', anchor='mm')
79
 
80
- # Save the image
81
  timestamp = int(time.time())
82
  img_path = os.path.join(IMAGES_DIR, f"passover_{age}_{i}_{timestamp}.png")
83
  img.save(img_path)
84
  image_paths.append(img_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  # Create formatted response
87
  response = f"## Jewish Passover (Pesach) Coloring Pages for a {age}-year-old:\n\n"
88
  response += "\n".join([f"- {desc}" for desc in selected_pages])
89
 
90
- # Return paths in a special format for the UI to find
 
 
 
91
  image_paths_str = "||".join(image_paths)
92
  return f"{response}\n\nIMAGE_PATHS:{image_paths_str}"
93
 
@@ -162,8 +183,10 @@ def run_with_simple_interface():
162
  print(f"Generating coloring pages for age {age}...")
163
  result = generate_passover_coloring_pages(age)
164
 
165
- # Remove paths for display
166
- if "IMAGE_PATHS:" in result:
 
 
167
  display_result = result.split("\n\nIMAGE_PATHS:")[0]
168
  else:
169
  display_result = result
@@ -190,11 +213,10 @@ def run_with_simple_interface():
190
  # Launch Gradio UI if available, otherwise use simple interface
191
  try:
192
  from Gradio_UI import GradioUI
 
193
 
194
  class SimpleGradioUI(GradioUI):
195
  def launch(self, **kwargs):
196
- import gradio as gr
197
-
198
  with gr.Blocks() as demo:
199
  gr.Markdown("# Jewish Passover (Pesach) Coloring Pages Generator")
200
 
@@ -204,7 +226,7 @@ try:
204
  submit_btn = gr.Button("Generate Coloring Pages")
205
 
206
  with gr.Row():
207
- output_text = gr.Markdown()
208
 
209
  # Simple function to process input without complex logic
210
  def process_input(age):
@@ -215,14 +237,19 @@ try:
215
  # Call the tool directly
216
  result = generate_passover_coloring_pages(int(age))
217
 
218
- # Remove the image paths for display
219
- if "IMAGE_PATHS:" in result:
220
- result = result.split("\n\nIMAGE_PATHS:")[0]
221
-
222
- result += "\n\n*Coloring pages have been generated. Look in the 'coloring_images' folder for the files.*"
223
- return result
 
 
 
 
 
224
  except Exception as e:
225
- return f"Error: {str(e)}"
226
 
227
  submit_btn.click(process_input, inputs=input_box, outputs=output_text)
228
 
 
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
 
 
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
  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 to disk
85
  timestamp = int(time.time())
86
  img_path = os.path.join(IMAGES_DIR, f"passover_{age}_{i}_{timestamp}.png")
87
  img.save(img_path)
88
  image_paths.append(img_path)
89
+
90
+ # Also encode the image as base64 for embedding in HTML
91
+ buffered = BytesIO()
92
+ img.save(buffered, format="PNG")
93
+ img_str = base64.b64encode(buffered.getvalue()).decode()
94
+
95
+ # Add HTML for this image
96
+ images_html += f"""
97
+ <div style="margin: 20px; border: 1px solid #ccc; padding: 10px; text-align: center;">
98
+ <img src="data:image/png;base64,{img_str}" style="max-width: 100%; height: auto;" />
99
+ <p>{description}</p>
100
+ <a href="{img_path}" download="passover_coloring_page_{i+1}.png">Download</a>
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 HTML with embedded images
109
+ response += f"\n\n<div>{images_html}</div>"
110
+
111
+ # Also return paths for fallback
112
  image_paths_str = "||".join(image_paths)
113
  return f"{response}\n\nIMAGE_PATHS:{image_paths_str}"
114
 
 
183
  print(f"Generating coloring pages for age {age}...")
184
  result = generate_passover_coloring_pages(age)
185
 
186
+ # Remove HTML and paths for display
187
+ if "<div>" in result:
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
 
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
  submit_btn = gr.Button("Generate Coloring Pages")
227
 
228
  with gr.Row():
229
+ output_text = gr.HTML() # Using HTML instead of Markdown to support embedded images
230
 
231
  # Simple function to process input without complex logic
232
  def process_input(age):
 
237
  # Call the tool directly
238
  result = generate_passover_coloring_pages(int(age))
239
 
240
+ # Extract the HTML portion with the embedded images
241
+ if "<div>" in result:
242
+ html_part = result.split("\n\n<div>")[1]
243
+ html_part = html_part.split("\n\nIMAGE_PATHS:")[0]
244
+ html_result = f"<h2>Jewish Passover (Pesach) Coloring Pages for a {age}-year-old:</h2>"
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"<p>Error: {str(e)}</p>"
253
 
254
  submit_btn.click(process_input, inputs=input_box, outputs=output_text)
255