Update app.py
Browse files
app.py
CHANGED
|
@@ -123,20 +123,20 @@ class BasicAgent:
|
|
| 123 |
# --- Robust Wikipedia Tool ---
|
| 124 |
def wiki_search(self, query):
|
| 125 |
try:
|
| 126 |
-
#
|
| 127 |
-
|
| 128 |
-
|
|
|
|
| 129 |
if not r.get("query", {}).get("search"):
|
| 130 |
-
return "No
|
| 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", "
|
| 138 |
-
except
|
| 139 |
-
return
|
| 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 |
-
|
| 152 |
-
#
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 187 |
memory = ""
|
| 188 |
-
#
|
| 189 |
-
|
| 190 |
|
| 191 |
for step in range(5):
|
| 192 |
-
prompt = f"""You are a GAIA
|
| 193 |
-
Available tools: read_excel, read_image,
|
| 194 |
-
{
|
| 195 |
|
| 196 |
Question: {question}
|
|
|
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 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
|
| 215 |
{"role": "user", "content": prompt}]
|
| 216 |
)
|
| 217 |
|
| 218 |
-
|
| 219 |
-
print(f"
|
| 220 |
|
| 221 |
-
if "FINAL:" in
|
| 222 |
-
|
| 223 |
-
return action.split("FINAL:")[-1].strip()
|
| 224 |
|
| 225 |
-
if "TOOL:" in
|
| 226 |
try:
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
result = self.execute_tool(
|
| 230 |
-
memory += f"\
|
| 231 |
-
except
|
| 232 |
-
memory +=
|
| 233 |
else:
|
| 234 |
-
|
| 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 |
"""
|