Download tools/files.py from andrei-sferdian/Agents_Course_Final_Assignment: direct link, hf CLI and curl.
- Browser
- Download file 4.19 kB
-
https://huggingface.co/spaces/andrei-sferdian/Agents_Course_Final_Assignment/resolve/main/tools/files.py
- Command line
-
hf download hf://spaces/andrei-sferdian/Agents_Course_Final_Assignment/tools/files.py
-
curl -L -o files.py https://huggingface.co/spaces/andrei-sferdian/Agents_Course_Final_Assignment/resolve/main/tools/files.py
4.19 kB
| from langchain_core.tools import tool | |
| def get_text_file(file_name: str) -> str: | |
| """ | |
| Loads a text file by its name and returns its content as a string. | |
| Args: | |
| file_name (str): The name of the text file to load. The file name should include | |
| a task ID followed by a file extension (e.g., "12345.txt"). | |
| Returns: | |
| str: The content of the text file as a string. | |
| """ | |
| print(f"Loading text file: {file_name}") | |
| try: | |
| import questions | |
| task_id, ext = file_name.rsplit(".", 1) | |
| if not task_id or not ext: | |
| return "Invalid file name format. Please provide a valid file name with task ID and extension." | |
| file_content = questions.get_file(task_id) | |
| if not file_content: | |
| return f"File with task ID {task_id} not found." | |
| return file_content.decode("utf-8") | |
| except Exception as e: | |
| return f"Error loading text file: {e}" | |
| def load_excel_file(file_name: str) -> str: | |
| """ | |
| Loads an Excel file by its name and returns its content as a string. | |
| Args: | |
| file_name (str): The name of the Excel file to load. The file name should include | |
| a task ID followed by a file extension (e.g., "12345.xlsx"). | |
| Returns: | |
| str: The content of the Excel file as a string. | |
| """ | |
| print(f"Loading Excel file: {file_name}") | |
| try: | |
| from io import BytesIO | |
| import pandas as pd | |
| import questions | |
| task_id, ext = file_name.rsplit(".", 1) | |
| if not task_id or not ext: | |
| return "Invalid file name format. Please provide a valid file name with task ID and extension." | |
| if ext.lower() not in ["xlsx", "xls"]: | |
| return "Invalid file format. Please provide an Excel file." | |
| file_bytes = questions.get_file(task_id) | |
| if not file_bytes: | |
| return f"File with task ID {task_id} not found." | |
| data = pd.read_excel(BytesIO(file_bytes)) | |
| print(f"Data shape: {data.shape}") | |
| print(f"Data columns: {data.columns.tolist()}") | |
| print(f"Data preview:\n{data.head()}") | |
| return data.to_markdown(index=False) | |
| except Exception as e: | |
| return f"Error loading Excel file: {e}" | |
| def sum_rows_cols(file_name: str, rows: list[str], cols: list[str]) -> float | str: | |
| """ | |
| Sums all data from the specified rows and columns in an Excel file. | |
| Args: | |
| file_name (str): The name of the Excel file to load. The file name should include | |
| a task ID followed by a file extension (e.g., "12345.xlsx"). | |
| rows (list[str]): List of row labels to sum. These should match the index labels in the Excel file. | |
| cols (list[str]): List of column labels to sum. These should match the column headers in the Excel file. | |
| Returns: | |
| float | str: The total sum of the specified rows and columns, or an error message if the operation fails. | |
| """ | |
| try: | |
| from io import BytesIO | |
| import pandas as pd | |
| import questions | |
| task_id, ext = file_name.rsplit(".", 1) | |
| if not task_id or not ext or ext.lower() not in ["xlsx", "xls"]: | |
| return "Invalid file name format. Please provide a valid Excel file." | |
| file_bytes = questions.get_file(task_id) | |
| if not file_bytes: | |
| return f"File with task ID {task_id} not found." | |
| data = pd.read_excel(BytesIO(file_bytes), index_col=0) | |
| print(f"Data shape: {data.shape}") | |
| print(f"Data columns: {data.columns.tolist()}") | |
| # Ensure rows and cols are in the DataFrame | |
| if not all(row in data.index for row in rows): | |
| return f"Some specified rows {rows} are not found in the data." | |
| if not all(col in data.columns for col in cols): | |
| return f"Some specified columns {cols} are not found in the data." | |
| # Sum all data from the specified rows and columns | |
| total_sum = data.loc[rows, cols].sum().sum() | |
| return total_sum | |
| except Exception as e: | |
| return f"Error processing Excel file: {e}" | |