Update app.py
Browse files
app.py
CHANGED
@@ -15,13 +15,108 @@ openai_key = os.environ.get("OPENAI_API_KEY")
|
|
15 |
|
16 |
search_tool = DuckDuckGoSearchTool()
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
19 |
log_output = ""
|
20 |
|
21 |
try:
|
22 |
|
23 |
agent = ToolCallingAgent(
|
24 |
-
tools=[search_tool],
|
25 |
model=OpenAIServerModel(
|
26 |
model_id="gpt-4.1", # ✅ valid OpenAI model name
|
27 |
api_key=os.environ["OPENAI_API_KEY"] # ✅ securely load from environment
|
|
|
15 |
|
16 |
search_tool = DuckDuckGoSearchTool()
|
17 |
|
18 |
+
##Tool 2
|
19 |
+
class ExcelAnalysisTool:
|
20 |
+
def __init__(self):
|
21 |
+
self.name = "ExcelAnalysisTool"
|
22 |
+
self.description = (
|
23 |
+
"Loads an Excel file from the GAIA dataset on Hugging Face and calculates "
|
24 |
+
"the total sales for items labeled as 'food', excluding drinks. "
|
25 |
+
"Provide input as a string with the filename, e.g., 'sales_data.xlsx'."
|
26 |
+
)
|
27 |
+
self.repo_id = "gaia-benchmark/GAIA"
|
28 |
+
|
29 |
+
def __call__(self, filename: str) -> str:
|
30 |
+
"""
|
31 |
+
Loads and processes the Excel file.
|
32 |
+
|
33 |
+
Args:
|
34 |
+
filename (str): The name of the Excel file (e.g., 'sales_data.xlsx').
|
35 |
+
|
36 |
+
Returns:
|
37 |
+
str: Total food sales in USD, or an error message.
|
38 |
+
"""
|
39 |
+
try:
|
40 |
+
# Download the file from Hugging Face Hub
|
41 |
+
file_path = hf_hub_download(
|
42 |
+
repo_id=self.repo_id,
|
43 |
+
filename=filename,
|
44 |
+
repo_type="dataset"
|
45 |
+
)
|
46 |
+
|
47 |
+
# Load the Excel file into a DataFrame
|
48 |
+
df = pd.read_excel(file_path)
|
49 |
+
|
50 |
+
# Filter rows: category == 'food' and item != 'drinks'
|
51 |
+
food_sales = df[
|
52 |
+
(df['category'].str.lower() == 'food') &
|
53 |
+
(df['item'].str.lower() != 'drinks')
|
54 |
+
]
|
55 |
+
|
56 |
+
total_sales = food_sales['sales'].sum()
|
57 |
+
return f"Total sales for food items: ${total_sales:.2f}"
|
58 |
+
|
59 |
+
except FileNotFoundError:
|
60 |
+
return "Error: The specified file was not found."
|
61 |
+
except KeyError as e:
|
62 |
+
return f"Error: Missing expected column in the Excel file: {str(e)}"
|
63 |
+
except Exception as e:
|
64 |
+
return f"An unexpected error occurred: {str(e)}"
|
65 |
+
|
66 |
+
##Tool 3
|
67 |
+
class WikiTool:
|
68 |
+
def __init__(self):
|
69 |
+
self.name = "WikiTool"
|
70 |
+
self.description = (
|
71 |
+
"Performs Wikipedia lookups. Supports actions: 'summary' and 'is_historical_country'.\n"
|
72 |
+
"Usage:\n"
|
73 |
+
"- {'action': 'summary', 'topic': 'France'}\n"
|
74 |
+
"- {'action': 'is_historical_country', 'country_name': 'Yugoslavia'}"
|
75 |
+
)
|
76 |
+
|
77 |
+
def fetch_summary(self, topic: str) -> str:
|
78 |
+
try:
|
79 |
+
summary = wikipedia.summary(topic, sentences=3)
|
80 |
+
return summary
|
81 |
+
except wikipedia.DisambiguationError as e:
|
82 |
+
return f"Disambiguation error: The topic '{topic}' is ambiguous. Suggestions: {e.options[:5]}"
|
83 |
+
except wikipedia.PageError:
|
84 |
+
return f"Page error: The topic '{topic}' does not exist on Wikipedia."
|
85 |
+
except Exception as e:
|
86 |
+
return f"An unexpected error occurred: {str(e)}"
|
87 |
+
|
88 |
+
def is_historical_country(self, country_name: str) -> bool:
|
89 |
+
try:
|
90 |
+
summary = wikipedia.summary(country_name, sentences=2).lower()
|
91 |
+
keywords = [
|
92 |
+
"former country", "no longer exists", "historical country",
|
93 |
+
"was a country", "defunct", "dissolved", "existed until",
|
94 |
+
"disestablished", "merged into"
|
95 |
+
]
|
96 |
+
return any(keyword in summary for keyword in keywords)
|
97 |
+
except Exception:
|
98 |
+
return False
|
99 |
+
|
100 |
+
def __call__(self, args: dict):
|
101 |
+
action = args.get("action")
|
102 |
+
|
103 |
+
if action == "summary":
|
104 |
+
return self.fetch_summary(args.get("topic", ""))
|
105 |
+
elif action == "is_historical_country":
|
106 |
+
return self.is_historical_country(args.get("country_name", ""))
|
107 |
+
else:
|
108 |
+
return "Error: Unknown action. Use 'summary' or 'is_historical_country'."
|
109 |
+
|
110 |
+
wiki_tool = WikiTool()
|
111 |
+
excel_tool = ExcelAnalysisTool()
|
112 |
+
|
113 |
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
114 |
log_output = ""
|
115 |
|
116 |
try:
|
117 |
|
118 |
agent = ToolCallingAgent(
|
119 |
+
tools=[search_tool, wiki_tool, excel_tool],
|
120 |
model=OpenAIServerModel(
|
121 |
model_id="gpt-4.1", # ✅ valid OpenAI model name
|
122 |
api_key=os.environ["OPENAI_API_KEY"] # ✅ securely load from environment
|