PascalHamar/First_agent_template
0
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import os5import pytz6import yaml7from tools.final_answer import FinalAnswerTool8 9from Gradio_UI import GradioUI10 11# Below is an example of a tool that does nothing. Amaze us with your creativity !12@tool13def generate_meme(meme:str, top:str, bottom:str)-> str: #it's import to specify the return type14 #Keep this format for the description / args / args description but feel free to modify the tool15 """A tool that creates a meme with custom text at the top and bottom. 16 Args:17 meme: A case-sensitive (first letter of each word is capital) string representing the choosen meme template.18 top: A string representing the shown text at the top of the meme.19 bottom: A string representing the shown text at the bottom of the meme.20 """21 url = "https://ronreiter-meme-generator.p.rapidapi.com/meme"22 params = {23 "meme": meme,24 "top": top,25 "bottom": bottom26 }27 headers = {28 "x-rapidapi-key": os.getenv("RAPIDAPI_KEY"), 29 "x-rapidapi-host": "ronreiter-meme-generator.p.rapidapi.com"30 }31 32 response = requests.get(url, headers=headers, params=params)33 34 if response.status_code == 200:35 return response.url # The image URL36 else:37 return f"Error: {response.status_code}, {response.text}"38 39@tool40def get_current_time_in_timezone(timezone: str) -> str:41 """A tool that fetches the current local time in a specified timezone.42 Args:43 timezone: A string representing a valid timezone (e.g., 'America/New_York').44 """45 try:46 # Create timezone object47 tz = pytz.timezone(timezone)48 # Get current time in that timezone49 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")50 return f"The current local time in {timezone} is: {local_time}"51 except Exception as e:52 return f"Error fetching time for timezone '{timezone}': {str(e)}"53 54 55final_answer = FinalAnswerTool()56 57# 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:58# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 59HF_TOKEN=os.getenv("HF_TOKEN")60if not HF_TOKEN:61 raise ValueError("HF_TOKEN is missing!")62 63model = HfApiModel(64max_tokens=2096,65temperature=0.5,66model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded67custom_role_conversions=None,68token=HF_TOKEN69)70 71 72# Import tool from Hub73image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)74 75with open("prompts.yaml", 'r') as stream:76 prompt_templates = yaml.safe_load(stream)77 78agent = CodeAgent(79 model=model,80 tools=[final_answer, generate_meme], ## add your tools here (don't remove final answer)81 max_steps=6,82 verbosity_level=1,83 grammar=None,84 planning_interval=None,85 name=None,86 description=None,87 prompt_templates=prompt_templates88)89 90 91GradioUI(agent).launch()