File size: 7,417 Bytes
9e3bb03
 
 
 
 
 
43b82ee
1d384b8
9e3bb03
 
1d384b8
 
 
 
 
 
 
 
 
 
 
 
 
9e3bb03
43b82ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e3bb03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f28aa7e
9e3bb03
 
 
 
43b82ee
9e3bb03
43b82ee
9e3bb03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f28aa7e
9e3bb03
 
 
 
1d384b8
43b82ee
 
9e3bb03
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import gradio as gr
from smolagents import CodeAgent, DuckDuckGoSearchTool, tool, InferenceClientModel
import datetime
import pytz
import math
import os
import requests
from deep_translator import GoogleTranslator

# ============ TOOLS ============
@tool
def translator_tool(text: str, target_language: str) -> str:
    """Translates text into a specified language.
    Args:
        text: The text or phrase to translate.
        target_language: The destination language (e.g., 'french', 'german', 'japanese', 'hindi').
    """
    try:
        # GoogleTranslator handles full language names or ISO codes
        translation = GoogleTranslator(source='auto', target=target_language).translate(text)
        return f"Translated to {target_language.title()}: {translation}"
    except Exception as e:
        return f"Translation error: {str(e)}. Make sure the language name is correct."

@tool
def weather_tool(location: str) -> str:
    """Get the current weather for any location worldwide.
    
    Args:
        location: The name of the city or place (e.g., 'London' or 'Tokyo').
    """
    try:
        # 1. Geocoding: Convert city name to coordinates using a free service
        geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=1&language=en&format=json"
        geo_res = requests.get(geo_url).json()
        
        if not geo_res.get('results'):
            return f"I couldn't find coordinates for '{location}'. Please check the spelling."
        
        data = geo_res['results'][0]
        lat, lon = data['latitude'], data['longitude']
        city_full = f"{data.get('name')}, {data.get('country')}"

        # 2. Weather: Get current data using coordinates
        weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&current=temperature_2m,relative_humidity_2m,apparent_temperature,weather_code,wind_speed_10m"
        w_res = requests.get(weather_url).json()
        
        curr = w_res['current']
        temp = curr['temperature_2m']
        feels = curr['apparent_temperature']
        hum = curr['relative_humidity_2m']
        wind = curr['wind_speed_10m']

        return (f"🌤️ Weather for {city_full}:\n"
                f"• Temperature: {temp}°C (Feels like {feels}°C)\n"
                f"• Humidity: {hum}%\n"
                f"• Wind Speed: {wind} km/h")
    except Exception as e:
        return f"Weather Error: {str(e)}"


@tool
def web_search_tool(query: str) -> str:
    """Search the web for current information using DuckDuckGo.
    
    Args:
        query: The search query string to look up.
    """
    try:
        search = DuckDuckGoSearchTool()
        results = search(query)
        if not results:
            return "No results found for your query."
        return results
    except Exception as e:
        return f"Search error: {str(e)}"

@tool
def time_tool(timezone: str = "UTC") -> str:
    """Get current time in a specific timezone.
    
    Args:
        timezone: The timezone to check (e.g., 'Asia/Tokyo') or 'list' to see options.
    """
    try:
        if timezone.lower() == "list":
            timezones = ["UTC", "America/New_York", "Europe/London", 
                        "Asia/Tokyo", "Australia/Sydney", "Europe/Paris"]
            return "Available timezones:\n" + "\n".join(f"• {tz}" for tz in timezones)
        
        tz = pytz.timezone(timezone)
        time = datetime.datetime.now(tz)
        return (f"⏰ Time in {timezone}:\n"
                f"• Date: {time.strftime('%Y-%m-%d')}\n"
                f"• Time: {time.strftime('%H:%M:%S')}\n"
                f"• Day: {time.strftime('%A')}")
    except Exception as e:
        return f"Error: {str(e)}. Try 'list' to see available timezones."

@tool
def calculator_tool(expression: str) -> str:
    """Perform mathematical calculations.
    
    Args:
        expression: The math expression to evaluate (e.g., '2 + 2' or 'sqrt(16)').
    """
    try:
        # Simple cleanup for user-friendly operators
        expr = expression.replace('^', '**').replace('×', '*').replace('÷', '/')
        safe_dict = {
            'abs': abs, 'round': round, 'min': min, 'max': max,
            'sqrt': math.sqrt, 'pow': pow, 'sin': math.sin, 
            'cos': math.cos, 'tan': math.tan, 'pi': math.pi, 'e': math.e
        }
        result = eval(expr, {"__builtins__": {}}, safe_dict)
        return f"🧮 Calculation Result: {result}"
    except Exception as e:
        return f"Calculation error: {str(e)}"

@tool
def unit_converter_tool(query: str) -> str:
    """Convert between different units of measurement.
    
    Args:
        query: The conversion string (e.g., '10 km to miles').
    """
    try:
        parts = query.lower().split()
        if len(parts) < 4:
            return "Format: '10 km to miles' or '100 celsius to fahrenheit'"
        
        value = float(parts[0])
        from_unit = parts[1]
        to_unit = parts[3]
        
        conversions = {
            'km': {'miles': 0.621371, 'meters': 1000},
            'miles': {'km': 1.60934, 'meters': 1609.34},
            'meters': {'km': 0.001, 'miles': 0.000621371},
            'kg': {'pounds': 2.20462, 'grams': 1000},
            'pounds': {'kg': 0.453592},
            'liters': {'gallons': 0.264172}
        }
        
        if from_unit == 'celsius' and to_unit == 'fahrenheit':
            result = (value * 9/5) + 32
        elif from_unit == 'fahrenheit' and to_unit == 'celsius':
            result = (value - 32) * 5/9
        elif from_unit in conversions and to_unit in conversions[from_unit]:
            result = value * conversions[from_unit][to_unit]
        else:
            return f"Can't convert {from_unit} to {to_unit}."
            
        return f"📏 {value} {from_unit} = {result:.4f} {to_unit}"
    except Exception as e:
        return f"Conversion error: {str(e)}"

# ============ AGENT SETUP ============

HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN", "")

model = InferenceClientModel(
    model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
    token=HF_TOKEN 
)

agent = CodeAgent(
    model=model,
    tools=[web_search_tool, time_tool, calculator_tool, translator_tool, weather_tool],
    max_steps=5,
    additional_authorized_imports=['math', 'datetime', 'pytz', 'deep_translator', 'requests']
)

# ============ GRADIO UI ============

def chat_with_agent(message, history):
    if not HF_TOKEN:
        return "Please set your HUGGINGFACE_TOKEN as an environment variable to use the AI."
    try:
        # agent.run returns the final answer string
        response = agent.run(message)
        return str(response)
    except Exception as e:
        return f"Agent Error: {str(e)}"

demo = gr.ChatInterface(
    fn=chat_with_agent,
    title="🤖 Alfred AI Assistant",
    description="I am Alfred, your tool-equipped assistant. I can search the web, calculate math, convert units, check the time, check the weather and translate the languages!",
    examples=[
        "What time is it in Tokyo?",
        "Convert 150 pounds to kg",
        "Calculate the square root of 144 plus 50",
        "Translate 'Hello, how are you today?' into Japanese",
        "Search for the latest news on AI agents",
        "What's the weather in Paris right now?", "How's the weather in New York compared to Tokyo?"
    ],
)

if __name__ == "__main__":
    demo.launch(theme=gr.themes.Soft())