CoolFace
Apppublic

toyga/First_agent_template

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
app.py93 linesDownload Raw Back to root
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7 8from Gradio_UI import GradioUI9 10#Custom tool for basic retail KPI calculation11@tool12def calculate_retail_margin(sales_price: float, cost_price: float, quantity: int) -> str:13    """Calculates basic retail KPIs such as revenue, gross profit, and gross margin.14    Args:15        sales_price: Unit selling price of the product.16        cost_price: Unit cost price of the product.17        quantity: Number of units sold.18    """19 20    try:21        revenue = sales_price * quantity22        total_cost = cost_price * quantity23        gross_profit = revenue - total_cost24 25        if revenue == 0:26            return "Revenue is zero, so gross margin cannot be claculated"27 28        gross_margin = gross_profit / revenue * 10029 30        return (31            f"revenue: {revenue:.2f}\n"32            f"Total Cost: {total_cost:.2f}\n"33            f"Gross Profit: {gross_profit:.2f}\n"34            f"Gross Margin: {gross_margin:.2f}%"35        )36    except Exception as e:37        return f"Error calculating retail margin: {str(e)}"38 39 40@tool41def get_current_time_in_timezone(timezone: str) -> str:42    """A tool that fetches the current local time in a specified timezone.43    Args:44        timezone: A string representing a valid timezone (e.g., 'America/New_York').45    """46    try:47        # Create timezone object48        tz = pytz.timezone(timezone)49        # Get current time in that timezone50        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")51        return f"The current local time in {timezone} is: {local_time}"52    except Exception as e:53        return f"Error fetching time for timezone '{timezone}': {str(e)}"54 55 56final_answer = FinalAnswerTool()57 58# 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:59# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 60 61model = HfApiModel(62max_tokens=2096,63temperature=0.5,64model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded65custom_role_conversions=None,66)67 68 69# Import tool from Hub70image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)71 72with open("prompts.yaml", 'r') as stream:73    prompt_templates = yaml.safe_load(stream)74    75agent = CodeAgent(76    model=model,77    tools=[78        final_answer,79        DuckDuckGoSearchTool(),80        get_current_time_in_timezone,81        calculate_retail_margin82    ], ## add your tools here (don't remove final answer)83    max_steps=6,84    verbosity_level=1,85    grammar=None,86    planning_interval=None,87    name=None,88    description=None,89    prompt_templates=prompt_templates90)91 92 93GradioUI(agent).launch()