sahilmate/JokeAndImageGenerator_LocalTimeTeller
0
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7 8from Gradio_UI import GradioUI9'''10# Below is an example of a tool that does nothing. Amaze us with your creativity !11@tool12def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type13 #Keep this format for the description / args / args description but feel free to modify the tool14 """A tool that does nothing yet 15 Args:16 arg1: the first argument17 arg2: the second argument18 """19 return "What magic will you build ?" 20'''21@tool22def get_random_joke()->str:23 """A tool that fetches a random joke from an online API."""24 try:25 response = requests.get("https://official-joke-api.appspot.com/random_joke")26 if response.status_code == 200:27 joke_data = response.json()28 return f"{joke_data['setup']} ... {joke_data['punchline']}"29 else:30 return "Sorry, I couldn't fetch a joke at this time."31 except Exception as e:32 return f"Error: {str(e)}"33 34@tool35def get_current_time_in_timezone(timezone: str) -> str:36 """A tool that fetches the current local time in a specified timezone.37 Args:38 timezone: A string representing a valid timezone (e.g., 'America/New_York').39 """40 try:41 # Create timezone object42 tz = pytz.timezone(timezone)43 # Get current time in that timezone44 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")45 return f"The current local time in {timezone} is: {local_time}"46 except Exception as e:47 return f"Error fetching time for timezone '{timezone}': {str(e)}"48 49 50final_answer = FinalAnswerTool()51 52# 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:53# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 54 55model = HfApiModel(56max_tokens=2096,57temperature=0.5,58model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded59custom_role_conversions=None,60)61 62 63# Import tool from Hub64image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)65 66with open("prompts.yaml", 'r') as stream:67 prompt_templates = yaml.safe_load(stream)68 69agent = CodeAgent(70 model=model,71 tools=[final_answer,image_generation_tool, get_current_time_in_timezone, get_random_joke], ## add your tools here (don't remove final answer)72 max_steps=6,73 verbosity_level=1,74 grammar=None,75 planning_interval=None,76 name=None,77 description=None,78 prompt_templates=prompt_templates79)80 81 82GradioUI(agent).launch()