Sonu313131 commited on
Commit
9651da6
·
verified ·
1 Parent(s): a9a3c73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -56
app.py CHANGED
@@ -16,46 +16,42 @@ openai_key = os.environ.get("OPENAI_API_KEY")
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:
@@ -64,48 +60,57 @@ class ExcelAnalysisTool:
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()
@@ -118,7 +123,7 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
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
123
  ),
124
  max_steps=20,
 
16
  search_tool = DuckDuckGoSearchTool()
17
 
18
  ##Tool 2
19
+ from smolagents import Tool
20
+ from huggingface_hub import hf_hub_download
21
+ import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ class ExcelAnalysisTool(Tool):
24
+ name = "excel_analysis"
25
+ description = (
26
+ "Loads an Excel file from the GAIA dataset on Hugging Face and calculates "
27
+ "the total sales for items labeled as 'food', excluding drinks. "
28
+ "Provide input as a string with the filename, e.g., 'sales_data.xlsx'."
29
+ )
30
+
31
+ inputs = {
32
+ "filename": {
33
+ "type": "string",
34
+ "description": "The name of the Excel file (e.g., 'sales_data.xlsx')"
35
+ }
36
+ }
37
+
38
+ output_type = "string"
39
+ repo_id = "gaia-benchmark/GAIA"
40
+
41
+ def forward(self, filename: str) -> str:
42
  try:
 
43
  file_path = hf_hub_download(
44
  repo_id=self.repo_id,
45
  filename=filename,
46
  repo_type="dataset"
47
  )
 
 
48
  df = pd.read_excel(file_path)
 
 
49
  food_sales = df[
50
  (df['category'].str.lower() == 'food') &
51
  (df['item'].str.lower() != 'drinks')
52
  ]
 
53
  total_sales = food_sales['sales'].sum()
54
  return f"Total sales for food items: ${total_sales:.2f}"
 
55
  except FileNotFoundError:
56
  return "Error: The specified file was not found."
57
  except KeyError as e:
 
60
  return f"An unexpected error occurred: {str(e)}"
61
 
62
  ##Tool 3
63
+ import wikipedia
64
+ from smolagents import Tool
65
+
66
+ class WikiTool(Tool):
67
+ name = "wiki_tool"
68
+ description = (
69
+ "Performs Wikipedia lookups. Actions supported: 'summary' and 'is_historical_country'."
70
+ )
71
+
72
+ inputs = {
73
+ "action": {
74
+ "type": "string",
75
+ "description": "The action to perform: 'summary' or 'is_historical_country'"
76
+ },
77
+ "topic": {
78
+ "type": "string",
79
+ "description": "The topic or country name to look up"
80
+ }
81
+ }
82
+
83
+ output_type = "string"
84
+
85
+ def forward(self, action: str, topic: str) -> str:
86
+ if action == "summary":
87
+ return self.fetch_summary(topic)
88
+ elif action == "is_historical_country":
89
+ return self.is_historical_country(topic)
90
+ else:
91
+ return "Error: Unknown action. Use 'summary' or 'is_historical_country'."
92
 
93
  def fetch_summary(self, topic: str) -> str:
94
  try:
95
+ return wikipedia.summary(topic, sentences=3)
 
96
  except wikipedia.DisambiguationError as e:
97
+ return f"Disambiguation: {e.options[:5]}"
98
  except wikipedia.PageError:
99
+ return "No page found."
100
  except Exception as e:
101
+ return f"Unexpected error: {str(e)}"
102
 
103
+ def is_historical_country(self, topic: str) -> str:
104
  try:
105
+ summary = wikipedia.summary(topic, sentences=2).lower()
106
  keywords = [
107
  "former country", "no longer exists", "historical country",
108
  "was a country", "defunct", "dissolved", "existed until",
109
  "disestablished", "merged into"
110
  ]
111
+ return "yes" if any(k in summary for k in keywords) else "no"
112
+ except:
113
+ return "no"
 
 
 
 
 
 
 
 
 
 
114
 
115
  wiki_tool = WikiTool()
116
  excel_tool = ExcelAnalysisTool()
 
123
  agent = ToolCallingAgent(
124
  tools=[search_tool, wiki_tool, excel_tool],
125
  model=OpenAIServerModel(
126
+ model_id="gpt-4o", # ✅ valid OpenAI model name
127
  api_key=os.environ["OPENAI_API_KEY"] # ✅ securely load from environment
128
  ),
129
  max_steps=20,