File size: 3,350 Bytes
82d9369
9b5b26a
 
 
c19d193
82d9369
9b5b26a
 
 
82d9369
9b5b26a
 
82d9369
9b5b26a
 
 
 
82d9369
9b5b26a
82d9369
8c01ffb
82d9369
 
 
 
 
 
8c01ffb
82d9369
 
 
ae7a494
82d9369
 
 
 
ae7a494
82d9369
 
 
 
8c01ffb
82d9369
 
 
 
 
 
 
e26fe83
82d9369
 
 
 
 
 
 
 
 
 
e26fe83
82d9369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c01ffb
82d9369
8c01ffb
82d9369
861422e
82d9369
8c01ffb
8fe992b
82d9369
8c01ffb
 
82d9369
 
8fe992b
 
8c01ffb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from smolagents import CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, InferenceClientModel,load_tool, tool
import datetime
import requests
import pytz
import yaml
from typing import List
from Gradio_UI import GradioUI

@tool
def get_current_time_in_timezone(timezone:str) -> str:
    """A tool that fetches the current local time in a specified timezone.
    Args:
        timezone: A string representing a valid timezone (e.g. , 'America/New_York')
    """
    try:
        tz = pytz.timezone(timezone)
        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
        return f"The current local time in {timezone} is {local_time}"
    except Exception as e:
        return f"Error fetching time for '{timezone}':{str(e)}"

@tool
def web_search(searchstring:str) -> List[str]:
    """A tool that conducts search about a topic and gives the first 3 results.
    Args:
        searchstring: A string containing the topic to search for (e.g. , 'GlobalWarming/Covid-19/BloomsTaxonomy')
    """

    try:
        search_provider = DuckDuckGoSearchTool()
        raw_results = search_provider.forward(searchstring)

        if isinstance(raw_results, str):
            results = raw_results.split("\n")
        else:
            results = raw_results

        return results[:3]
        
    except Exception as e:
        return [f"Search Failed :{str(e)}"]

@tool
def get_weather_forecast(city: str) -> str:
    """A tool that fetches a 3 day weather forecast.
    Args:
        city: The name of the city (e.g. 'Paris', 'Tokyo').
    """
    try:
        geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1&language=en&format=json"
        geo_res = requests.get(geo_url).json()
    
        if not geo_res.get('results'):
            return f"Could no tfind co-odinates for {city}"
    
        lat = geo_res['results'][0]['latitude']
        lon = geo_res['results'][0]['longitude']
    
        #Forecast
    
        weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&daily=weather_code,temperature_2m_max,precipitation_probability_max&timezone=auto"
        weather_res = requests.get(weather_url).json()
    
        days = weather_res['daily']
        summary = f"Forecast for {city}"
    
        for i in range(3):
            date = days['time'][i]
            temp = days['temperature_2m_max'][i]
            rain_probability = days['precipitation_probability_max'][i]
            summary += f"-{date} : {temp}°C, Rain probability : {rain_probability}%\n"
        return summary
    except Exception as e:
        return f"Error fetching weather : {str(e)}"
    
final_answer = FinalAnswerTool()
model = InferenceClientModel(
    max_tokens=2096,
    temperature=0.5,
    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
    custom_role_conversions=None
)

image_generation_tool = load_tool("agents-course/text-to-image",trust_remote_code=True)

with open('prompts.yaml', 'r') as stream:
    prompt_templates = yaml.safe_load(stream)

agent = CodeAgent(
    model=model,
    tools=[final_answer,get_current_time_in_timezone,web_search,image_generation_tool,get_weather_forecast],
    max_steps=6,
    verbosity_level=1,
    prompt_templates=prompt_templates,
    additional_authorized_imports=['requests', 'pytz', 'datetime', 'yaml']
)

GradioUI(agent).launch()