Spaces:
Configuration error
Configuration error
Commit
·
d4c21c3
1
Parent(s):
89f0b01
minor code restructuration
Browse files- custom_tools.py +11 -39
- utils.py +3 -0
custom_tools.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from utils import download_file, read_file, download_yt_video, extract_frames, encode_image, analyze_frame, generate_prompt_for_video_frame_analysis, get_response_from_frames_analysis, transcript_audio_file
|
| 2 |
|
| 3 |
import os
|
| 4 |
import requests
|
|
@@ -125,7 +125,7 @@ def add_numbers(numbers_list: List[float]) -> Union[float, str]:
|
|
| 125 |
|
| 126 |
|
| 127 |
@tool
|
| 128 |
-
def
|
| 129 |
"""
|
| 130 |
Sum the values of specified columns in a pandas DataFrame read from an Excel file.
|
| 131 |
|
|
@@ -138,53 +138,25 @@ def sum_pandas_df(task_id: str, file_name: str, column_names: List[str]) -> floa
|
|
| 138 |
float: The sum of the specified columns.
|
| 139 |
|
| 140 |
Example:
|
| 141 |
-
|
| 142 |
"""
|
| 143 |
-
|
| 144 |
-
download_file(task_id, file_name)
|
| 145 |
-
except Exception as e:
|
| 146 |
-
return f"Error downloading file: {e}"
|
| 147 |
|
| 148 |
if not os.path.exists(file_name):
|
| 149 |
return f"File {file_name} does not exist."
|
| 150 |
|
| 151 |
-
|
| 152 |
-
extension = os.path.splitext(file_name)[1].lower()
|
| 153 |
-
except Exception as e:
|
| 154 |
-
return f"Error parsing file extension: {e}"
|
| 155 |
|
| 156 |
if extension not in ['.csv', '.xlsx']:
|
| 157 |
return "Unsupported file format. Please provide a CSV or XLSX file."
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
df = pd.read_excel(file_name)
|
| 164 |
-
else:
|
| 165 |
-
return "Unsupported file format. Please provide a CSV or XLSX file."
|
| 166 |
-
except FileNotFoundError:
|
| 167 |
-
return f"File {file_name} not found."
|
| 168 |
-
except pd.errors.EmptyDataError:
|
| 169 |
-
return "The file is empty."
|
| 170 |
-
except Exception as e:
|
| 171 |
-
return f"Error reading file: {e}"
|
| 172 |
-
|
| 173 |
-
if df.empty:
|
| 174 |
-
return "The DataFrame is empty."
|
| 175 |
-
|
| 176 |
-
if not isinstance(column_names, list):
|
| 177 |
-
return "column_names should be a list of column names."
|
| 178 |
-
|
| 179 |
-
if not column_names:
|
| 180 |
-
return "No columns specified to sum."
|
| 181 |
-
|
| 182 |
-
missing_columns = [col for col in column_names if col not in df.columns]
|
| 183 |
-
if missing_columns:
|
| 184 |
-
return f"The following columns do not exist in the DataFrame: {missing_columns}"
|
| 185 |
|
| 186 |
try:
|
| 187 |
-
total_sum = df
|
| 188 |
return total_sum
|
| 189 |
except Exception as e:
|
| 190 |
return f"Error summing columns: {e}"
|
|
@@ -353,7 +325,7 @@ custom_tools = [
|
|
| 353 |
wiki_search,
|
| 354 |
DuckDuckGoSearchResults(),
|
| 355 |
# add_numbers,
|
| 356 |
-
|
| 357 |
youtube_transcript,
|
| 358 |
analyse_youtube_video,
|
| 359 |
analyze_image,
|
|
|
|
| 1 |
+
from utils import download_file, read_file, sum_pandas_df_cols, download_yt_video, extract_frames, encode_image, analyze_frame, generate_prompt_for_video_frame_analysis, get_response_from_frames_analysis, transcript_audio_file
|
| 2 |
|
| 3 |
import os
|
| 4 |
import requests
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
@tool
|
| 128 |
+
def sum_excel_cols(task_id: str, file_name: str, column_names: List[str]) -> float:
|
| 129 |
"""
|
| 130 |
Sum the values of specified columns in a pandas DataFrame read from an Excel file.
|
| 131 |
|
|
|
|
| 138 |
float: The sum of the specified columns.
|
| 139 |
|
| 140 |
Example:
|
| 141 |
+
sum_excel_cols("task123", "data.xlsx", ["Column1", "Column2"]) -> 100.0
|
| 142 |
"""
|
| 143 |
+
file_status = download_file(task_id, file_name)
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
if not os.path.exists(file_name):
|
| 146 |
return f"File {file_name} does not exist."
|
| 147 |
|
| 148 |
+
extension = os.path.splitext(file_name)[1].lower()
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
if extension not in ['.csv', '.xlsx']:
|
| 151 |
return "Unsupported file format. Please provide a CSV or XLSX file."
|
| 152 |
|
| 153 |
+
if extension == '.csv':
|
| 154 |
+
df = pd.read_csv(file_name)
|
| 155 |
+
elif extension == '.xlsx':
|
| 156 |
+
df = pd.read_excel(file_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
try:
|
| 159 |
+
total_sum = sum_pandas_df_cols(df, column_names)
|
| 160 |
return total_sum
|
| 161 |
except Exception as e:
|
| 162 |
return f"Error summing columns: {e}"
|
|
|
|
| 325 |
wiki_search,
|
| 326 |
DuckDuckGoSearchResults(),
|
| 327 |
# add_numbers,
|
| 328 |
+
sum_excel_cols,
|
| 329 |
youtube_transcript,
|
| 330 |
analyse_youtube_video,
|
| 331 |
analyze_image,
|
utils.py
CHANGED
|
@@ -99,6 +99,9 @@ def read_file(file_name: str) -> str:
|
|
| 99 |
return f"Permission denied: Unable to access the file at {file_name}."
|
| 100 |
except Exception as e:
|
| 101 |
return f"An unexpected error occurred: {e}"
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
def download_yt_video(url: str) -> str:
|
| 104 |
"""
|
|
|
|
| 99 |
return f"Permission denied: Unable to access the file at {file_name}."
|
| 100 |
except Exception as e:
|
| 101 |
return f"An unexpected error occurred: {e}"
|
| 102 |
+
|
| 103 |
+
def sum_pandas_df_cols(df: pd.DataFrame, cols: list):
|
| 104 |
+
return df[cols].sum().sum()
|
| 105 |
|
| 106 |
def download_yt_video(url: str) -> str:
|
| 107 |
"""
|