File size: 4,733 Bytes
60544bf
 
 
 
8805ef5
60544bf
827acbf
6819f29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f103d45
6819f29
 
 
 
 
 
60544bf
d59f99c
60544bf
 
 
 
 
 
 
 
d59f99c
60544bf
 
 
 
 
 
 
 
 
 
 
 
c5a1bb4
d59f99c
f103d45
60544bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f103d45
60544bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f103d45
60544bf
 
 
 
 
 
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
import numpy as np
import matplotlib.pyplot as plt
from smolagents.tools import tool
import time
from typing import Optional, List, Dict

@tool
def plot_line_chart(x_values: List, y_values: List[float], title: str = "Gráfico de Linhas", x_label: str = "Eixo X", y_label: str = "Eixo Y") -> str:
    """
    Gera e salva um gráfico de linhas, ideal para mostrar tendências ao longo do tempo.
    Args:
        x_values: Uma lista de rótulos ou números para o eixo X (ex: meses, anos).
        y_values: Uma lista de números (int ou float) para o eixo Y.
        title: O título do gráfico.
        x_label: O rótulo do eixo X.
        y_label: O rótulo do eixo Y.
    Returns:
        O caminho do arquivo da imagem PNG gerada.
    """
    try:
        if len(x_values) != len(y_values):
            return "Erro: As listas x_values e y_values devem ter o mesmo tamanho."

        plt.figure(figsize=(10, 6))
        plt.plot(x_values, y_values, marker='o', linestyle='-')
        plt.title(title)
        plt.xlabel(x_label)
        plt.ylabel(y_label)
        plt.grid(True, which='both', linestyle='--', linewidth=0.5)
        plt.tight_layout()

        file_path = f"line_chart_{int(time.time_ns())}.png"
        plt.savefig(file_path)
        plt.close()

        return f"Gráfico de linhas gerado e salvo em: {file_path}"
    except Exception as e:
        return f"Erro ao gerar o gráfico de linhas: {str(e)}"
@tool
def plot_bar_chart(labels: List[str], values: List[float], title: str = "Gráfico de Barras", x_label: str = "Categorias", y_label: str = "Valores", y_lim: Optional[List[float]] = None) -> str:
    """
    Gera e salva um gráfico de barras.
    Args:
        labels: Uma lista de strings para os rótulos de cada barra.
        values: Uma lista de números (int ou float) correspondendo ao valor de cada barra.
        title: O título do gráfico.
        x_label: O rótulo do eixo X.
        y_label: O rótulo do eixo Y.
        y_lim: Opcional. Uma lista [min, max] para definir o limite do eixo Y, útil para criar distorções.
    Returns:
        O caminho do arquivo da imagem PNG gerada.
    """
    try:
        plt.figure(figsize=(10, 6))
        plt.bar(labels, values)
        plt.title(title)
        plt.xlabel(x_label)
        plt.ylabel(y_label)
        plt.xticks(rotation=45, ha="right")
        plt.grid(axis='y', linestyle='--', alpha=0.7)
        plt.tight_layout()
        if y_lim: 
            plt.ylim(y_lim)
        file_path = f"bar_chart_{int(time.time_ns())}.png"
        plt.savefig(file_path)
        plt.close()
        return f"Gráfico de barras gerado e salvo em: {file_path}"
    except Exception as e:
        return f"Erro ao gerar o gráfico de barras: {str(e)}"

@tool
def plot_pie_chart(labels: List[str], sizes: List[float], title: str = "Gráfico de Setores") -> str:
    """
    Gera e salva um gráfico de pizza (setores).
    Args:
        labels: Uma lista de strings para os rótulos de cada fatia.
        sizes: Uma lista de números (int ou float) correspondendo ao tamanho de cada fatia.
        title: O título do gráfico.
    Returns:
        O caminho do arquivo da imagem PNG gerada.
    """
    try:
        plt.figure(figsize=(8, 8))
        plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140, shadow=True)
        plt.title(title)
        plt.axis('equal')  # Assegura que o gráfico seja um círculo.

        file_path = f"pie_chart_{int(time.time_ns())}.png"
        plt.savefig(file_path)
        plt.close()

        return f"Gráfico de setores gerado e salvo em: {file_path}"
    except Exception as e:
        return f"Erro ao gerar o gráfico de setores: {str(e)}"

@tool
def plot_histogram(data: List[float], bins: int = 10, title: str = "Histograma", x_label: str = "Valores", y_label: str = "Frequência") -> str:
    """
    Gera e salva um histograma a partir de um conjunto de dados.
    Args:
        data: Uma lista de números (int ou float) para compor o histograma.
        bins: O número de 'caixas' (colunas) no histograma.
        title: O título do gráfico.
        x_label: O rótulo do eixo X.
        y_label: O rótulo do eixo Y.
    Returns:
        O caminho do arquivo da imagem PNG gerada.
    """
    try:
        plt.figure(figsize=(10, 6))
        plt.hist(data, bins=bins, edgecolor='black')
        plt.title(title)
        plt.xlabel(x_label)
        plt.ylabel(y_label)
        plt.grid(axis='y', linestyle='--', alpha=0.7)

        file_path = f"histogram_{int(time.time_ns())}.png"
        plt.savefig(file_path)
        plt.close()

        return f"Histograma gerado e salvo em: {file_path}"
    except Exception as e:
        return f"Erro ao gerar o histograma: {str(e)}"