Download tools/excel_analysis_tool.py from Punisher2444/Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 1.8 kB
-
https://huggingface.co/Punisher2444/Final_Assignment_Template/resolve/main/tools/excel_analysis_tool.py
- Command line
-
hf download hf://Punisher2444/Final_Assignment_Template/tools/excel_analysis_tool.py
-
curl -L -o excel_analysis_tool.py https://huggingface.co/Punisher2444/Final_Assignment_Template/resolve/main/tools/excel_analysis_tool.py
1.8 kB
| 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)}" | |