Vinsmart06 commited on
Commit
61c8c93
·
verified ·
1 Parent(s): 5737bb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -74
app.py CHANGED
@@ -42,12 +42,12 @@ def read_image(file_path):
42
  return "Image read error"
43
 
44
 
45
- def read_audio(file_path):
46
- try:
47
- audio = AudioSegment.from_file(file_path)
48
- return f"Audio length: {len(audio)} ms"
49
- except:
50
- return "Audio read error"
51
 
52
  def execute_tool(self, tool, input_data, file_url):
53
 
@@ -106,7 +106,14 @@ class BasicAgent:
106
  print("🚀 Super GAIA Agent initialized")
107
  # Ensure your API Key is set in the Hugging Face Space Secrets
108
  self.client = OpenAI()
109
-
 
 
 
 
 
 
 
110
  def download_file(self, url):
111
  if not url or not url.startswith("http"):
112
  return None
@@ -123,29 +130,16 @@ class BasicAgent:
123
  # --- Robust Wikipedia Tool ---
124
  def wiki_search(self, query):
125
  try:
126
- # Clean the query
127
  query = query.strip(' "').replace('TOOL:', '').replace('INPUT:', '')
128
- # Step 1: Search for the page
129
  search_url = "https://en.wikipedia.org"
130
  params = {"action": "query", "list": "search", "srsearch": query, "format": "json"}
131
  r = requests.get(search_url, params=params, timeout=10).json()
132
-
133
- if not r.get("query", {}).get("search"):
134
- return "No results. Try simpler keywords."
135
-
136
- # Step 2: Get the top result's snippet and title
137
- best_result = r["query"]["search"][0]
138
- title = best_result["title"]
139
- snippet = BeautifulSoup(best_result["snippet"], "html.parser").get_text()
140
-
141
- # Step 3: Get the page summary content
142
- summary_url = f"https://en.wikipedia.org{title.replace(' ', '_')}"
143
- sum_r = requests.get(summary_url, timeout=10).json()
144
- extract = sum_r.get("extract", snippet)
145
-
146
- return f"Source: {title}\nContent: {extract}"
147
- except Exception as e:
148
- return f"Wiki error: {str(e)}"
149
 
150
  def youtube_captions(self, url):
151
  try:
@@ -159,45 +153,30 @@ class BasicAgent:
159
 
160
  def execute_tool(self, tool, input_data, file_url):
161
  input_data = input_data.strip(' "')
 
 
 
 
162
 
163
- # Tool: Image or Excel
164
  if tool in ["read_image", "read_excel"]:
165
- target = file_url if (not input_data or "http" not in input_data) else input_data
166
- if not target: return "Error: No file available for this task."
167
  try:
168
  r = requests.get(target, timeout=15)
169
  with open("temp_file", "wb") as f: f.write(r.content)
170
- if tool == "read_image":
171
- return pytesseract.image_to_string(Image.open("temp_file"))
172
  return pd.read_excel("temp_file").to_string()[:5000]
173
- except Exception as e:
174
- return f"File error: {str(e)}"
175
 
176
- if tool == "wiki_search":
177
- return self.wiki_search(input_data)
178
-
179
  if tool == "calculator":
180
- try:
181
- expr = re.sub(r'[^\d\+\-\*\/\(\)\.]', '', input_data)
182
- return str(eval(expr, {"__builtins__": {}}))
183
  except: return "Math error."
184
-
185
  return f"Tool {tool} not recognized."
186
 
187
- if tool == "wiki_search": return self.wiki_search(input_data)
188
 
189
- if tool == "calculator":
190
- try:
191
- # Remove everything except math chars
192
- expr = re.sub(r'[^\d\+\-\*\/\(\)\.]', '', input_data)
193
- return str(eval(expr, {"__builtins__": {}}))
194
- except: return "Calculation error."
195
-
196
- return f"Tool {tool} not found."
197
 
198
  # 2. Handle web/search tools
199
- if tool == "wiki_search":
200
- return self.wiki_search(input_data)
201
  if tool == "scrape_page":
