Spaces:
Sleeping
Sleeping
| 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 ============ | |
| 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." | |
| 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}¤t=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)}" | |
| 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)}" | |
| 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." | |
| 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)}" | |
| 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()) |