Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
import re
|
|
|
|
|
|
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
@@ -70,28 +72,83 @@ def generate_passover_coloring_pages(age: int) -> str:
|
|
| 70 |
|
| 71 |
images = [image_generation_tool(prompt) for prompt in enhanced_prompts]
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
# Create a formatted response with descriptions
|
| 74 |
response = f"## Passover Coloring Pages for a {age}-year-old:\n\n"
|
| 75 |
response += "\n".join(descriptions)
|
| 76 |
|
| 77 |
-
# Return
|
| 78 |
-
return
|
| 79 |
except Exception as e:
|
| 80 |
# If image generation fails, return text description
|
| 81 |
response = f"## Passover Coloring Pages for a {age}-year-old:\n\n"
|
| 82 |
response += "\n".join(descriptions)
|
| 83 |
-
return response
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
class
|
| 87 |
-
def
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
#
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# Modified prompt templates to handle simple age input
|
| 97 |
custom_prompt_templates = {
|
|
@@ -115,7 +172,7 @@ Always use Python syntax for calling tools. After using a tool, analyze the resu
|
|
| 115 |
}
|
| 116 |
|
| 117 |
# Setup agent
|
| 118 |
-
final_answer =
|
| 119 |
|
| 120 |
model = HfApiModel(
|
| 121 |
max_tokens=2096,
|
|
@@ -141,5 +198,5 @@ agent = CodeAgent(
|
|
| 141 |
prompt_templates=prompt_templates
|
| 142 |
)
|
| 143 |
|
| 144 |
-
# Launch Gradio UI
|
| 145 |
-
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
import re
|
| 7 |
+
import tempfile
|
| 8 |
+
import os
|
| 9 |
from tools.final_answer import FinalAnswerTool
|
| 10 |
from Gradio_UI import GradioUI
|
| 11 |
|
|
|
|
| 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 = {
|
|
|
|
| 172 |
}
|
| 173 |
|
| 174 |
# Setup agent
|
| 175 |
+
final_answer = FinalAnswerTool()
|
| 176 |
|
| 177 |
model = HfApiModel(
|
| 178 |
max_tokens=2096,
|
|
|
|
| 198 |
prompt_templates=prompt_templates
|
| 199 |
)
|
| 200 |
|
| 201 |
+
# Launch custom Gradio UI
|
| 202 |
+
CustomGradioUI(agent).launch()
|