File size: 3,827 Bytes
180d089
9b5b26a
 
 
c19d193
100fb9f
6aae614
9b5b26a
 
180d089
9b5b26a
180d089
f9e5983
177a6bf
 
3c5f31c
f9e5983
177a6bf
f9e5983
177a6bf
180d089
 
 
 
 
 
 
 
9b5b26a
ea43098
9b5b26a
ea43098
 
 
177a6bf
ea43098
177a6bf
 
ea43098
177a6bf
9b5b26a
ea43098
 
916de5e
323afad
 
 
 
47edb3d
9b5b26a
ea43098
 
1083af3
180d089
1083af3
52003fe
 
177a6bf
52003fe
177a6bf
180d089
52003fe
 
 
180d089
52003fe
180d089
52003fe
8c01ffb
180d089
 
 
f9e5983
5c0bd6c
177a6bf
3c5f31c
177a6bf
f9e5983
177a6bf
180d089
 
8d3c638
 
 
 
5c0bd6c
180d089
5c0bd6c
180d089
 
8c01ffb
5c0bd6c
180d089
1083af3
180d089
 
e121372
180d089
 
 
 
13d500a
8c01ffb
180d089
861422e
 
180d089
 
8c01ffb
8fe992b
19d06fe
8c01ffb
 
 
 
 
 
861422e
8fe992b
 
180d089
 
177a6bf
f9e5983
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
import re
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI

# Tool to fetch weather data (No API Key Required, Uses wttr.in)
@tool
def get_weather(city: str) -> str:
    """Fetches current weather for a given city using wttr.in.
    
    Args:
        city: The name of the city for which weather information is needed.
    
    Returns:
        str: A string containing weather details such as temperature, humidity, and wind speed.
    """
    try:
        url = f"https://wttr.in/{city}?format=%C+%t+%h+%w"
        response = requests.get(url)
        if response.status_code == 200:
            return f"Weather in {city}: {response.text}"
        return "Weather data unavailable."
    except Exception as e:
        return f"Error fetching weather: {e}"

# Tool to fetch top 5 tourist attractions in a city
@tool
def get_top_tourist_attractions(city: str) -> str:
    """Finds the top 5 must-visit places in a specified city using DuckDuckGo search.

    Args:
        city: The name of the city where attractions are being searched.

    Returns:
        str: A list of the top 5 tourist attractions (names only).
    """
    try:
        search_tool = DuckDuckGoSearchTool()
        results = search_tool.forward(f"Top tourist attractions in {city}")

        if results:
            return f"Top 5 Tourist Attractions in {city}:\n{results}"
        
        return f"No tourist attractions found for {city}."

    except Exception as e:
        return f"Error fetching tourist attractions: {e}"


# Tool to get local time
@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:
        # Create timezone object
        tz = pytz.timezone(timezone)
        # Get current time in that 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 '{timezone}': {str(e)}"

# Tool to fetch top 5 restaurants using DuckDuckGo Search
@tool
def get_top_restaurants(city: str) -> str:
    """Finds the top 5 restaurants in a specified city using DuckDuckGo search.

    Args:
        city: The name of the city where restaurants are being searched.
    Returns:
        str: A list of the top 5 restaurants including names and website links.
    """
    try:
        search_tool = DuckDuckGoSearchTool()
        results = search_tool.forward(f"Top 5 restaurants in {city}")

        if results:
            return f"Top 5 Restaurants in {city}:\n{results}"
        
        return "No restaurant data found."

    except Exception as e:
        return f"Error fetching restaurants: {e}"


# Final answer tool
final_answer = FinalAnswerTool()

# AI model setup (using a default model for now, can be replaced)
model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
    custom_role_conversions=None,
)

# Loading the prompt templates
with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)

# Setting up the CodeAgent with the defined tools
agent = CodeAgent(
    model=model,
    tools=[final_answer, get_weather, get_top_tourist_attractions, get_current_time_in_timezone, get_top_restaurants],
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)

# Launch Gradio UI
GradioUI(agent).launch()