Vinsmart06 commited on
Commit
ea667ff
·
verified ·
1 Parent(s): ed898b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -59
app.py CHANGED
@@ -123,20 +123,20 @@ class BasicAgent:
123
  # --- Robust Wikipedia Tool ---
124
  def wiki_search(self, query):
125
  try:
126
- # Step 1: Search for the most relevant page title
127
- search_url = f"https://en.wikipedia.org{query}&format=json"
128
- r = requests.get(search_url).json()
 
129
  if not r.get("query", {}).get("search"):
130
- return "No Wikipedia page found for this query."
131
 
132
  title = r["query"]["search"][0]["title"]
133
-
134
- # Step 2: Get the summary of that specific page
135
  summary_url = f"https://en.wikipedia.org{title.replace(' ', '_')}"
136
- page = requests.get(summary_url).json()
137
- return page.get("extract", "Summary not available.")
138
- except Exception as e:
139
- return f"Wiki error: {str(e)}"
140
 
141
  def youtube_captions(self, url):
142
  try:
@@ -148,21 +148,36 @@ class BasicAgent:
148
  except Exception as e:
149
  return f"YouTube error: {e}"
150
 
151
- def execute_tool(self, tool, input_data, file_url):
152
- # 1. Handle tools that need the provided file_url
153
- if tool in ["read_excel", "read_image"]:
154
- target_url = file_url if file_url else input_data
155
- local_file = self.download_file(target_url)
156
- if not local_file: return "Error: Could not download file. Check if file_url is valid."
 
 
157
 
158
- if tool == "read_excel":
159
- try:
160
- return pd.read_excel(local_file).to_string()[:5000]
161
- except: return "Excel read error."
162
- if tool == "read_image":
163
- try:
164
- return pytesseract.image_to_string(Image.open(local_file))
165
- except: return "OCR read error."
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
  # 2. Handle web/search tools
168
  if tool == "wiki_search":
@@ -183,58 +198,49 @@ class BasicAgent:
183
 
184
  return "Unknown tool"
185
 
186
- def agent_loop(self, question, file_url=None):
187
  memory = ""
188
- # Provide the file_url explicitly in the prompt so the agent knows it exists
189
- file_info = f"\nNote: A file is available for this task at: {file_url}" if file_url else ""
190
 
191
  for step in range(5):
192
- prompt = f"""You are a GAIA solving agent.
193
- Available tools: read_excel, read_image, scrape_page, youtube_captions, calculator, wiki_search.
194
- {file_info}
195
 
196
  Question: {question}
 
197
 
198
- Current History:
199
- {memory}
200
-
201
- Instructions:
202
- 1. If you need to read the provided file, use TOOL: read_excel or TOOL: read_image with INPUT: {file_url if file_url else 'none'}.
203
- 2. Give a FINAL answer as soon as you have enough information.
204
-
205
- Format:
206
- TOOL: tool_name
207
- INPUT: tool_input
208
- OR
209
- FINAL: your_precise_answer"""
210
 
211
  response = self.client.chat.completions.create(
212
  model="gpt-4o-mini",
213
- temperature=0,
214
- messages=[{"role": "system", "content": "You are a helpful assistant that uses tools to solve tasks."},
215
  {"role": "user", "content": prompt}]
216
  )
217
 
218
- action = response.choices[0].message.content.strip()
219
- print(f"Agent step: {action}")
220
 
221
- if "FINAL:" in action:
222
- # Extract only the content after FINAL:
223
- return action.split("FINAL:")[-1].strip()
224
 
225
- if "TOOL:" in action:
226
  try:
227
- tool_part = action.split("TOOL:")[1].split("\n")[0].strip()
228
- input_part = action.split("INPUT:")[1].split("\n")[0].strip()
229
- result = self.execute_tool(tool_part, input_part, file_url)
230
- memory += f"\nStep {step}: Tool {tool_part} returned: {result[:1000]}"
231
- except Exception as e:
232
- memory += f"\nStep {step}: Tool call error: {str(e)}"
233
  else:
234
- # If the agent just talks without a tool or FINAL, treat as text
235
- memory += f"\nStep {step}: {action}"
236
 
237
- return "No answer found"
238
 
239
  def run_and_submit_all( profile: gr.OAuthProfile | None):
