File size: 2,381 Bytes
9073c71 03dd32d 9073c71 03dd32d 056e59a 03dd32d 056e59a 03dd32d 056e59a 03dd32d 056e59a 03dd32d 056e59a 03dd32d 056e59a |
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 |
from smolagents.tools import Tool
import pandas as pd
from huggingface_hub import hf_hub_download
class ExcelAnalysisTool(Tool):
def __init__(self):
self.name = "ExcelAnalysisTool"
self.description = (
"Loads an Excel file from the GAIA dataset on Hugging Face and calculates "
"the total sales for items labeled as 'food', excluding drinks. "
"Provide input as a string with the filename, e.g., 'sales_data.xlsx'."
)
self.inputs = {
"filename": {
"type": "string",
"description": "The name of the Excel file to process (e.g., 'sales_data.xlsx').",
"nullable": True # Сделать filename необязательным
}
}
self.output_type = "string"
self.repo_id = "gaia-benchmark/GAIA"
super().__init__(
name=self.name,
description=self.description,
inputs=self.inputs,
output_type=self.output_type
)
def forward(self, filename: str = "") -> str:
"""
Loads and processes the Excel file.
Args:
filename (str): The name of the Excel file (e.g., 'sales_data.xlsx').
Returns:
str: Total food sales in USD, or an error message.
"""
if not filename:
return "Error: 'filename' is required."
try:
# Download the file from Hugging Face Hub
file_path = hf_hub_download(
repo_id=self.repo_id,
filename=filename,
repo_type="dataset"
)
# Load the Excel file into a DataFrame
df = pd.read_excel(file_path)
# Filter rows: category == 'food' and item != 'drinks'
food_sales = df[
(df['category'].str.lower() == 'food') &
(df['item'].str.lower() != 'drinks')
]
total_sales = food_sales['sales'].sum()
return f"Total sales for food items: ${total_sales:.2f}"
except FileNotFoundError:
return "Error: The specified file was not found."
except KeyError as e:
return f"Error: Missing expected column in the Excel file: {str(e)}"
except Exception as e:
return f"An unexpected error occurred: {str(e)}" |