from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool import datetime import requests import pytz import yaml from tools.final_answer import FinalAnswerTool from Gradio_UI import GradioUI # 定义工具 @tool def get_weather(city: str) -> str: """A tool that fetches the current weather for a specified city. Args: city: The name of the city (e.g., 'Beijing'). """ try: api_key = "YOUR_OPENWEATHERMAP_API_KEY" # 替换为你的OpenWeatherMap API Key url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric" response = requests.get(url) data = response.json() if response.status_code == 200: weather = data['weather'][0]['description'] temperature = data['main']['temp'] return f"The current weather in {city} is {weather} with a temperature of {temperature}°C." else: return f"Error fetching weather data for {city}: {data['message']}" except Exception as e: return f"Error fetching weather data for {city}: {str(e)}" @tool def translate_text(text: str, source_lang: str, target_lang: str) -> str: """A tool that translates text from one language to another. Args: text: The text to be translated. source_lang: The source language code (e.g., 'en' for English). target_lang: The target language code (e.g., 'zh' for Chinese). """ try: api_key = "YOUR_GOOGLE_TRANSLATE_API_KEY" # 替换为你的Google Translate API Key url = f"https://translation.googleapis.com/language/translate/v2?key={api_key}" data = { "q": text, "source": source_lang, "target": target_lang } response = requests.post(url, json=data) result = response.json() if 'data' in result and 'translations' in result['data']: return result['data']['translations'][0]['translatedText'] else: return f"Error translating text: {result.get('error', {}).get('message', 'Unknown error')}" except Exception as e: return f"Error translating text: {str(e)}" @tool def read_file(file_path: str) -> str: """A tool that reads the content of a local file. Args: file_path: The path to the file (e.g., 'example.txt'). """ try: with open(file_path, 'r', encoding='utf-8') as file: content = file.read() return content except FileNotFoundError: return f"File not found: {file_path}" except Exception as e: return f"Error reading file {file_path}: {str(e)}" @tool def summarize_text(text: str, max_length: int = 100) -> str: """A tool that generates a summary of a given text. Args: text: The text to be summarized. max_length: The maximum length of the summary (default is 100). """ try: from transformers import pipeline summarizer = pipeline("summarization") summary = summarizer(text, max_length=max_length, clean_up_tokenization_spaces=True) return summary[0]['summary_text'] except Exception as e: return f"Error summarizing text: {str(e)}" @tool def calculate_expression(expression: str) -> str: """A tool that evaluates a mathematical expression. Args: expression: The mathematical expression to evaluate (e.g., '2 + 3 * 4'). """ try: result = eval(expression) return f"The result of the expression '{expression}' is {result}." except Exception as e: return f"Error evaluating expression '{expression}': {str(e)}"