202
  try:
203
  r = requests.get(input_data, timeout=15)
@@ -205,60 +184,51 @@ class BasicAgent:
205
  except: return "Web scraping failed."
206
  if tool == "youtube_captions":
207
  return self.youtube_captions(input_data)
208
- if tool == "calculator":
209
- try:
210
- # Basic math security
211
- clean = re.sub(r'[^\d\+\-\*\/\(\)\.]', '', input_data)
212
- return str(eval(clean, {"__builtins__": {}}))
213
- except: return "Math error."
214
-
215
- return "Unknown tool"
216
 
217
  def agent_loop(self, question, file_url):
218
  memory = ""
219
  context = f"File URL: {file_url}" if file_url else "No file provided."
220
 
221
  for step in range(5):
222
- prompt = f"""You are a GAIA solver. Use tools only when necessary.
223
- Available tools: wiki_search, read_image, read_excel, calculator.
224
  {context}
225
-
226
  Question: {question}
227
- History: {memory}
228
 
229
- Output Format:
 
 
 
 
 
230
  TOOL: tool_name
231
  INPUT: tool_input
232
  OR
233
- FINAL: your_answer"""
234
 
235
  response = self.client.chat.completions.create(
236
  model="gpt-4o-mini",
237
  temperature=0,
238
- messages=[{"role": "system", "content": "Be concise. If you see a file URL in context, use it for 'read' tools."},
239
  {"role": "user", "content": prompt}]
240
  )
241
 
242
  resp = response.choices[0].message.content.strip()
243
  print(f"Step {step}: {resp}")
244
 
245
- if "FINAL:" in resp:
246
- return resp.split("FINAL:")[-1].strip()
247
 
248
- # Robust Tool Parsing
249
  try:
250
  t_match = re.search(r"TOOL:\s*(.*)", resp, re.I)
251
  i_match = re.search(r"INPUT:\s*(.*)", resp, re.I)
252
-
253
  if t_match and i_match:
254
  t_name = t_match.group(1).strip().lower()
255
  t_input = i_match.group(1).strip()
256
  result = self.execute_tool(t_name, t_input, file_url)
257
  memory += f"\nStep {step} {t_name} output: {result[:800]}"
258
- else:
259
- memory += f"\nStep {step} info: {resp}"
260
- except:
261
- memory += f"\nStep {step}: Parsing error."
262
 
263
  return "No answer found."
264
 
 
42
  return "Image read error"
43
 
44
 
45
+ #def read_audio(file_path):
46
+ # try:
47
+ # audio = AudioSegment.from_file(file_path)
48
+ # return f"Audio length: {len(audio)} ms"
49
+ # except:
50
+ # return "Audio read error"
51
 
52
  def execute_tool(self, tool, input_data, file_url):
53
 
 
106
  print("🚀 Super GAIA Agent initialized")
107
  # Ensure your API Key is set in the Hugging Face Space Secrets
108
  self.client = OpenAI()
109
+ def read_audio(self, file_url):
110
+ try:
111
+ r = requests.get(file_url, timeout=20)
112
+ with open("temp_audio.mp3", "wb") as f: f.write(r.content)
113
+ result = self.audio_model.transcribe("temp_audio.mp3")
114
+ return result
115
+ except Exception as e:
116
+ return f"Audio error: {str(e)}"
117
  def download_file(self, url):
118
  if not url or not url.startswith("http"):
119
  return None
 
130
  # --- Robust Wikipedia Tool ---
131
  def wiki_search(self, query):
132
  try:
 
133
  query = query.strip(' "').replace('TOOL:', '').replace('INPUT:', '')
 
134
  search_url = "https://en.wikipedia.org"
135
  params = {"action": "query", "list": "search", "srsearch": query, "format": "json"}
136
  r = requests.get(search_url, params=params, timeout=10).json()
