CoolFace
Apppublic

Odeta/First_agent_template

sourceHugging Faceupdated 2y 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# Below is an example of a tool that does nothing. Amaze us with your creativity !11@tool12def get_best_sephora_products(limit: int = 5) -> str:13    """Fetches the best-rated products from Sephora's best sellers page.14    Args:15        limit: The number of products to return (default is 5).16    """17    try:18        url = "https://www.sephora.com/beauty/beauty-best-sellers"19        headers = {'User-Agent': 'Mozilla/5.0'}20        response = requests.get(url, headers=headers)21        22        if response.status_code != 200:23            return f"Failed to fetch data from Sephora. Status code: {response.status_code}"24 25        soup = BeautifulSoup(response.content, 'html.parser')26        products = soup.select('.css-12egk0t')27        28        if not products:29            return "No products found. The page structure may have changed."30        31        result = []32        for product in products[:limit]:33            name = product.select_one('.css-2y8mvb')34            rating = product.select_one('.css-1opkl9m')35            price = product.select_one('.css-0')36            37            if name and rating and price:38                result.append(f"{name.text.strip()} - {rating.text.strip()}⭐ ({price.text.strip()})")39        40        return "\n".join(result) if result else "No products available."41    except Exception as e:42        return f"Error fetching Sephora products: {str(e)}"43 44 45@tool46def get_current_time_in_timezone(timezone: str) -> str:47    """A tool that fetches the current local time in a specified timezone.48    Args:49        timezone: A string representing a valid timezone (e.g., 'America/New_York').50    """51    try:52        # Create timezone object53        tz = pytz.timezone(timezone)54        # Get current time in that timezone55        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")56        return f"The current local time in {timezone} is: {local_time}"57    except Exception as e:58        return f"Error fetching time for timezone '{timezone}': {str(e)}"59 60 61final_answer = FinalAnswerTool()62 63# 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:64# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 65 66model = HfApiModel(67max_tokens=2096,68temperature=0.5,69model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded70custom_role_conversions=None,71)72 73 74# Import tool from Hub75image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)76 77with open("prompts.yaml", 'r') as stream:78    prompt_templates = yaml.safe_load(stream)79    80agent = CodeAgent(81    model=model,82    tools=[final_answer,get_best_sephora_products], ## 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()