Spaces:
Runtime error
Runtime error
File size: 5,533 Bytes
9b5b26a c19d193 6aae614 8fe992b 9b5b26a 5df72d6 9b5b26a c6b6634 9b5b26a 5300cd2 9b5b26a c6b6634 9b5b26a 5300cd2 c6b6634 5300cd2 c6b6634 5300cd2 c6b6634 5300cd2 c6b6634 5300cd2 c6b6634 5300cd2 c6b6634 5300cd2 c6b6634 5300cd2 9b5b26a 8c01ffb 6aae614 ae7a494 40b26a2 ae7a494 e121372 bf6d34c 40b26a2 98f5ded fe328e0 13d500a 8c01ffb 9b5b26a 8c01ffb 861422e 9b5b26a 8c01ffb 8fe992b 5300cd2 8c01ffb 861422e 8fe992b 9b5b26a 8c01ffb |
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
# Below is an example of a tool that does nothing. Amaze us with your creativity !
@tool
def get_zodiac_info(arg1:str, arg2:int)-> str: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""
A tool that returns your Zodiac sign and Chinese zodiac animal based on birth month and year.
Args:
arg1: the first argument
arg2: the second argument
"""
try:
# Validate month input
if not isinstance(arg1, str):
return "Error: Birth month must be a string (e.g., 'February')."
# Convert month to lowercase for case-insensitive comparison
arg1 = arg1.lower()
# Define months and their numerical values for easier date comparison
month_to_num = {
"january": 1, "february": 2, "march": 3, "april": 4,
"may": 5, "june": 6, "july": 7, "august": 8,
"september": 9, "october": 10, "november": 11, "december": 12
}
# Get month number
month_num = month_to_num.get(arg1)
if month_num is None:
return "Error: Invalid month name provided. Please use full month names (e.g., 'February')."
# Validate year input
if not isinstance(arg2, int):
try:
arg2 = int(arg2)
except (ValueError, TypeError):
return "Error: Birth year must be a valid integer."
if arg2 < 1900 or arg2 > 2100:
return "Error: Birth year should be between 1900 and 2100."
# Determine Western zodiac sign
if (month_num == 1 and 20 <= 31) or (month_num == 2 and 1 <= 18):
zodiac_sign = "Aquarius"
elif (month_num == 2 and 19 <= 29) or (month_num == 3 and 1 <= 20):
zodiac_sign = "Pisces"
elif (month_num == 3 and 21 <= 31) or (month_num == 4 and 1 <= 19):
zodiac_sign = "Aries"
elif (month_num == 4 and 20 <= 30) or (month_num == 5 and 1 <= 20):
zodiac_sign = "Taurus"
elif (month_num == 5 and 21 <= 31) or (month_num == 6 and 1 <= 20):
zodiac_sign = "Gemini"
elif (month_num == 6 and 21 <= 30) or (month_num == 7 and 1 <= 22):
zodiac_sign = "Cancer"
elif (month_num == 7 and 23 <= 31) or (month_num == 8 and 1 <= 22):
zodiac_sign = "Leo"
elif (month_num == 8 and 23 <= 31) or (month_num == 9 and 1 <= 22):
zodiac_sign = "Virgo"
elif (month_num == 9 and 23 <= 30) or (month_num == 10 and 1 <= 22):
zodiac_sign = "Libra"
elif (month_num == 10 and 23 <= 31) or (month_num == 11 and 1 <= 21):
zodiac_sign = "Scorpio"
elif (month_num == 11 and 22 <= 30) or (month_num == 12 and 1 <= 21):
zodiac_sign = "Sagittarius"
else:
zodiac_sign = "Capricorn"
# Determine Chinese zodiac animal
# Chinese zodiac repeats every 12 years
# Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey, Rooster, Dog, Pig
chinese_zodiac = ["Monkey", "Rooster", "Dog", "Pig", "Rat", "Ox",
"Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat"]
# Calculate Chinese zodiac animal
zodiac_animal = chinese_zodiac[arg2 % 12]
# Return formatted string
return f"You're a {zodiac_sign} and you were born in the year of the {zodiac_animal}."
except Exception as e:
return f"Error: An unexpected error occurred: {str(e)}"
@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').
"""
try:
# Create timezone object
tz = pytz.timezone(timezone)
# Get current time in that 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)}"
final_answer = FinalAnswerTool()
# 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:
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
# model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
custom_role_conversions=None,
)
# Import tool from Hub
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[get_zodiac_info, final_answer], ## add your tools here (don't remove final answer)
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
GradioUI(agent).launch() |