137
+ if not r.get("query", {}).get("search"): return "No results."
138
+ title = r["query"]["search"][0]["title"]
139
+ sum_url = f"https://en.wikipedia.org{title.replace(' ', '_')}"
140
+ sum_r = requests.get(sum_url, timeout=10).json()
141
+ return f"Source: {title}\nContent: {sum_r.get('extract', 'No summary.')}"
142
+ except: return "Wiki error."
 
 
 
 
 
 
 
 
 
 
 
143
 
144
  def youtube_captions(self, url):
145
  try:
 
153
 
154
  def execute_tool(self, tool, input_data, file_url):
155
  input_data = input_data.strip(' "')
156
+ # Use provided file_url if agent input is generic or missing
157
+ target = file_url if (not input_data or "http" not in input_data) else input_data
158
+
159
+ if tool == "read_audio": return self.read_audio(target)
160
 
 
161
  if tool in ["read_image", "read_excel"]:
162
+ if not target: return "Error: No file URL."
 
163
  try:
164
  r = requests.get(target, timeout=15)
165
  with open("temp_file", "wb") as f: f.write(r.content)
166
+ if tool == "read_image": return pytesseract.image_to_string(Image.open("temp_file"))
 
167
  return pd.read_excel("temp_file").to_string()[:5000]
168
+ except Exception as e: return f"File error: {str(e)}"
 
169
 
170
+ if tool == "wiki_search": return self.wiki_search(input_data)
 
 
171
  if tool == "calculator":
172
+ try: return str(eval(re.sub(r'[^\d\+\-\*\/\(\)\.]', '', input_data), {"__builtins__": {}}))
 
 
173
  except: return "Math error."
 
174
  return f"Tool {tool} not recognized."
175
 
 
176
 
 
 
 
 
 
 
 
 
177
 
178
  # 2. Handle web/search tools
179
+
 
180
  if tool == "scrape_page":
181
  try:
182
  r = requests.get(input_data, timeout=15)
 
184
  except: return "Web scraping failed."
185
  if tool == "youtube_captions":
186
  return self.youtube_captions(input_data)
187
+
 
 
 
 
 
 
 
188
 
189
  def agent_loop(self, question, file_url):
190
  memory = ""
191
  context = f"File URL: {file_url}" if file_url else "No file provided."
192
 
193
  for step in range(5):
194
+ # Updated system prompt to handle botanical "trap" questions
195
+ prompt = f"""You are a GAIA solver.
196
  {context}
 
197
  Question: {question}
 
198
 
199
+ IMPORTANT:
200
+ - If asked for 'vegetables' botanically, remember that anything with seeds (tomatoes, peppers, beans, corn, zucchini, cucumbers) is a FRUIT.
201
+ - Available tools: wiki_search, read_image, read_excel, read_audio, calculator.
202
+ - History: {memory}
203
+
204
+ Format:
205
  TOOL: tool_name
206
  INPUT: tool_input
207
  OR
208
+ FINAL: answer"""
209
 
210
  response = self.client.chat.completions.create(
211
  model="gpt-4o-mini",
212
  temperature=0,
213
+ messages=[{"role": "system", "content": "You are a precise scientific agent. For audio tasks, use read_audio."},
214
  {"role": "user", "content": prompt}]
215
  )
216
 
217
  resp = response.choices[0].message.content.strip()
218
  print(f"Step {step}: {resp}")
219
 
220
+ if "FINAL:" in resp: return resp.split("FINAL:")[-1].strip()
 
221
 
 
222
  try:
223
  t_match = re.search(r"TOOL:\s*(.*)", resp, re.I)
224
  i_match = re.search(r"INPUT:\s*(.*)", resp, re.I)
 
225
  if t_match and i_match:
226
  t_name = t_match.group(1).strip().lower()
227
  t_input = i_match.group(1).strip()
228
  result = self.execute_tool(t_name, t_input, file_url)
229
  memory += f"\nStep {step} {t_name} output: {result[:800]}"
230
+ else: memory += f"\nStep {step} info: {resp}"
231
+ except: memory += f"\nStep {step}: Parsing error."
 
 
232
 
233
  return "No answer found."
234