CoolFace
Apppublic

pallxavi/First_agent_template

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py109 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@tool12 13 14def get_weather_info(city: str) -> str:15    """A tool that returns a mock weather report for a specified city.16    Args:17        city: The name of the city (e.g., 'New York').18    """19    import random20    try:21        # Simulate weather conditions22        conditions = ["Sunny", "Rainy", "Cloudy", "Stormy", "Snowy", "Windy"]23        temp = random.randint(-5, 35)  # Random temperature between -5 and 35°C24        weather_condition = random.choice(conditions)25        26        # Return mock weather info27        return f"The current weather in {city} is {temp}°C with {weather_condition}."28    29    except Exception as e:30        return f"Error fetching weather for city '{city}': {str(e)}"31 32@tool33def get_current_time_in_timezone(timezone: str) -> str:34    """A tool that fetches the current local time in a specified timezone.35    Args:36        timezone: A string representing a valid timezone (e.g., 'America/New_York').37    """38    try:39        # Create timezone object40        tz = pytz.timezone(timezone)41        # Get current time in that timezone42        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")43        return f"The current local time in {timezone} is: {local_time}"44    except Exception as e:45        return f"Error fetching time for timezone '{timezone}': {str(e)}"46 47 48@tool49def generate_random_quote() -> str:50    """A tool that returns a random motivational quote.51    Args:52        None53    """54    try:55        quotes = [56            "The only way to do great work is to love what you do. – Steve Jobs",57            "It does not matter how slowly you go as long as you do not stop. – Confucius",58            "Success is not final, failure is not fatal: It is the courage to continue that counts. – Winston Churchill",59            "The future belongs to those who believe in the beauty of their dreams. – Eleanor Roosevelt",60            "Don't watch the clock; do what it does. Keep going. – Sam Levenson",61            "Believe you can and you're halfway there. – Theodore Roosevelt",62            "The best time to plant a tree was 20 years ago. The second best time is now. – Chinese Proverb"63        ]64        65        # Select a random quote66        quote = random.choice(quotes)67        68        # Return the selected quote69        return f"Here's a motivational quote: {quote}"70    71    except Exception as e:72        return f"Error generating quote: {str(e)}"73 74 75 76final_answer = FinalAnswerTool()77model = HfApiModel(78max_tokens=2096,79temperature=0.5,80model_id='Qwen/Qwen2.5-Coder-32B-Instruct',81custom_role_conversions=None,82)83 84 85# Import tool from Hub86image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)87 88with open("prompts.yaml", 'r') as stream:89    prompt_templates = yaml.safe_load(stream)90 91custom_tools = [92    get_current_time_in_timezone,93    get_weather_info,generate_random_quote94    ]95    96agent = CodeAgent(97    model=model,98    tools=[final_answer] + custom_tools, # add your tools here (don't remove final_answer)99    max_steps=6,100    verbosity_level=1,101    grammar=None,102    planning_interval=None,103    name=None,104    description=None,105    prompt_templates=prompt_templates106)107 108 109GradioUI(agent).launch()