Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -34,6 +34,32 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
final_answer = FinalAnswerTool()
|
38 |
|
39 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
@@ -55,7 +81,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
55 |
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer, my_custom_tool, get_current_time_in_timezone, image_generation_tool], ## add your tools here (don't remove final answer)
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
|
|
34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
|
36 |
|
37 |
+
# Define a custom image generation tool with better error handling
|
38 |
+
@tool
|
39 |
+
def generate_image(prompt: str) -> object:
|
40 |
+
"""Generates an image based on the text prompt provided.
|
41 |
+
Args:
|
42 |
+
prompt: A detailed description of the image to generate. For better results,
|
43 |
+
include details like 'high-res, photorealistic, detailed lighting'.
|
44 |
+
"""
|
45 |
+
try:
|
46 |
+
# First try to use the FLUX model via Tool.from_space
|
47 |
+
image_tool = Tool.from_space(
|
48 |
+
"black-forest-labs/FLUX.1-schnell",
|
49 |
+
name="image_generator",
|
50 |
+
description="Generate an image from a prompt"
|
51 |
+
)
|
52 |
+
return image_tool.forward(prompt=prompt)
|
53 |
+
except Exception as e:
|
54 |
+
# Fall back to the original tool if the first method fails
|
55 |
+
try:
|
56 |
+
# Try the m-ric tool which is known to work well
|
57 |
+
fallback_tool = load_tool("m-ric/text-to-image", trust_remote_code=True)
|
58 |
+
return fallback_tool(prompt=prompt)
|
59 |
+
except Exception as e2:
|
60 |
+
return f"Image generation failed with both methods. Errors: {str(e)}, then {str(e2)}"
|
61 |
+
|
62 |
+
|
63 |
final_answer = FinalAnswerTool()
|
64 |
|
65 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
|
|
81 |
|
82 |
agent = CodeAgent(
|
83 |
model=model,
|
84 |
+
tools=[final_answer, my_custom_tool, get_current_time_in_timezone, generate_image, image_generation_tool], ## add your tools here (don't remove final answer)
|
85 |
max_steps=6,
|
86 |
verbosity_level=1,
|
87 |
grammar=None,
|