Spaces:
Running
Running
Update agent.py
Browse files
agent.py
CHANGED
@@ -2,6 +2,7 @@ from smolagents import CodeAgent, LiteLLMModel, tool, load_tool, DuckDuckGoSear
|
|
2 |
import asyncio
|
3 |
import os
|
4 |
import re
|
|
|
5 |
from typing import Optional
|
6 |
from token_bucket import Limiter
|
7 |
import yaml
|
@@ -92,6 +93,64 @@ def SpeechToTextTool(audio_path: str) -> str:
|
|
92 |
result = model.transcribe(audio_path)
|
93 |
return result.get("text", "")
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
#@tool
|
97 |
#class LocalFileAudioTool:
|
@@ -130,10 +189,12 @@ class MagAgent:
|
|
130 |
WikipediaSearchTool(),
|
131 |
# ImageAnalysisTool,
|
132 |
SpeechToTextTool
|
|
|
133 |
# LocalFileAudioTool()
|
134 |
],
|
135 |
verbosity_level=2,
|
136 |
-
|
|
|
137 |
)
|
138 |
print("MagAgent initialized.")
|
139 |
|
|
|
2 |
import asyncio
|
3 |
import os
|
4 |
import re
|
5 |
+
import pandas as pd
|
6 |
from typing import Optional
|
7 |
from token_bucket import Limiter
|
8 |
import yaml
|
|
|
93 |
result = model.transcribe(audio_path)
|
94 |
return result.get("text", "")
|
95 |
|
96 |
+
class ExcelReaderTool(Tool):
|
97 |
+
name = "excel_reader"
|
98 |
+
description = """
|
99 |
+
This tool reads and processes Excel files (.xlsx, .xls).
|
100 |
+
It can extract data, calculate statistics, and perform data analysis on spreadsheets.
|
101 |
+
"""
|
102 |
+
inputs = {
|
103 |
+
"excel_path": {
|
104 |
+
"type": "string",
|
105 |
+
"description": "The path to the Excel file to read",
|
106 |
+
},
|
107 |
+
"sheet_name": {
|
108 |
+
"type": "string",
|
109 |
+
"description": "The name of the sheet to read (optional, defaults to first sheet)",
|
110 |
+
"nullable": True
|
111 |
+
}
|
112 |
+
}
|
113 |
+
output_type = "string"
|
114 |
+
|
115 |
+
def forward(self, excel_path: str, sheet_name: str = None) -> str:
|
116 |
+
"""
|
117 |
+
Reads and processes the given Excel file.
|
118 |
+
"""
|
119 |
+
try:
|
120 |
+
# Check if the file exists
|
121 |
+
if not os.path.exists(excel_path):
|
122 |
+
return f"Error: Excel file not found at {excel_path}"
|
123 |
+
|
124 |
+
import pandas as pd
|
125 |
+
|
126 |
+
# Read the Excel file
|
127 |
+
if sheet_name:
|
128 |
+
df = pd.read_excel(excel_path, sheet_name=sheet_name)
|
129 |
+
else:
|
130 |
+
df = pd.read_excel(excel_path)
|
131 |
+
|
132 |
+
# Get basic info about the data
|
133 |
+
info = {
|
134 |
+
"shape": df.shape,
|
135 |
+
"columns": list(df.columns),
|
136 |
+
"dtypes": df.dtypes.to_dict(),
|
137 |
+
"head": df.head(5).to_dict()
|
138 |
+
}
|
139 |
+
|
140 |
+
# Return formatted info
|
141 |
+
result = f"Excel file: {excel_path}\n"
|
142 |
+
result += f"Shape: {info['shape'][0]} rows × {info['shape'][1]} columns\n\n"
|
143 |
+
result += "Columns:\n"
|
144 |
+
for col in info['columns']:
|
145 |
+
result += f"- {col} ({info['dtypes'].get(col)})\n"
|
146 |
+
|
147 |
+
result += "\nPreview (first 5 rows):\n"
|
148 |
+
result += df.head(5).to_string()
|
149 |
+
|
150 |
+
return result
|
151 |
+
|
152 |
+
except Exception as e:
|
153 |
+
return f"Error reading Excel file: {str(e)}"
|
154 |
|
155 |
#@tool
|
156 |
#class LocalFileAudioTool:
|
|
|
189 |
WikipediaSearchTool(),
|
190 |
# ImageAnalysisTool,
|
191 |
SpeechToTextTool
|
192 |
+
ExcelReaderTool(),
|
193 |
# LocalFileAudioTool()
|
194 |
],
|
195 |
verbosity_level=2,
|
196 |
+
add_base_tools=True,
|
197 |
+
max_steps=20
|
198 |
)
|
199 |
print("MagAgent initialized.")
|
200 |
|