Spaces:
Sleeping
Sleeping
Create statistical_graph_generator.py
Browse files
tools/statistical_graph_generator.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
from smolagents.tools import tool
|
| 4 |
+
import time
|
| 5 |
+
from typing import List, Dict
|
| 6 |
+
|
| 7 |
+
@tool
|
| 8 |
+
def plot_bar_chart(labels: List[str], values: List[float], title: str = "Gráfico de Barras", x_label: str = "Categorias", y_label: str = "Valores") -> str:
|
| 9 |
+
"""
|
| 10 |
+
Gera e salva um gráfico de barras.
|
| 11 |
+
Args:
|
| 12 |
+
labels: Uma lista de strings para os rótulos de cada barra.
|
| 13 |
+
values: Uma lista de números (int ou float) correspondendo ao valor de cada barra.
|
| 14 |
+
title: O título do gráfico.
|
| 15 |
+
x_label: O rótulo do eixo X.
|
| 16 |
+
y_label: O rótulo do eixo Y.
|
| 17 |
+
Returns:
|
| 18 |
+
O caminho do arquivo da imagem PNG gerada.
|
| 19 |
+
"""
|
| 20 |
+
try:
|
| 21 |
+
plt.figure(figsize=(10, 6))
|
| 22 |
+
plt.bar(labels, values)
|
| 23 |
+
plt.title(title)
|
| 24 |
+
plt.xlabel(x_label)
|
| 25 |
+
plt.ylabel(y_label)
|
| 26 |
+
plt.xticks(rotation=45, ha="right")
|
| 27 |
+
plt.grid(axis='y', linestyle='--', alpha=0.7)
|
| 28 |
+
plt.tight_layout()
|
| 29 |
+
|
| 30 |
+
file_path = f"bar_chart_{int(time.time())}.png"
|
| 31 |
+
plt.savefig(file_path)
|
| 32 |
+
plt.close()
|
| 33 |
+
|
| 34 |
+
return f"Gráfico de barras gerado e salvo em: {file_path}"
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"Erro ao gerar o gráfico de barras: {str(e)}"
|
| 37 |
+
|
| 38 |
+
@tool
|
| 39 |
+
def plot_pie_chart(labels: List[str], sizes: List[float], title: str = "Gráfico de Setores") -> str:
|
| 40 |
+
"""
|
| 41 |
+
Gera e salva um gráfico de pizza (setores).
|
| 42 |
+
Args:
|
| 43 |
+
labels: Uma lista de strings para os rótulos de cada fatia.
|
| 44 |
+
sizes: Uma lista de números (int ou float) correspondendo ao tamanho de cada fatia.
|
| 45 |
+
title: O título do gráfico.
|
| 46 |
+
Returns:
|
| 47 |
+
O caminho do arquivo da imagem PNG gerada.
|
| 48 |
+
"""
|
| 49 |
+
try:
|
| 50 |
+
plt.figure(figsize=(8, 8))
|
| 51 |
+
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140, shadow=True)
|
| 52 |
+
plt.title(title)
|
| 53 |
+
plt.axis('equal') # Assegura que o gráfico seja um círculo.
|
| 54 |
+
|
| 55 |
+
file_path = f"pie_chart_{int(time.time())}.png"
|
| 56 |
+
plt.savefig(file_path)
|
| 57 |
+
plt.close()
|
| 58 |
+
|
| 59 |
+
return f"Gráfico de setores gerado e salvo em: {file_path}"
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return f"Erro ao gerar o gráfico de setores: {str(e)}"
|
| 62 |
+
|
| 63 |
+
@tool
|
| 64 |
+
def plot_histogram(data: List[float], bins: int = 10, title: str = "Histograma", x_label: str = "Valores", y_label: str = "Frequência") -> str:
|
| 65 |
+
"""
|
| 66 |
+
Gera e salva um histograma a partir de um conjunto de dados.
|
| 67 |
+
Args:
|
| 68 |
+
data: Uma lista de números (int ou float) para compor o histograma.
|
| 69 |
+
bins: O número de 'caixas' (colunas) no histograma.
|
| 70 |
+
title: O título do gráfico.
|
| 71 |
+
x_label: O rótulo do eixo X.
|
| 72 |
+
y_label: O rótulo do eixo Y.
|
| 73 |
+
Returns:
|
| 74 |
+
O caminho do arquivo da imagem PNG gerada.
|
| 75 |
+
"""
|
| 76 |
+
try:
|
| 77 |
+
plt.figure(figsize=(10, 6))
|
| 78 |
+
plt.hist(data, bins=bins, edgecolor='black')
|
| 79 |
+
plt.title(title)
|
| 80 |
+
plt.xlabel(x_label)
|
| 81 |
+
plt.ylabel(y_label)
|
| 82 |
+
plt.grid(axis='y', linestyle='--', alpha=0.7)
|
| 83 |
+
|
| 84 |
+
file_path = f"histogram_{int(time.time())}.png"
|
| 85 |
+
plt.savefig(file_path)
|
| 86 |
+
plt.close()
|
| 87 |
+
|
| 88 |
+
return f"Histograma gerado e salvo em: {file_path}"
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return f"Erro ao gerar o histograma: {str(e)}"
|