Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from tools.final_answer import FinalAnswerTool
|
|
| 6 |
from Gradio_UI import GradioUI
|
| 7 |
import random
|
| 8 |
|
| 9 |
-
# Enhanced custom tool
|
| 10 |
@tool
|
| 11 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 12 |
"""
|
|
@@ -30,7 +30,7 @@ def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
| 30 |
"voila",
|
| 31 |
"presto chango"
|
| 32 |
]
|
| 33 |
-
# Select a phrase based on the magic factor (
|
| 34 |
phrase = magic_phrases[arg2 % len(magic_phrases)]
|
| 35 |
|
| 36 |
# Optionally, add a random flourish
|
|
@@ -38,6 +38,7 @@ def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
| 38 |
|
| 39 |
return f"{flourish} {reversed_message} {phrase}! Your magic factor is {arg2}."
|
| 40 |
|
|
|
|
| 41 |
@tool
|
| 42 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 43 |
"""
|
|
@@ -56,6 +57,23 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 56 |
except Exception as e:
|
| 57 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
final_answer = FinalAnswerTool()
|
| 60 |
model = HfApiModel(
|
| 61 |
max_tokens=2096,
|
|
@@ -64,16 +82,17 @@ model = HfApiModel(
|
|
| 64 |
custom_role_conversions=None,
|
| 65 |
)
|
| 66 |
|
| 67 |
-
# Import tool from Hub (example
|
| 68 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 69 |
|
|
|
|
| 70 |
with open("prompts.yaml", 'r') as stream:
|
| 71 |
prompt_templates = yaml.safe_load(stream)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
agent = CodeAgent(
|
| 75 |
model=model,
|
| 76 |
-
tools=[final_answer, my_custom_tool, get_current_time_in_timezone],
|
| 77 |
max_steps=6,
|
| 78 |
verbosity_level=1,
|
| 79 |
grammar=None,
|
|
@@ -83,4 +102,5 @@ agent = CodeAgent(
|
|
| 83 |
prompt_templates=prompt_templates
|
| 84 |
)
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
| 6 |
from Gradio_UI import GradioUI
|
| 7 |
import random
|
| 8 |
|
| 9 |
+
# Enhanced custom tool that performs a creative transformation (magic)
|
| 10 |
@tool
|
| 11 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 12 |
"""
|
|
|
|
| 30 |
"voila",
|
| 31 |
"presto chango"
|
| 32 |
]
|
| 33 |
+
# Select a phrase based on the magic factor (cycling through the phrases)
|
| 34 |
phrase = magic_phrases[arg2 % len(magic_phrases)]
|
| 35 |
|
| 36 |
# Optionally, add a random flourish
|
|
|
|
| 38 |
|
| 39 |
return f"{flourish} {reversed_message} {phrase}! Your magic factor is {arg2}."
|
| 40 |
|
| 41 |
+
# Tool to fetch the current local time in a specified timezone
|
| 42 |
@tool
|
| 43 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 44 |
"""
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 59 |
|
| 60 |
+
# Tool that performs a magic trick by predicting a random number
|
| 61 |
+
@tool
|
| 62 |
+
def perform_magic() -> str:
|
| 63 |
+
"""
|
| 64 |
+
A tool that performs a magic trick by predicting a random number.
|
| 65 |
+
|
| 66 |
+
Returns:
|
| 67 |
+
A string with the magical prediction.
|
| 68 |
+
"""
|
| 69 |
+
# Simulate inviting the user to think of a number
|
| 70 |
+
print("Please think of a number between 1 and 10...")
|
| 71 |
+
predicted_number = random.randint(1, 10)
|
| 72 |
+
magic_message = f"✨ Abracadabra! Your magical prediction is: {predicted_number} ✨"
|
| 73 |
+
print(magic_message)
|
| 74 |
+
return magic_message
|
| 75 |
+
|
| 76 |
+
# Initialize the final_answer tool and HfApiModel
|
| 77 |
final_answer = FinalAnswerTool()
|
| 78 |
model = HfApiModel(
|
| 79 |
max_tokens=2096,
|
|
|
|
| 82 |
custom_role_conversions=None,
|
| 83 |
)
|
| 84 |
|
| 85 |
+
# Import an image generation tool from the Hub (example usage; not added to agent's tools)
|
| 86 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 87 |
|
| 88 |
+
# Load prompt templates from a YAML file
|
| 89 |
with open("prompts.yaml", 'r') as stream:
|
| 90 |
prompt_templates = yaml.safe_load(stream)
|
| 91 |
|
| 92 |
+
# Instantiate the CodeAgent with the required tools. The final_answer tool must always be included.
|
| 93 |
agent = CodeAgent(
|
| 94 |
model=model,
|
| 95 |
+
tools=[final_answer, my_custom_tool, get_current_time_in_timezone, perform_magic],
|
| 96 |
max_steps=6,
|
| 97 |
verbosity_level=1,
|
| 98 |
grammar=None,
|
|
|
|
| 102 |
prompt_templates=prompt_templates
|
| 103 |
)
|
| 104 |
|
| 105 |
+
# Launch the Gradio UI to interact with the agent.
|
| 106 |
+
GradioUI(agent).launch()
|