Sonu313131 commited on
Commit
af65316
·
verified ·
1 Parent(s): b7e5eff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -77
app.py CHANGED
@@ -15,105 +15,66 @@ openai_key = os.environ.get("OPENAI_API_KEY")
15
 
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:
58
- return f"Error: Missing expected column in the Excel file: {str(e)}"
59
- except Exception 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()
117
 
118
  async def run_and_submit_all(profile: gr.OAuthProfile | None):
119
  log_output = ""
@@ -121,7 +82,7 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
121
  try:
122
 
123
  agent = ToolCallingAgent(
124
- tools=[search_tool, wiki_tool, excel_tool],
125
  model=OpenAIServerModel(
126
  model_id="gpt-4o", # ✅ valid OpenAI model name
127
  temperature=0.0,
 
15
 
16
  search_tool = DuckDuckGoSearchTool()
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ ##Tool 2
20
  import wikipedia
21
  from smolagents import Tool
22
+ from smolagents.models import InferenceClientModel
23
 
24
+ class WikipediaQATool(Tool):
25
+ name = "wikipedia_qa"
26
  description = (
27
+ "Searches Wikipedia for a topic, reads its content, and answers the input question "
28
+ "based on the content of the Wikipedia page."
29
  )
30
 
31
  inputs = {
32
+ "question": {
33
  "type": "string",
34
+ "description": "The question that should be answered using Wikipedia."
35
  },
36
  "topic": {
37
  "type": "string",
38
+ "description": "The topic to search for on Wikipedia."
39
  }
40
  }
41
 
42
  output_type = "string"
43
 
44
+ def __init__(self, model=None):
45
+ super().__init__()
46
+ self.model = model or InferenceClientModel(
47
+ model="mistralai/Magistral-Small-2506", provider="featherless-ai"
48
+ )
 
 
49
 
50
+ def forward(self, question: str, topic: str) -> str:
51
  try:
52
+ page = wikipedia.page(topic)
53
+ content = page.content[:2000] # Limit for context
54
+
55
+ # Build QA prompt
56
+ prompt = (
57
+ f"You are a Wikipedia expert. Based only on the following content from the Wikipedia page on '{topic}', "
58
+ f"answer the question briefly and factually.\n\n"
59
+ f"=== Wikipedia Content ===\n{content}\n\n"
60
+ f"=== Question ===\n{question}\n\n"
61
+ f"Answer in a single line. Avoid any extra explanation.\n"
62
+ f"FINAL ANSWER:"
63
+ )
64
+
65
+ response = self.model(prompt)
66
+ return response.strip()
67
+
68
  except wikipedia.DisambiguationError as e:
69
+ return f"Disambiguation error: multiple results found: {', '.join(e.options[:5])}"
70
  except wikipedia.PageError:
71
+ return "Wikipedia page not found."
72
  except Exception as e:
73
+ return f"Error while retrieving Wikipedia content: {str(e)}"
74
 
75
+
76
+ wiki_tool = WikipediaQAToolTool()
77
+ #excel_tool = ExcelAnalysisTool()
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  async def run_and_submit_all(profile: gr.OAuthProfile | None):
80
  log_output = ""
 
82
  try:
83
 
84
  agent = ToolCallingAgent(
85
+ tools=[search_tool, wiki_tool],
86
  model=OpenAIServerModel(
87
  model_id="gpt-4o", # ✅ valid OpenAI model name
88
  temperature=0.0,