Spaces:
Sleeping
Sleeping
add filtering tool
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
@@ -33,6 +34,45 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
|
@@ -55,7 +95,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
import pandas as pd
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 36 |
|
| 37 |
+
@tool
|
| 38 |
+
def filter_dataframe(df: pd.DataFrame, filters: dict) -> pd.DataFrame:
|
| 39 |
+
"""
|
| 40 |
+
Filters a DataFrame based on provided keys and their corresponding values.
|
| 41 |
+
|
| 42 |
+
Parameters:
|
| 43 |
+
df (pd.DataFrame): The pandas DataFrame to filter.
|
| 44 |
+
filters (dict): A dictionary where each key is a column name in the DataFrame,
|
| 45 |
+
and the corresponding value is the filter criteria. The filter
|
| 46 |
+
value can be a single value for an equality check, or a list of
|
| 47 |
+
values to use with the 'isin' filter.
|
| 48 |
+
|
| 49 |
+
Example:
|
| 50 |
+
filters = {
|
| 51 |
+
"name": "Alice", # filter where 'name' == "Alice"
|
| 52 |
+
"city": ["New York", "Boston"] # filter where 'city' is either "New York" or "Boston"
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
Returns:
|
| 56 |
+
pd.DataFrame: A DataFrame filtered based on the provided criteria.
|
| 57 |
+
|
| 58 |
+
Raises:
|
| 59 |
+
ValueError: If any of the provided filter keys are not present in the DataFrame.
|
| 60 |
+
"""
|
| 61 |
+
filtered_df = df.copy()
|
| 62 |
+
|
| 63 |
+
for column, value in filters.items():
|
| 64 |
+
if column not in filtered_df.columns:
|
| 65 |
+
raise ValueError(f"Column '{column}' does not exist in the DataFrame.")
|
| 66 |
+
|
| 67 |
+
if isinstance(value, list):
|
| 68 |
+
# Filter for any value in the list (like SQL IN)
|
| 69 |
+
filtered_df = filtered_df[filtered_df[column].isin(value)]
|
| 70 |
+
else:
|
| 71 |
+
# Filter for equality
|
| 72 |
+
filtered_df = filtered_df[filtered_df[column] == value]
|
| 73 |
+
|
| 74 |
+
return filtered_df
|
| 75 |
+
|
| 76 |
|
| 77 |
final_answer = FinalAnswerTool()
|
| 78 |
|
|
|
|
| 95 |
|
| 96 |
agent = CodeAgent(
|
| 97 |
model=model,
|
| 98 |
+
tools=[final_answer, get_current_time_in_timezone, filter_dataframe], ## add your tools here (don't remove final answer)
|
| 99 |
max_steps=6,
|
| 100 |
verbosity_level=1,
|
| 101 |
grammar=None,
|