CoolFace
Apppublic

umiahmed30/First_agent_template

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py90 linesDownload Raw Back to root
1from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7from Gradio_UI import GradioUI8 9@tool10def activity_recommendation(location: str) -> str:11    """12    A tool that recommends activities based on a specific location.13    14    Args:15        location: A string representing a valid location anywhere on Earth (e.g., London, Paris, Bangladesh)16    17    Returns:18        A string containing activity recommendations or an error message if the location is fictional.19    """20    # Use the model correctly to check if the location is real21    prompt = f"Is {location} a real-world place or a fictional location? Answer with just 'Real' or 'Fictional'."22 23    messages = [24      {"role": "user", "content": [{"type": "text", "text": prompt}]}25    ]26    27    response = agent.model(messages)28 29    response_text = response.content.strip().lower()30 31    if "fictional" in response_text:32        return "I can't recommend activities in a fictional location!"33 34    # Use DuckDuckGo to fetch activities35    search_tool = DuckDuckGoSearchTool()36    results = search_tool(f"Recommend me some activities in {location}")37    return results38 39@tool40def get_current_time_in_timezone(timezone: str) -> str:41    """42    A tool that fetches the current local time in a specified timezone.43 44    Args:45        timezone: A string representing a valid timezone (e.g., 'America/New_York').46    47    Returns:48        A formatted string with the current time in the given timezone.49    """50    try:51        tz = pytz.timezone(timezone)52        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")53        return f"The current local time in {timezone} is: {local_time}"54    except Exception as e:55        return f"Error fetching time for timezone '{timezone}': {str(e)}"56 57# Initialize final answer tool58final_answer = FinalAnswerTool()59 60# Define model correctly61model = HfApiModel(62    max_tokens=2096,63    temperature=0.5,64    model_id="Qwen/Qwen2.5-Coder-32B-Instruct",65    custom_role_conversions=None,66)67 68# Import tool from Hugging Face Hub69image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)70 71# Load prompt templates72with open("prompts.yaml", "r") as stream:73    prompt_templates = yaml.safe_load(stream)74 75# Initialize the agent with the correct model76agent = CodeAgent(77    model=model,78    tools=[final_answer, get_current_time_in_timezone, activity_recommendation],  # Keep final_answer tool79    max_steps=6,80    verbosity_level=1,81    grammar=None,82    planning_interval=None,83    name=None,84    description=None,85    prompt_templates=prompt_templates,86)87 88# Launch Gradio UI89GradioUI(agent).launch()90