Update app.py
Browse files
app.py
CHANGED
|
@@ -36,37 +36,55 @@ def web_search(query: str) -> str:
|
|
| 36 |
for r in results:
|
| 37 |
output.append(f"- {r.get('title','')}: {r.get('body','')}")
|
| 38 |
return "\n".join(output)
|
| 39 |
-
except:
|
|
|
|
| 40 |
return ""
|
| 41 |
|
| 42 |
|
| 43 |
-
def
|
| 44 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 45 |
try:
|
| 46 |
url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
if response.status_code == 404:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
return {"has_file": False}
|
| 51 |
|
| 52 |
content_type = response.headers.get('content-type', '').lower()
|
| 53 |
disposition = response.headers.get('content-disposition', '')
|
| 54 |
|
| 55 |
-
|
|
|
|
| 56 |
if 'filename=' in disposition:
|
| 57 |
-
filename = disposition.split('filename=')[-1].strip('"\'')
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Python files
|
| 62 |
-
if filename.endswith('.py'):
|
| 63 |
result["content"] = response.text
|
| 64 |
result["file_type"] = "python"
|
|
|
|
| 65 |
return result
|
| 66 |
|
| 67 |
-
# Text
|
| 68 |
if 'text' in content_type or filename.endswith(('.txt', '.md', '.csv', '.json')):
|
| 69 |
-
result["content"] = response.text
|
| 70 |
result["file_type"] = "text"
|
| 71 |
return result
|
| 72 |
|
|
@@ -75,72 +93,86 @@ def get_task_file(task_id: str) -> dict:
|
|
| 75 |
try:
|
| 76 |
from io import BytesIO
|
| 77 |
df = pd.read_excel(BytesIO(response.content))
|
| 78 |
-
result["content"] = df.
|
| 79 |
result["dataframe"] = df
|
| 80 |
result["file_type"] = "excel"
|
|
|
|
| 81 |
return result
|
| 82 |
except Exception as e:
|
| 83 |
-
|
|
|
|
| 84 |
result["file_type"] = "excel"
|
| 85 |
return result
|
| 86 |
|
| 87 |
# Images
|
| 88 |
if 'image' in content_type or filename.endswith(('.png', '.jpg', '.jpeg', '.gif')):
|
| 89 |
result["file_type"] = "image"
|
| 90 |
-
result["content"] = "[
|
| 91 |
return result
|
| 92 |
|
| 93 |
-
#
|
| 94 |
-
if 'audio' in content_type or
|
| 95 |
-
result["file_type"] = "
|
| 96 |
-
result["content"] = "[
|
| 97 |
return result
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
except Exception as e:
|
|
|
|
| 104 |
return {"has_file": False, "error": str(e)}
|
| 105 |
|
| 106 |
|
| 107 |
-
def
|
| 108 |
-
"""Execute Python code and
|
| 109 |
try:
|
| 110 |
import io
|
| 111 |
import sys
|
| 112 |
|
| 113 |
-
# Create string buffer for stdout
|
| 114 |
old_stdout = sys.stdout
|
| 115 |
-
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
|
| 124 |
-
# Get output
|
| 125 |
-
output = buffer.getvalue()
|
| 126 |
sys.stdout = old_stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
return output.strip() if output.strip() else "Code executed, no print output"
|
| 129 |
except Exception as e:
|
| 130 |
-
return f"
|
| 131 |
|
| 132 |
|
| 133 |
-
def
|
| 134 |
-
"""Reverse a string"""
|
| 135 |
return text[::-1]
|
| 136 |
|
| 137 |
|
| 138 |
-
def
|
| 139 |
-
"""Check if text
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
text_lower = text.lower()
|
| 143 |
-
return any(ind in text_lower for ind in indicators)
|
| 144 |
|
| 145 |
|
| 146 |
# ============================================
|
|
@@ -151,155 +183,115 @@ class BasicAgent:
|
|
| 151 |
def __init__(self):
|
| 152 |
print("Initializing agent...")
|
| 153 |
self.client = get_groq_client()
|
| 154 |
-
print("β
|
| 155 |
|
| 156 |
-
def
|
| 157 |
-
"""
|
| 158 |
-
for attempt in range(
|
| 159 |
try:
|
| 160 |
response = self.client.chat.completions.create(
|
| 161 |
model="llama-3.3-70b-versatile",
|
| 162 |
messages=[{"role": "user", "content": prompt}],
|
| 163 |
-
temperature=0,
|
| 164 |
max_tokens=200,
|
| 165 |
)
|
| 166 |
return response.choices[0].message.content.strip()
|
| 167 |
except Exception as e:
|
| 168 |
if "rate" in str(e).lower() or "429" in str(e):
|
| 169 |
wait = (attempt + 1) * 20
|
| 170 |
-
print(f"
|
| 171 |
time.sleep(wait)
|
| 172 |
else:
|
| 173 |
-
print(f"
|
| 174 |
-
return "
|
| 175 |
-
return "
|
| 176 |
|
| 177 |
-
def
|
| 178 |
-
"""Clean
|
| 179 |
if not answer:
|
| 180 |
-
return "
|
| 181 |
-
|
| 182 |
-
# Remove
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
"Final answer:", "Final Answer:", "Based on the", "According to",
|
| 186 |
-
"The result is:", "The result is", "The output is:", "The output is",
|
| 187 |
-
]
|
| 188 |
-
for p in prefixes:
|
| 189 |
-
if answer.startswith(p):
|
| 190 |
-
answer = answer[len(p):].strip()
|
| 191 |
-
elif answer.lower().startswith(p.lower()):
|
| 192 |
answer = answer[len(p):].strip()
|
| 193 |
|
| 194 |
-
#
|
| 195 |
answer = answer.replace("**", "").replace("```", "").strip()
|
|
|
|
| 196 |
|
| 197 |
-
# Remove
|
| 198 |
-
if (answer.startswith('"') and answer.endswith('"')) or \
|
| 199 |
-
(answer.startswith("'") and answer.endswith("'")):
|
| 200 |
-
answer = answer[1:-1]
|
| 201 |
-
|
| 202 |
-
# Remove trailing period for short answers
|
| 203 |
if answer.endswith('.') and len(answer.split()) <= 5:
|
| 204 |
answer = answer[:-1]
|
| 205 |
|
| 206 |
-
# Filter out non-answers
|
| 207 |
-
bad_phrases = ["I'm unable", "I cannot", "I don't have", "I couldn't", "unfortunately"]
|
| 208 |
-
if any(bp.lower() in answer.lower() for bp in bad_phrases):
|
| 209 |
-
return "unknown"
|
| 210 |
-
|
| 211 |
return answer.strip()
|
| 212 |
|
| 213 |
def __call__(self, question: str, task_id: str = None) -> str:
|
| 214 |
-
|
| 215 |
|
| 216 |
-
# ===
|
| 217 |
-
if
|
| 218 |
-
|
| 219 |
-
print(f" [
|
| 220 |
-
question = decoded
|
| 221 |
|
| 222 |
-
# ===
|
| 223 |
-
file_info =
|
| 224 |
-
file_context = ""
|
| 225 |
|
| 226 |
if file_info.get("has_file"):
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
print(f" [File: {filename} ({file_type})]")
|
| 230 |
|
| 231 |
-
if
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
output
|
| 235 |
-
|
| 236 |
-
file_context = f"Python code output:\n{output}"
|
| 237 |
|
| 238 |
-
elif
|
| 239 |
-
|
| 240 |
-
if df is not None:
|
| 241 |
-
# Provide summary and data
|
| 242 |
-
file_context = f"Excel file ({len(df)} rows):\n{file_info.get('content', '')[:3000]}"
|
| 243 |
-
else:
|
| 244 |
-
file_context = f"Excel content:\n{file_info.get('content', '')[:3000]}"
|
| 245 |
-
|
| 246 |
-
elif file_type == "text":
|
| 247 |
-
file_context = f"File content:\n{file_info.get('content', '')[:4000]}"
|
| 248 |
|
| 249 |
-
elif
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
"
|
| 262 |
-
"
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
search_context = f"Search results:\n{results[:2500]}"
|
| 277 |
-
print(f" [Web search done]")
|
| 278 |
-
|
| 279 |
-
# === STEP 4: Build prompt and ask LLM ===
|
| 280 |
-
context = ""
|
| 281 |
-
if file_context:
|
| 282 |
-
context += f"\n\n{file_context}"
|
| 283 |
-
if search_context:
|
| 284 |
-
context += f"\n\n{search_context}"
|
| 285 |
-
|
| 286 |
-
prompt = f"""Answer this question. Give ONLY the final answer - no explanation.
|
| 287 |
-
|
| 288 |
-
RULES:
|
| 289 |
-
- Just the answer (number, name, word, or short phrase)
|
| 290 |
-
- No "The answer is" or similar prefixes
|
| 291 |
-
- If it's a number, just the number
|
| 292 |
-
- If it's a name, just the name
|
| 293 |
-
- If it's a list, comma-separated items
|
| 294 |
-
- Be precise - this is graded by exact match
|
| 295 |
-
{context}
|
| 296 |
|
| 297 |
Question: {question}
|
| 298 |
|
| 299 |
Answer:"""
|
| 300 |
-
|
| 301 |
-
answer = self.
|
| 302 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
|
| 305 |
# ============================================
|
|
@@ -327,8 +319,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 327 |
|
| 328 |
try:
|
| 329 |
questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15).json()
|
| 330 |
-
print(f"π {len(questions)} questions")
|
| 331 |
-
print(f"β±οΈ Est. time: {len(questions) * DELAY_BETWEEN_QUESTIONS // 60} min\n")
|
| 332 |
except Exception as e:
|
| 333 |
return f"β Fetch failed: {e}", None
|
| 334 |
|
|
@@ -340,10 +331,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 340 |
task_id = q.get("task_id")
|
| 341 |
question = q.get("question", "")
|
| 342 |
|
| 343 |
-
print(f"\n[{i+1}/{len(questions)}] {
|
|
|
|
| 344 |
|
| 345 |
answer = agent(question, task_id)
|
| 346 |
-
print(f"
|
| 347 |
|
| 348 |
answers.append({"task_id": task_id, "submitted_answer": answer})
|
| 349 |
results.append({"#": i+1, "Question": question[:50]+"...", "Answer": answer})
|
|
@@ -352,7 +344,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 352 |
time.sleep(DELAY_BETWEEN_QUESTIONS)
|
| 353 |
|
| 354 |
total = time.time() - start
|
| 355 |
-
print(f"\nβ±οΈ
|
| 356 |
|
| 357 |
try:
|
| 358 |
result = requests.post(
|
|
@@ -371,7 +363,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 371 |
|
| 372 |
status = f"β
Done in {total/60:.1f} min\n\n"
|
| 373 |
status += f"π― Score: {score}% ({correct}/{total_q})\n\n"
|
| 374 |
-
status += "π PASSED!
|
| 375 |
|
| 376 |
return status, pd.DataFrame(results)
|
| 377 |
except Exception as e:
|
|
@@ -384,26 +376,15 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 384 |
|
| 385 |
with gr.Blocks() as demo:
|
| 386 |
gr.Markdown("# π― GAIA Agent - Unit 4")
|
| 387 |
-
gr.Markdown(""
|
| 388 |
-
**Model:** Llama 3.3 70B via Groq
|
| 389 |
-
|
| 390 |
-
**Features:**
|
| 391 |
-
- β
Python code execution
|
| 392 |
-
- β
Excel file analysis
|
| 393 |
-
- β
Reversed text detection
|
| 394 |
-
- β
Web search
|
| 395 |
-
|
| 396 |
-
β±οΈ ~5 minutes runtime
|
| 397 |
-
""")
|
| 398 |
|
| 399 |
gr.LoginButton()
|
| 400 |
-
run_btn = gr.Button("π Run
|
| 401 |
-
status = gr.Textbox(label="Status", lines=
|
| 402 |
table = gr.DataFrame(label="Results")
|
| 403 |
|
| 404 |
run_btn.click(run_and_submit_all, outputs=[status, table])
|
| 405 |
|
| 406 |
if __name__ == "__main__":
|
| 407 |
-
print("π― GAIA Agent Starting")
|
| 408 |
print(f"GROQ_API_KEY: {'β
' if os.environ.get('GROQ_API_KEY') else 'β'}")
|
| 409 |
demo.launch()
|
|
|
|
| 36 |
for r in results:
|
| 37 |
output.append(f"- {r.get('title','')}: {r.get('body','')}")
|
| 38 |
return "\n".join(output)
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f" Search error: {e}")
|
| 41 |
return ""
|
| 42 |
|
| 43 |
|
| 44 |
+
def fetch_task_file(task_id: str) -> dict:
|
| 45 |
+
"""Fetch file from GAIA API"""
|
| 46 |
+
if not task_id:
|
| 47 |
+
return {"has_file": False}
|
| 48 |
+
|
| 49 |
try:
|
| 50 |
url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
|
| 51 |
+
print(f" Fetching file: {url}")
|
| 52 |
+
|
| 53 |
+
response = requests.get(url, timeout=30)
|
| 54 |
+
print(f" Response status: {response.status_code}")
|
| 55 |
|
| 56 |
if response.status_code == 404:
|
| 57 |
+
print(f" No file for this task")
|
| 58 |
+
return {"has_file": False}
|
| 59 |
+
|
| 60 |
+
if response.status_code != 200:
|
| 61 |
+
print(f" Error status: {response.status_code}")
|
| 62 |
return {"has_file": False}
|
| 63 |
|
| 64 |
content_type = response.headers.get('content-type', '').lower()
|
| 65 |
disposition = response.headers.get('content-disposition', '')
|
| 66 |
|
| 67 |
+
# Extract filename
|
| 68 |
+
filename = "unknown"
|
| 69 |
if 'filename=' in disposition:
|
| 70 |
+
filename = disposition.split('filename=')[-1].strip('"\'').strip()
|
| 71 |
+
elif 'filename*=' in disposition:
|
| 72 |
+
filename = disposition.split('filename*=')[-1].strip('"\'').strip()
|
| 73 |
|
| 74 |
+
print(f" File: {filename}, Type: {content_type}")
|
| 75 |
+
|
| 76 |
+
result = {"has_file": True, "filename": filename, "content_type": content_type}
|
| 77 |
|
| 78 |
# Python files
|
| 79 |
+
if filename.endswith('.py') or 'python' in content_type:
|
| 80 |
result["content"] = response.text
|
| 81 |
result["file_type"] = "python"
|
| 82 |
+
print(f" Python file, {len(response.text)} chars")
|
| 83 |
return result
|
| 84 |
|
| 85 |
+
# Text files
|
| 86 |
if 'text' in content_type or filename.endswith(('.txt', '.md', '.csv', '.json')):
|
| 87 |
+
result["content"] = response.text
|
| 88 |
result["file_type"] = "text"
|
| 89 |
return result
|
| 90 |
|
|
|
|
| 93 |
try:
|
| 94 |
from io import BytesIO
|
| 95 |
df = pd.read_excel(BytesIO(response.content))
|
| 96 |
+
result["content"] = df.to_csv(index=False)
|
| 97 |
result["dataframe"] = df
|
| 98 |
result["file_type"] = "excel"
|
| 99 |
+
print(f" Excel file, {len(df)} rows")
|
| 100 |
return result
|
| 101 |
except Exception as e:
|
| 102 |
+
print(f" Excel parse error: {e}")
|
| 103 |
+
result["content"] = f"Excel file (error: {e})"
|
| 104 |
result["file_type"] = "excel"
|
| 105 |
return result
|
| 106 |
|
| 107 |
# Images
|
| 108 |
if 'image' in content_type or filename.endswith(('.png', '.jpg', '.jpeg', '.gif')):
|
| 109 |
result["file_type"] = "image"
|
| 110 |
+
result["content"] = "[IMAGE - cannot process]"
|
| 111 |
return result
|
| 112 |
|
| 113 |
+
# Audio/Video
|
| 114 |
+
if 'audio' in content_type or 'video' in content_type:
|
| 115 |
+
result["file_type"] = "media"
|
| 116 |
+
result["content"] = "[MEDIA - cannot process]"
|
| 117 |
return result
|
| 118 |
|
| 119 |
+
# Try as text
|
| 120 |
+
try:
|
| 121 |
+
result["content"] = response.text[:8000]
|
| 122 |
+
result["file_type"] = "text"
|
| 123 |
+
return result
|
| 124 |
+
except:
|
| 125 |
+
result["content"] = "[Binary file]"
|
| 126 |
+
result["file_type"] = "binary"
|
| 127 |
+
return result
|
| 128 |
|
| 129 |
except Exception as e:
|
| 130 |
+
print(f" File fetch error: {e}")
|
| 131 |
return {"has_file": False, "error": str(e)}
|
| 132 |
|
| 133 |
|
| 134 |
+
def run_python_code(code: str) -> str:
|
| 135 |
+
"""Execute Python code and return output"""
|
| 136 |
try:
|
| 137 |
import io
|
| 138 |
import sys
|
| 139 |
|
|
|
|
| 140 |
old_stdout = sys.stdout
|
| 141 |
+
old_stderr = sys.stderr
|
| 142 |
+
sys.stdout = stdout_buffer = io.StringIO()
|
| 143 |
+
sys.stderr = stderr_buffer = io.StringIO()
|
| 144 |
|
| 145 |
+
try:
|
| 146 |
+
exec(code, {"__builtins__": __builtins__})
|
| 147 |
+
except Exception as e:
|
| 148 |
+
sys.stdout = old_stdout
|
| 149 |
+
sys.stderr = old_stderr
|
| 150 |
+
return f"Execution error: {e}"
|
| 151 |
|
|
|
|
|
|
|
| 152 |
sys.stdout = old_stdout
|
| 153 |
+
sys.stderr = old_stderr
|
| 154 |
+
|
| 155 |
+
output = stdout_buffer.getvalue()
|
| 156 |
+
errors = stderr_buffer.getvalue()
|
| 157 |
+
|
| 158 |
+
if output:
|
| 159 |
+
return output.strip()
|
| 160 |
+
if errors:
|
| 161 |
+
return f"Stderr: {errors.strip()}"
|
| 162 |
+
return "No output"
|
| 163 |
|
|
|
|
| 164 |
except Exception as e:
|
| 165 |
+
return f"Error: {e}"
|
| 166 |
|
| 167 |
|
| 168 |
+
def reverse_text(text: str) -> str:
|
|
|
|
| 169 |
return text[::-1]
|
| 170 |
|
| 171 |
|
| 172 |
+
def is_reversed(text: str) -> bool:
|
| 173 |
+
"""Check if text is reversed English"""
|
| 174 |
+
patterns = ['.rewsna', 'eht sa', 'tfel', 'drow eht', 'etisoppo', 'tahW', 'erehW']
|
| 175 |
+
return any(p in text for p in patterns)
|
|
|
|
|
|
|
| 176 |
|
| 177 |
|
| 178 |
# ============================================
|
|
|
|
| 183 |
def __init__(self):
|
| 184 |
print("Initializing agent...")
|
| 185 |
self.client = get_groq_client()
|
| 186 |
+
print("β
Agent ready!")
|
| 187 |
|
| 188 |
+
def ask(self, prompt: str) -> str:
|
| 189 |
+
"""Ask LLM with retries"""
|
| 190 |
+
for attempt in range(3):
|
| 191 |
try:
|
| 192 |
response = self.client.chat.completions.create(
|
| 193 |
model="llama-3.3-70b-versatile",
|
| 194 |
messages=[{"role": "user", "content": prompt}],
|
| 195 |
+
temperature=0.1,
|
| 196 |
max_tokens=200,
|
| 197 |
)
|
| 198 |
return response.choices[0].message.content.strip()
|
| 199 |
except Exception as e:
|
| 200 |
if "rate" in str(e).lower() or "429" in str(e):
|
| 201 |
wait = (attempt + 1) * 20
|
| 202 |
+
print(f" Rate limit, waiting {wait}s...")
|
| 203 |
time.sleep(wait)
|
| 204 |
else:
|
| 205 |
+
print(f" LLM error: {e}")
|
| 206 |
+
return ""
|
| 207 |
+
return ""
|
| 208 |
|
| 209 |
+
def clean(self, answer: str) -> str:
|
| 210 |
+
"""Clean answer for exact match"""
|
| 211 |
if not answer:
|
| 212 |
+
return ""
|
| 213 |
+
|
| 214 |
+
# Remove prefixes
|
| 215 |
+
for p in ["Answer:", "The answer is:", "The answer is", "A:", "Final answer:", "**"]:
|
| 216 |
+
if answer.lower().startswith(p.lower()):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
answer = answer[len(p):].strip()
|
| 218 |
|
| 219 |
+
# Clean formatting
|
| 220 |
answer = answer.replace("**", "").replace("```", "").strip()
|
| 221 |
+
answer = answer.strip('"\'')
|
| 222 |
|
| 223 |
+
# Remove trailing punctuation for short answers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
if answer.endswith('.') and len(answer.split()) <= 5:
|
| 225 |
answer = answer[:-1]
|
| 226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
return answer.strip()
|
| 228 |
|
| 229 |
def __call__(self, question: str, task_id: str = None) -> str:
|
| 230 |
+
context_parts = []
|
| 231 |
|
| 232 |
+
# === Handle reversed text ===
|
| 233 |
+
if is_reversed(question):
|
| 234 |
+
question = reverse_text(question)
|
| 235 |
+
print(f" [Decoded reversed: {question[:50]}...]")
|
|
|
|
| 236 |
|
| 237 |
+
# === Fetch file ===
|
| 238 |
+
file_info = fetch_task_file(task_id)
|
|
|
|
| 239 |
|
| 240 |
if file_info.get("has_file"):
|
| 241 |
+
ftype = file_info.get("file_type", "")
|
| 242 |
+
content = file_info.get("content", "")
|
|
|
|
| 243 |
|
| 244 |
+
if ftype == "python" and content:
|
| 245 |
+
print(f" [Executing Python...]")
|
| 246 |
+
output = run_python_code(content)
|
| 247 |
+
print(f" [Output: {output[:100]}]")
|
| 248 |
+
context_parts.append(f"Python code output:\n{output}")
|
|
|
|
| 249 |
|
| 250 |
+
elif ftype == "excel":
|
| 251 |
+
context_parts.append(f"Excel data:\n{content[:4000]}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
+
elif ftype == "text":
|
| 254 |
+
context_parts.append(f"File content:\n{content[:4000]}")
|
| 255 |
+
|
| 256 |
+
elif ftype in ["image", "media"]:
|
| 257 |
+
context_parts.append("[This task has an image/media file that cannot be processed]")
|
| 258 |
+
|
| 259 |
+
# === Web search ===
|
| 260 |
+
do_search = not file_info.get("has_file") or file_info.get("file_type") in ["image", "media"]
|
| 261 |
+
|
| 262 |
+
if do_search:
|
| 263 |
+
results = web_search(question[:100])
|
| 264 |
+
if results:
|
| 265 |
+
context_parts.append(f"Web search:\n{results[:2500]}")
|
| 266 |
+
print(f" [Search done]")
|
| 267 |
+
|
| 268 |
+
# === Ask LLM ===
|
| 269 |
+
context = "\n\n".join(context_parts)
|
| 270 |
+
|
| 271 |
+
prompt = f"""Answer this question with ONLY the answer. No explanation.
|
| 272 |
+
|
| 273 |
+
Rules:
|
| 274 |
+
- Give just the answer (number, name, or short phrase)
|
| 275 |
+
- No "The answer is" prefix
|
| 276 |
+
- Be precise - exact match grading
|
| 277 |
+
- If unsure, give your best guess
|
| 278 |
+
|
| 279 |
+
{f"Context:{chr(10)}{context}" if context else ""}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
Question: {question}
|
| 282 |
|
| 283 |
Answer:"""
|
| 284 |
+
|
| 285 |
+
answer = self.ask(prompt)
|
| 286 |
+
answer = self.clean(answer)
|
| 287 |
+
|
| 288 |
+
# Don't return empty
|
| 289 |
+
if not answer or "unable" in answer.lower() or "cannot" in answer.lower():
|
| 290 |
+
# Try simpler prompt
|
| 291 |
+
simple = self.ask(f"Answer in 1-3 words: {question}")
|
| 292 |
+
answer = self.clean(simple) or "unknown"
|
| 293 |
+
|
| 294 |
+
return answer
|
| 295 |
|
| 296 |
|
| 297 |
# ============================================
|
|
|
|
| 319 |
|
| 320 |
try:
|
| 321 |
questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15).json()
|
| 322 |
+
print(f"π {len(questions)} questions\n")
|
|
|
|
| 323 |
except Exception as e:
|
| 324 |
return f"β Fetch failed: {e}", None
|
| 325 |
|
|
|
|
| 331 |
task_id = q.get("task_id")
|
| 332 |
question = q.get("question", "")
|
| 333 |
|
| 334 |
+
print(f"\n[{i+1}/{len(questions)}] Task: {task_id}")
|
| 335 |
+
print(f"Q: {question[:70]}...")
|
| 336 |
|
| 337 |
answer = agent(question, task_id)
|
| 338 |
+
print(f"A: {answer}")
|
| 339 |
|
| 340 |
answers.append({"task_id": task_id, "submitted_answer": answer})
|
| 341 |
results.append({"#": i+1, "Question": question[:50]+"...", "Answer": answer})
|
|
|
|
| 344 |
time.sleep(DELAY_BETWEEN_QUESTIONS)
|
| 345 |
|
| 346 |
total = time.time() - start
|
| 347 |
+
print(f"\nβ±οΈ {total/60:.1f} min total")
|
| 348 |
|
| 349 |
try:
|
| 350 |
result = requests.post(
|
|
|
|
| 363 |
|
| 364 |
status = f"β
Done in {total/60:.1f} min\n\n"
|
| 365 |
status += f"π― Score: {score}% ({correct}/{total_q})\n\n"
|
| 366 |
+
status += "π PASSED!" if score >= 30 else f"Need {30-score}% more"
|
| 367 |
|
| 368 |
return status, pd.DataFrame(results)
|
| 369 |
except Exception as e:
|
|
|
|
| 376 |
|
| 377 |
with gr.Blocks() as demo:
|
| 378 |
gr.Markdown("# π― GAIA Agent - Unit 4")
|
| 379 |
+
gr.Markdown("**Llama 3.3 70B** via Groq | ~5 min runtime")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
|
| 381 |
gr.LoginButton()
|
| 382 |
+
run_btn = gr.Button("π Run", variant="primary", size="lg")
|
| 383 |
+
status = gr.Textbox(label="Status", lines=5)
|
| 384 |
table = gr.DataFrame(label="Results")
|
| 385 |
|
| 386 |
run_btn.click(run_and_submit_all, outputs=[status, table])
|
| 387 |
|
| 388 |
if __name__ == "__main__":
|
|
|
|
| 389 |
print(f"GROQ_API_KEY: {'β
' if os.environ.get('GROQ_API_KEY') else 'β'}")
|
| 390 |
demo.launch()
|