Final_Assignment_Template / tools /excel_analysis_tool.py
Punisher2444's picture
Upload folder using huggingface_hub
6073dc2 verified
import pandas as pd
from huggingface_hub import hf_hub_download
class ExcelAnalysisTool:
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.repo_id = "gaia-benchmark/GAIA"
def __call__(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.
"""
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)}"