SaharNasser/First_agent_template
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@tool21def compute_muscle_to_fat_ratio(weight: float, body_fat_percentage: float) -> str:22 """23 A tool that computes the muscle-to-fat mass ratio for a person.24 25 Args:26 weight: The person's total body weight in kilograms.27 body_fat_percentage: The person's body fat percentage (e.g., 20.0 for 20%).28 29 Returns:30 A string summarizing the computed muscle-to-fat ratio. This ratio is approximated as:31 (Lean Body Mass / Fat Mass), where Lean Body Mass = weight - fat mass and32 Fat Mass = weight * (body_fat_percentage / 100).33 34 Note:35 This is a simplified estimation. For a more accurate analysis, consider using advanced36 body composition assessments.37 """38 try:39 fat_mass = weight * (body_fat_percentage / 100)40 if fat_mass == 0:41 return "Fat mass is zero; the muscle-to-fat ratio is undefined (check the body fat percentage)."42 lean_mass = weight - fat_mass43 ratio = lean_mass / fat_mass44 return f"The estimated muscle-to-fat ratio is {ratio:.2f}."45 except Exception as e:46 return f"Error computing muscle-to-fat ratio: {str(e)}"47 48@tool49def get_current_time_in_timezone(timezone: str) -> str:50 """A tool that fetches the current local time in a specified timezone.51 Args:52 timezone: A string representing a valid timezone (e.g., 'America/New_York').53 """54 try:55 # Create timezone object56 tz = pytz.timezone(timezone)57 # Get current time in that timezone58 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")59 return f"The current local time in {timezone} is: {local_time}"60 except Exception as e:61 return f"Error fetching time for timezone '{timezone}': {str(e)}"62 63 64final_answer = FinalAnswerTool()65 66# 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:67# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 68 69model = HfApiModel(70max_tokens=2096,71temperature=0.5,72model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded73custom_role_conversions=None,74 75)76 77 78# Import tool from Hub79image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)80 81with open("prompts.yaml", 'r') as stream:82 prompt_templates = yaml.safe_load(stream)83 84agent = CodeAgent(85 model=model,86 tools=[final_answer,compute_muscle_to_fat_ratio], ## add your tools here (don't remove final answer)87 max_steps=6,88 verbosity_level=1,89 grammar=None,90 planning_interval=None,91 name=None,92 description=None,93 prompt_templates=prompt_templates94)95 96 97GradioUI(agent).launch()