Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,8 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
-
import datetime
|
| 3 |
-
import requests
|
| 4 |
-
import pytz
|
| 5 |
-
import yaml
|
| 6 |
-
import re
|
| 7 |
import tempfile
|
| 8 |
import os
|
| 9 |
from tools.final_answer import FinalAnswerTool
|
| 10 |
-
|
| 11 |
|
| 12 |
@tool
|
| 13 |
def generate_passover_coloring_pages(age: int) -> str:
|
|
@@ -93,62 +88,53 @@ def generate_passover_coloring_pages(age: int) -> str:
|
|
| 93 |
response += "\n".join(descriptions)
|
| 94 |
return f"{response}\n\nError generating images: {str(e)}"
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
with gr.
|
| 106 |
-
with gr.
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 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 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 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 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
# Modified prompt templates to handle simple age input
|
| 154 |
custom_prompt_templates = {
|
|
@@ -171,32 +157,37 @@ Always use Python syntax for calling tools. After using a tool, analyze the resu
|
|
| 171 |
"react-assistant": "{agent_response}",
|
| 172 |
}
|
| 173 |
|
| 174 |
-
|
| 175 |
-
|
|
|
|
| 176 |
|
| 177 |
-
model = HfApiModel(
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
)
|
| 182 |
|
| 183 |
-
# Check if custom prompts should be used or merged with existing ones
|
| 184 |
-
try:
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
except:
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
agent = CodeAgent(
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import tempfile
|
| 3 |
import os
|
| 4 |
from tools.final_answer import FinalAnswerTool
|
| 5 |
+
import yaml
|
| 6 |
|
| 7 |
@tool
|
| 8 |
def generate_passover_coloring_pages(age: int) -> str:
|
|
|
|
| 88 |
response += "\n".join(descriptions)
|
| 89 |
return f"{response}\n\nError generating images: {str(e)}"
|
| 90 |
|
| 91 |
+
def create_gradio_interface():
|
| 92 |
+
import gradio as gr
|
| 93 |
+
|
| 94 |
+
# Create a custom interface that can display images
|
| 95 |
+
with gr.Blocks() as demo:
|
| 96 |
+
with gr.Row():
|
| 97 |
+
with gr.Column(scale=1):
|
| 98 |
+
gr.Markdown("# Passover Coloring Pages Generator")
|
| 99 |
+
|
| 100 |
+
with gr.Row():
|
| 101 |
+
with gr.Column(scale=1):
|
| 102 |
+
input_box = gr.Number(label="Enter child's age (1-12)", minimum=1, maximum=12, step=1)
|
| 103 |
+
submit_btn = gr.Button("Generate Coloring Pages")
|
| 104 |
+
|
| 105 |
+
with gr.Row():
|
| 106 |
+
with gr.Column(scale=1):
|
| 107 |
+
output_text = gr.Markdown(label="Generated Pages")
|
| 108 |
|
| 109 |
+
with gr.Row():
|
| 110 |
+
gallery = gr.Gallery(label="Coloring Pages")
|
| 111 |
+
|
| 112 |
+
def process_input(age):
|
| 113 |
+
try:
|
| 114 |
+
# Validate age
|
| 115 |
+
if age is None or not (1 <= age <= 12):
|
| 116 |
+
return "Please enter a valid age between 1 and 12.", []
|
| 117 |
|
| 118 |
+
# Generate the coloring pages
|
| 119 |
+
result = generate_passover_coloring_pages(int(age))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
# Get the image paths from the temporary files
|
| 122 |
+
image_paths = []
|
| 123 |
+
for file in os.listdir(tempfile.gettempdir()):
|
| 124 |
+
if file.endswith('.png'):
|
| 125 |
+
image_paths.append(os.path.join(tempfile.gettempdir(), file))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
# Display the most recent images (last 4 if there are more)
|
| 128 |
+
image_paths = sorted(image_paths, key=os.path.getmtime, reverse=True)[:4]
|
| 129 |
+
|
| 130 |
+
# Return the result and image paths
|
| 131 |
+
return result, image_paths
|
| 132 |
+
except Exception as e:
|
| 133 |
+
return f"Error: {str(e)}", []
|
| 134 |
+
|
| 135 |
+
submit_btn.click(process_input, inputs=input_box, outputs=[output_text, gallery])
|
| 136 |
+
|
| 137 |
+
return demo
|
| 138 |
|
| 139 |
# Modified prompt templates to handle simple age input
|
| 140 |
custom_prompt_templates = {
|
|
|
|
| 157 |
"react-assistant": "{agent_response}",
|
| 158 |
}
|
| 159 |
|
| 160 |
+
def main():
|
| 161 |
+
# Setup agent
|
| 162 |
+
final_answer = FinalAnswerTool()
|
| 163 |
|
| 164 |
+
model = HfApiModel(
|
| 165 |
+
max_tokens=2096,
|
| 166 |
+
temperature=0.5,
|
| 167 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 168 |
+
)
|
| 169 |
|
| 170 |
+
# Check if custom prompts should be used or merged with existing ones
|
| 171 |
+
try:
|
| 172 |
+
with open("prompts.yaml", 'r') as stream:
|
| 173 |
+
prompt_templates = yaml.safe_load(stream)
|
| 174 |
+
# Merge the templates, giving priority to our custom ones
|
| 175 |
+
prompt_templates.update(custom_prompt_templates)
|
| 176 |
+
except:
|
| 177 |
+
# If file doesn't exist or has issues, use our custom templates
|
| 178 |
+
prompt_templates = custom_prompt_templates
|
| 179 |
+
|
| 180 |
+
agent = CodeAgent(
|
| 181 |
+
model=model,
|
| 182 |
+
tools=[final_answer, generate_passover_coloring_pages],
|
| 183 |
+
max_steps=6,
|
| 184 |
+
verbosity_level=1,
|
| 185 |
+
prompt_templates=prompt_templates
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
# Create and launch Gradio interface
|
| 189 |
+
demo = create_gradio_interface()
|
| 190 |
+
demo.launch(share=False)
|
| 191 |
|
| 192 |
+
if __name__ == "__main__":
|
| 193 |
+
main()
|