File size: 3,655 Bytes
4aacf92
9b5b26a
 
 
c19d193
6aae614
9b5b26a
 
4aacf92
9b5b26a
4aacf92
 
9b5b26a
4aacf92
9b5b26a
4aacf92
 
 
 
 
 
 
 
 
 
 
 
 
9b5b26a
 
4aacf92
 
9b5b26a
4aacf92
 
 
9b5b26a
 
4aacf92
 
 
 
 
 
 
 
 
 
 
 
 
9b5b26a
4aacf92
ae7a494
4aacf92
 
 
 
 
 
 
 
 
 
 
 
 
 
8fe992b
4aacf92
 
 
 
 
 
 
 
 
 
 
 
 
 
9b5b26a
4aacf92
 
 
 
 
 
 
 
 
 
 
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
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)}"