240
  """
 
123
  # --- Robust Wikipedia Tool ---
124
  def wiki_search(self, query):
125
  try:
126
+ # Clean common AI hallucinations from the query
127
+ query = query.strip(' "').replace('TOOL:', '').replace('INPUT:', '')
128
+ url = f"https://en.wikipedia.org{query}&format=json"
129
+ r = requests.get(url, timeout=10).json()
130
  if not r.get("query", {}).get("search"):
131
+ return "No results found. Try a broader search term."
132
 
133
  title = r["query"]["search"][0]["title"]
134
+ # Fetch the actual content summary
 
135
  summary_url = f"https://en.wikipedia.org{title.replace(' ', '_')}"
136
+ page = requests.get(summary_url, timeout=10).json()
137
+ return page.get("extract", "No summary available.")
138
+ except:
139
+ return "Wikipedia access error."
140
 
141
  def youtube_captions(self, url):
142
  try:
 
148
  except Exception as e:
149
  return f"YouTube error: {e}"
150
 
151
+ def execute_tool(self, tool, input_data, file_url):
152
+ # Clean input_data (remove extra quotes or labels)
153
+ input_data = input_data.strip(' "')
154
+
155
+ if tool == "read_image" or tool == "read_excel":
156
+ # GAIA Fix: If agent says "none", use the file_url provided by the system
157
+ target = file_url if (not input_data or input_data.lower() == "none") else input_data
158
+ if not target: return "Error: No file provided."
159
 
160
+ try:
161
+ r = requests.get(target, timeout=15)
162
+ with open("temp_file", "wb") as f: f.write(r.content)
163
+
164
+ if tool == "read_image":
165
+ return pytesseract.image_to_string(Image.open("temp_file"))
166
+ else:
167
+ return pd.read_excel("temp_file").to_string()[:5000]
168
+ except Exception as e:
169
+ return f"File tool error: {str(e)}"
170
+
171
+ if tool == "wiki_search": return self.wiki_search(input_data)
172
+
173
+ if tool == "calculator":
174
+ try:
175
+ # Remove everything except math chars
176
+ expr = re.sub(r'[^\d\+\-\*\/\(\)\.]', '', input_data)
177
+ return str(eval(expr, {"__builtins__": {}}))
178
+ except: return "Calculation error."
179
+
180
+ return f"Tool {tool} not found."
181
 
182
  # 2. Handle web/search tools
183
  if tool == "wiki_search":
 
198
 
199
  return "Unknown tool"
200
 
201
+ def agent_loop(self, question, file_url):
202
  memory = ""
203
+ # Explicitly tell the agent about the file
204
+ context = f"A file for this task is located at: {file_url}" if file_url else "No file attached."
205
 
206
  for step in range(5):
207
+ prompt = f"""You are a GAIA solver.
208
+ Available tools: read_excel, read_image, wiki_search, calculator.
209
+ {context}
210
 
211
  Question: {question}
212
+ History: {memory}
213
 
214
+ If you have the answer, respond with FINAL: [answer].
215
+ If you need to use a tool, respond with:
216
+ TOOL: [tool_name]
217
+ INPUT: [input_data]"""
 
 
 
 
 
 
 
 
218
 
219
  response = self.client.chat.completions.create(
220
  model="gpt-4o-mini",
221
+ temperature=0, # Crucial for accuracy
222
+ messages=[{"role": "system", "content": "You are a precise agent. For 'opposite' questions, think carefully. For files, use the provided URL."},
223
  {"role": "user", "content": prompt}]
224
  )
225
 
226
+ resp_text = response.choices[0].message.content.strip()
227
+ print(f"Step {step}: {resp_text}")
228
 
229
+ if "FINAL:" in resp_text:
230
+ return resp_text.split("FINAL:")[-1].strip()
 
231
 
232
+ if "TOOL:" in resp_text and "INPUT:" in resp_text:
233
  try:
234
+ tool_name = re.search(r"TOOL:\s*(.*)", resp_text).group(1).split('\n')[0].strip()
235
+ tool_input = re.search(r"INPUT:\s*(.*)", resp_text).group(1).strip()
236
+ result = self.execute_tool(tool_name, tool_input, file_url)
237
+ memory += f"\n- {tool_name} output: {result[:1000]}"
238
+ except:
239
+ memory += "\n- Failed to parse tool call."
240
  else:
241
+ memory += f"\n- {resp_text}"
 
242
 
243
+ return "No answer found."
244
 
245
  def run_and_submit_all( profile: gr.OAuthProfile | None):
246
  """