Spaces:
Sleeping
Sleeping
File size: 3,568 Bytes
367ef16 9b5b26a c19d193 6aae614 9b5b26a 367ef16 9b5b26a e9844c4 9b5b26a 367ef16 9b5b26a 367ef16 9b5b26a 367ef16 e9844c4 367ef16 9b5b26a e9844c4 9b5b26a 367ef16 9b5b26a 367ef16 9b5b26a 8c01ffb e9844c4 6aae614 e121372 367ef16 13d500a 8c01ffb e9844c4 9b5b26a 8c01ffb e9844c4 861422e 367ef16 e9844c4 8c01ffb 8fe992b e9844c4 8c01ffb 861422e 8fe992b e9844c4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
import random
# Enhanced custom tool that performs a creative transformation (magic)
@tool
def my_custom_tool(arg1: str, arg2: int) -> str:
"""
A creatively enhanced tool that transforms a given message based on a magic factor.
Args:
arg1: The input message to transform.
arg2: The magic factor (an integer) that determines the level of transformation.
Returns:
A magically transformed message.
"""
# Reverse the input string as a simple transformation
reversed_message = arg1[::-1]
# Define a few inspirational magic phrases
magic_phrases = [
"abracadabra",
"hocus pocus",
"alakazam",
"voila",
"presto chango"
]
# Select a phrase based on the magic factor (cycling through the phrases)
phrase = magic_phrases[arg2 % len(magic_phrases)]
# Optionally, add a random flourish
flourish = random.choice(["✨", "🌟", "🔮", "🎩"])
return f"{flourish} {reversed_message} {phrase}! Your magic factor is {arg2}."
# Tool to fetch the current local time in a specified timezone
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""
A tool that fetches the current local time in a specified timezone.
Args:
timezone: A string representing a valid timezone (e.g., 'America/New_York').
Returns:
A string with the current local time or an error message if unsuccessful.
"""
try:
tz = pytz.timezone(timezone)
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"The current local time in {timezone} is: {local_time}"
except Exception as e:
return f"Error fetching time for timezone '{timezone}': {str(e)}"
# Tool that performs a magic trick by predicting a random number
@tool
def perform_magic() -> str:
"""
A tool that performs a magic trick by predicting a random number.
Returns:
A string with the magical prediction.
"""
# Simulate inviting the user to think of a number
print("Please think of a number between 1 and 10...")
predicted_number = random.randint(1, 10)
magic_message = f"✨ Abracadabra! Your magical prediction is: {predicted_number} ✨"
print(magic_message)
return magic_message
# Initialize the final_answer tool and HfApiModel
final_answer = FinalAnswerTool()
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud',
custom_role_conversions=None,
)
# Import an image generation tool from the Hub (example usage; not added to agent's tools)
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
# Load prompt templates from a YAML file
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
# Instantiate the CodeAgent with the required tools. The final_answer tool must always be included.
agent = CodeAgent(
model=model,
tools=[final_answer, my_custom_tool, get_current_time_in_timezone, perform_magic],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
# Launch the Gradio UI to interact with the agent.
GradioUI(agent).launch()
|