Nioi's picture
rebuild
726d2d9
raw
history blame
1.15 kB
from smolagents import tool
import urllib
import pandas as pd
@tool
def download_file_from_url(url: str) -> str:
"""
Download a file from a URL and save it to a temporary location.
Args:
url: the URL of the file to download.
"""
file_path = None
try:
result = urllib.request.urlretrieve(url)
file_path = result[0]
except Exception as e:
return f"Error downloading file: {str(e)}"
return file_path
@tool
def csv_reader(file_path: str) -> str:
"""
Extract CSV file content and return it in a json format. Supported file extensions: .csv
Args:
file_path: the path to the CSV file.
"""
try:
df = pd.read_csv(file_path)
return df.to_json()
except Exception as e:
return f"Error analyzing CSV file: {str(e)}"
@tool
def excel_reader(file_path: str) -> str:
"""
Extract Excel file content and return it in a json format. Supported file extensions: .xls, .xlsx, .xlsb, .xlsm, .odf, .ods, .odt
Args:
file_path: the path to the Excel file.
"""
try:
df = pd.read_excel(file_path)
return df.to_json()
except Exception as e:
return f"Error analyzing Excel file: {str(e)}"