Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -57,6 +57,7 @@ def generate_passover_coloring_pages(age: int) -> str:
|
|
| 57 |
}
|
| 58 |
|
| 59 |
selected_pages = passover_pages[complexity]
|
|
|
|
| 60 |
|
| 61 |
# Try to generate images with improved prompts
|
| 62 |
try:
|
|
@@ -68,15 +69,53 @@ def generate_passover_coloring_pages(age: int) -> str:
|
|
| 68 |
enhanced_prompts.append(f"{page}, suitable for printing as a coloring page")
|
| 69 |
|
| 70 |
images = [image_generation_tool(prompt) for prompt in enhanced_prompts]
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
except Exception as e:
|
| 73 |
# If image generation fails, return text description
|
| 74 |
-
response = f"Passover Coloring Pages for a {age}-year-old:\n\n"
|
| 75 |
-
response += "\n".join(
|
| 76 |
return response
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
# Setup agent
|
| 79 |
-
final_answer =
|
| 80 |
|
| 81 |
model = HfApiModel(
|
| 82 |
max_tokens=2096,
|
|
@@ -84,8 +123,15 @@ model = HfApiModel(
|
|
| 84 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 85 |
)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
agent = CodeAgent(
|
| 91 |
model=model,
|
|
@@ -95,4 +141,5 @@ agent = CodeAgent(
|
|
| 95 |
prompt_templates=prompt_templates
|
| 96 |
)
|
| 97 |
|
|
|
|
| 98 |
GradioUI(agent).launch()
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
selected_pages = passover_pages[complexity]
|
| 60 |
+
descriptions = [f"- {page.split(',')[0]}" for page in selected_pages]
|
| 61 |
|
| 62 |
# Try to generate images with improved prompts
|
| 63 |
try:
|
|
|
|
| 69 |
enhanced_prompts.append(f"{page}, suitable for printing as a coloring page")
|
| 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 both the text and images to final_answer
|
| 78 |
+
return {"text": response, "images": images}
|
| 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 |
+
# Modified final answer function to handle image objects
|
| 86 |
+
class EnhancedFinalAnswerTool(FinalAnswerTool):
|
| 87 |
+
def __call__(self, answer):
|
| 88 |
+
# If answer is a dictionary with text and images
|
| 89 |
+
if isinstance(answer, dict) and "text" in answer and "images" in answer:
|
| 90 |
+
# Process images here if needed
|
| 91 |
+
# For GradioUI, we can return the dictionary directly
|
| 92 |
+
return answer
|
| 93 |
+
# Otherwise use the original behavior
|
| 94 |
+
return super().__call__(answer)
|
| 95 |
+
|
| 96 |
+
# Modified prompt templates to handle simple age input
|
| 97 |
+
custom_prompt_templates = {
|
| 98 |
+
"react-system": """You are an helpful agent that helps children learn about Passover through coloring pages.
|
| 99 |
+
You have access to the following tools:
|
| 100 |
+
|
| 101 |
+
{tools_string}
|
| 102 |
+
|
| 103 |
+
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.
|
| 104 |
+
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.
|
| 105 |
+
|
| 106 |
+
To use a tool, output an action in this format:
|
| 107 |
+
```py
|
| 108 |
+
action = tool_name(arg1=value1, arg2=value2, ...)
|
| 109 |
+
```
|
| 110 |
+
|
| 111 |
+
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.
|
| 112 |
+
""",
|
| 113 |
+
"react-human": "{input_string}",
|
| 114 |
+
"react-assistant": "{agent_response}",
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
# Setup agent
|
| 118 |
+
final_answer = EnhancedFinalAnswerTool()
|
| 119 |
|
| 120 |
model = HfApiModel(
|
| 121 |
max_tokens=2096,
|
|
|
|
| 123 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 124 |
)
|
| 125 |
|
| 126 |
+
# Check if custom prompts should be used or merged with existing ones
|
| 127 |
+
try:
|
| 128 |
+
with open("prompts.yaml", 'r') as stream:
|
| 129 |
+
prompt_templates = yaml.safe_load(stream)
|
| 130 |
+
# Merge the templates, giving priority to our custom ones
|
| 131 |
+
prompt_templates.update(custom_prompt_templates)
|
| 132 |
+
except:
|
| 133 |
+
# If file doesn't exist or has issues, use our custom templates
|
| 134 |
+
prompt_templates = custom_prompt_templates
|
| 135 |
|
| 136 |
agent = CodeAgent(
|
| 137 |
model=model,
|
|
|
|
| 141 |
prompt_templates=prompt_templates
|
| 142 |
)
|
| 143 |
|
| 144 |
+
# Launch Gradio UI
|
| 145 |
GradioUI(agent).launch()
|