Athmiiii commited on
Commit
c663d92
·
verified ·
1 Parent(s): 617aa69

Update tools/visualize_csv.py

Browse files
Files changed (1) hide show
  1. tools/visualize_csv.py +33 -0
tools/visualize_csv.py CHANGED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import matplotlib.pyplot as plt
3
+ import io
4
+ import base64
5
+ from smolagents import tool
6
+
7
+ @tool
8
+ def visualize_csv(csv_content: str, x_col: str, y_col: str) -> str:
9
+ """
10
+ Generates a plot from CSV content.
11
+ Args:
12
+ csv_content: The raw CSV file content as a string.
13
+ x_col: The name of the column for the x-axis.
14
+ y_col: The name of the column for the y-axis.
15
+ Returns:
16
+ A base64-encoded image tag of the generated plot.
17
+ """
18
+ try:
19
+ df = pd.read_csv(io.StringIO(csv_content))
20
+ plt.figure(figsize=(8, 5))
21
+ plt.plot(df[x_col], df[y_col])
22
+ plt.xlabel(x_col)
23
+ plt.ylabel(y_col)
24
+ plt.title(f"{y_col} vs {x_col}")
25
+
26
+ buf = io.BytesIO()
27
+ plt.savefig(buf, format='png')
28
+ buf.seek(0)
29
+ img_base64 = base64.b64encode(buf.read()).decode('utf-8')
30
+ plt.close()
31
+ return f'<img src="data:image/png;base64,{img_base64}" />'
32
+ except Exception as e:
33
+ return f"Error: {str(e)}"