Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ from duckduckgo_search import DDGS
|
|
| 9 |
|
| 10 |
# --- Constants ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
-
DELAY_BETWEEN_QUESTIONS = 15
|
| 13 |
|
| 14 |
# ============================================
|
| 15 |
# GROQ CLIENT
|
|
@@ -26,6 +26,7 @@ def get_groq_client():
|
|
| 26 |
# ============================================
|
| 27 |
|
| 28 |
def web_search(query: str) -> str:
|
|
|
|
| 29 |
try:
|
| 30 |
with DDGS() as ddgs:
|
| 31 |
results = list(ddgs.text(query, max_results=5))
|
|
@@ -33,18 +34,17 @@ def web_search(query: str) -> str:
|
|
| 33 |
return ""
|
| 34 |
output = []
|
| 35 |
for r in results:
|
| 36 |
-
output.append(f"
|
| 37 |
-
output.append(f"Snippet: {r.get('body','')}")
|
| 38 |
-
output.append("---")
|
| 39 |
return "\n".join(output)
|
| 40 |
except:
|
| 41 |
return ""
|
| 42 |
|
| 43 |
|
| 44 |
def get_task_file(task_id: str) -> dict:
|
|
|
|
| 45 |
try:
|
| 46 |
url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
|
| 47 |
-
response = requests.get(url, timeout=
|
| 48 |
|
| 49 |
if response.status_code == 404:
|
| 50 |
return {"has_file": False}
|
|
@@ -58,66 +58,89 @@ def get_task_file(task_id: str) -> dict:
|
|
| 58 |
|
| 59 |
result = {"has_file": True, "filename": filename, "type": content_type}
|
| 60 |
|
| 61 |
-
# Python files
|
| 62 |
if filename.endswith('.py'):
|
| 63 |
result["content"] = response.text
|
| 64 |
-
result["
|
| 65 |
return result
|
| 66 |
|
| 67 |
-
# Text files
|
| 68 |
if 'text' in content_type or filename.endswith(('.txt', '.md', '.csv', '.json')):
|
| 69 |
-
result["content"] = response.text[:
|
|
|
|
| 70 |
return result
|
| 71 |
|
| 72 |
-
# Excel
|
| 73 |
if 'excel' in content_type or 'spreadsheet' in content_type or filename.endswith(('.xlsx', '.xls')):
|
| 74 |
try:
|
| 75 |
from io import BytesIO
|
| 76 |
df = pd.read_excel(BytesIO(response.content))
|
| 77 |
result["content"] = df.to_string()
|
| 78 |
-
result["
|
|
|
|
| 79 |
return result
|
| 80 |
except Exception as e:
|
| 81 |
-
result["content"] = f"
|
|
|
|
| 82 |
return result
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
if 'image' in content_type:
|
| 86 |
-
result["
|
| 87 |
-
result["
|
| 88 |
return result
|
| 89 |
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
return result
|
|
|
|
| 92 |
except Exception as e:
|
| 93 |
return {"has_file": False, "error": str(e)}
|
| 94 |
|
| 95 |
|
| 96 |
-
def
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
def is_reversed(text: str) -> bool:
|
| 101 |
-
indicators = ['.rewsna', 'eht sa', 'tfel', 'drow eht', 'etisoppo']
|
| 102 |
-
return any(x in text.lower() for x in indicators)
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
def execute_python(code: str) -> str:
|
| 106 |
-
"""Safely execute Python code and return output"""
|
| 107 |
try:
|
| 108 |
import io
|
| 109 |
import sys
|
| 110 |
-
from contextlib import redirect_stdout
|
| 111 |
|
| 112 |
-
#
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
exec(code, {"__builtins__": __builtins__})
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
except Exception as e:
|
| 120 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
# ============================================
|
|
@@ -126,18 +149,19 @@ def execute_python(code: str) -> str:
|
|
| 126 |
|
| 127 |
class BasicAgent:
|
| 128 |
def __init__(self):
|
| 129 |
-
print("Initializing
|
| 130 |
self.client = get_groq_client()
|
| 131 |
print("β
Ready!")
|
| 132 |
|
| 133 |
-
def
|
|
|
|
| 134 |
for attempt in range(max_retries):
|
| 135 |
try:
|
| 136 |
response = self.client.chat.completions.create(
|
| 137 |
-
model="llama-3.3-70b-versatile",
|
| 138 |
messages=[{"role": "user", "content": prompt}],
|
| 139 |
temperature=0,
|
| 140 |
-
max_tokens=
|
| 141 |
)
|
| 142 |
return response.choices[0].message.content.strip()
|
| 143 |
except Exception as e:
|
|
@@ -146,98 +170,135 @@ class BasicAgent:
|
|
| 146 |
print(f" β³ Rate limit, waiting {wait}s...")
|
| 147 |
time.sleep(wait)
|
| 148 |
else:
|
| 149 |
-
|
|
|
|
| 150 |
return "unknown"
|
| 151 |
|
| 152 |
def clean_answer(self, answer: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
# Remove common prefixes
|
| 154 |
prefixes = [
|
| 155 |
-
"Answer:", "The answer is:", "The answer is", "A:",
|
| 156 |
-
"Final answer:", "Final
|
| 157 |
-
"
|
| 158 |
]
|
| 159 |
for p in prefixes:
|
| 160 |
-
if answer.
|
|
|
|
|
|
|
| 161 |
answer = answer[len(p):].strip()
|
| 162 |
|
| 163 |
-
# Remove markdown
|
| 164 |
answer = answer.replace("**", "").replace("```", "").strip()
|
| 165 |
-
answer = answer.strip('"\'')
|
| 166 |
|
| 167 |
-
#
|
| 168 |
-
if "
|
| 169 |
-
|
|
|
|
| 170 |
|
| 171 |
# Remove trailing period for short answers
|
| 172 |
if answer.endswith('.') and len(answer.split()) <= 5:
|
| 173 |
answer = answer[:-1]
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
return answer.strip()
|
| 176 |
|
| 177 |
def __call__(self, question: str, task_id: str = None) -> str:
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
# 2
|
| 187 |
-
file_info = {"has_file": False}
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
-
if
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
else:
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
-
|
| 208 |
-
if not file_info.get("has_file") or file_info.get("is_image"):
|
| 209 |
search_triggers = [
|
| 210 |
-
"who ", "
|
| 211 |
-
"
|
| 212 |
-
"
|
| 213 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
]
|
| 215 |
|
| 216 |
if any(t in question.lower() for t in search_triggers):
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
-
# 4
|
| 222 |
-
context = "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
-
prompt = f"""
|
| 225 |
|
| 226 |
RULES:
|
| 227 |
-
-
|
| 228 |
-
-
|
| 229 |
-
- If
|
| 230 |
-
- If
|
| 231 |
-
- If it's a list,
|
| 232 |
-
-
|
|
|
|
| 233 |
|
| 234 |
-
|
| 235 |
|
| 236 |
-
|
| 237 |
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
answer = self.ask(prompt)
|
| 241 |
return self.clean_answer(answer)
|
| 242 |
|
| 243 |
|
|
@@ -255,7 +316,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 255 |
if not os.environ.get("GROQ_API_KEY"):
|
| 256 |
return "β Add GROQ_API_KEY to secrets!", None
|
| 257 |
|
| 258 |
-
print(f"\n{'='*
|
|
|
|
|
|
|
| 259 |
|
| 260 |
try:
|
| 261 |
agent = BasicAgent()
|
|
@@ -265,7 +328,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 265 |
try:
|
| 266 |
questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15).json()
|
| 267 |
print(f"π {len(questions)} questions")
|
| 268 |
-
print(f"β±οΈ
|
| 269 |
except Exception as e:
|
| 270 |
return f"β Fetch failed: {e}", None
|
| 271 |
|
|
@@ -277,21 +340,19 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 277 |
task_id = q.get("task_id")
|
| 278 |
question = q.get("question", "")
|
| 279 |
|
| 280 |
-
print(f"[{i+1}/{len(questions)}] {question[:
|
| 281 |
|
| 282 |
answer = agent(question, task_id)
|
| 283 |
-
print(f"
|
| 284 |
|
| 285 |
answers.append({"task_id": task_id, "submitted_answer": answer})
|
| 286 |
-
results.append({"#": i+1, "
|
| 287 |
|
| 288 |
-
# Delay to avoid rate limits
|
| 289 |
if i < len(questions) - 1:
|
| 290 |
-
print(f" β³ Waiting {DELAY_BETWEEN_QUESTIONS}s...")
|
| 291 |
time.sleep(DELAY_BETWEEN_QUESTIONS)
|
| 292 |
|
| 293 |
total = time.time() - start
|
| 294 |
-
print(f"\nβ±οΈ Total: {total
|
| 295 |
|
| 296 |
try:
|
| 297 |
result = requests.post(
|
|
@@ -308,8 +369,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 308 |
correct = result.get('correct_count', 0)
|
| 309 |
total_q = result.get('total_attempted', 0)
|
| 310 |
|
| 311 |
-
status = f"β
Done in {total/60:.1f} min\n\n
|
| 312 |
-
status += "
|
|
|
|
| 313 |
|
| 314 |
return status, pd.DataFrame(results)
|
| 315 |
except Exception as e:
|
|
@@ -323,18 +385,25 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 323 |
with gr.Blocks() as demo:
|
| 324 |
gr.Markdown("# π― GAIA Agent - Unit 4")
|
| 325 |
gr.Markdown("""
|
| 326 |
-
**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
|
| 328 |
-
β±οΈ
|
| 329 |
""")
|
| 330 |
|
| 331 |
gr.LoginButton()
|
| 332 |
-
run_btn = gr.Button("π Run", variant="primary", size="lg")
|
| 333 |
-
status = gr.Textbox(label="Status", lines=
|
| 334 |
table = gr.DataFrame(label="Results")
|
| 335 |
|
| 336 |
run_btn.click(run_and_submit_all, outputs=[status, table])
|
| 337 |
|
| 338 |
if __name__ == "__main__":
|
|
|
|
| 339 |
print(f"GROQ_API_KEY: {'β
' if os.environ.get('GROQ_API_KEY') else 'β'}")
|
| 340 |
demo.launch()
|
|
|
|
| 9 |
|
| 10 |
# --- Constants ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
+
DELAY_BETWEEN_QUESTIONS = 15
|
| 13 |
|
| 14 |
# ============================================
|
| 15 |
# GROQ CLIENT
|
|
|
|
| 26 |
# ============================================
|
| 27 |
|
| 28 |
def web_search(query: str) -> str:
|
| 29 |
+
"""Search with DuckDuckGo"""
|
| 30 |
try:
|
| 31 |
with DDGS() as ddgs:
|
| 32 |
results = list(ddgs.text(query, max_results=5))
|
|
|
|
| 34 |
return ""
|
| 35 |
output = []
|
| 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 get_task_file(task_id: str) -> dict:
|
| 44 |
+
"""Get file associated with a GAIA task"""
|
| 45 |
try:
|
| 46 |
url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
|
| 47 |
+
response = requests.get(url, timeout=20)
|
| 48 |
|
| 49 |
if response.status_code == 404:
|
| 50 |
return {"has_file": False}
|
|
|
|
| 58 |
|
| 59 |
result = {"has_file": True, "filename": filename, "type": content_type}
|
| 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/CSV/JSON files
|
| 68 |
if 'text' in content_type or filename.endswith(('.txt', '.md', '.csv', '.json')):
|
| 69 |
+
result["content"] = response.text[:8000]
|
| 70 |
+
result["file_type"] = "text"
|
| 71 |
return result
|
| 72 |
|
| 73 |
+
# Excel files
|
| 74 |
if 'excel' in content_type or 'spreadsheet' in content_type or filename.endswith(('.xlsx', '.xls')):
|
| 75 |
try:
|
| 76 |
from io import BytesIO
|
| 77 |
df = pd.read_excel(BytesIO(response.content))
|
| 78 |
result["content"] = df.to_string()
|
| 79 |
+
result["dataframe"] = df
|
| 80 |
+
result["file_type"] = "excel"
|
| 81 |
return result
|
| 82 |
except Exception as e:
|
| 83 |
+
result["content"] = f"Excel error: {e}"
|
| 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"] = "[Cannot process image]"
|
| 91 |
return result
|
| 92 |
|
| 93 |
+
# MP3/Audio
|
| 94 |
+
if 'audio' in content_type or filename.endswith(('.mp3', '.wav')):
|
| 95 |
+
result["file_type"] = "audio"
|
| 96 |
+
result["content"] = "[Cannot process audio]"
|
| 97 |
+
return result
|
| 98 |
+
|
| 99 |
+
result["content"] = response.text[:5000] if len(response.content) < 50000 else "[Large binary file]"
|
| 100 |
+
result["file_type"] = "other"
|
| 101 |
return result
|
| 102 |
+
|
| 103 |
except Exception as e:
|
| 104 |
return {"has_file": False, "error": str(e)}
|
| 105 |
|
| 106 |
|
| 107 |
+
def execute_python_code(code: str) -> str:
|
| 108 |
+
"""Execute Python code and capture ALL output"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
try:
|
| 110 |
import io
|
| 111 |
import sys
|
|
|
|
| 112 |
|
| 113 |
+
# Create string buffer for stdout
|
| 114 |
+
old_stdout = sys.stdout
|
| 115 |
+
sys.stdout = buffer = io.StringIO()
|
|
|
|
| 116 |
|
| 117 |
+
# Execute the code
|
| 118 |
+
exec_globals = {
|
| 119 |
+
'__builtins__': __builtins__,
|
| 120 |
+
'print': print,
|
| 121 |
+
}
|
| 122 |
+
exec(code, exec_globals)
|
| 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"Execution error: {e}"
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def reverse_string(text: str) -> str:
|
| 134 |
+
"""Reverse a string"""
|
| 135 |
+
return text[::-1]
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def is_reversed_text(text: str) -> bool:
|
| 139 |
+
"""Check if text appears to be reversed"""
|
| 140 |
+
# Common reversed English patterns
|
| 141 |
+
indicators = ['.rewsna', 'eht sa', 'tfel', 'drow eht', 'etisoppo', 'siht']
|
| 142 |
+
text_lower = text.lower()
|
| 143 |
+
return any(ind in text_lower for ind in indicators)
|
| 144 |
|
| 145 |
|
| 146 |
# ============================================
|
|
|
|
| 149 |
|
| 150 |
class BasicAgent:
|
| 151 |
def __init__(self):
|
| 152 |
+
print("Initializing agent...")
|
| 153 |
self.client = get_groq_client()
|
| 154 |
print("β
Ready!")
|
| 155 |
|
| 156 |
+
def ask_llm(self, prompt: str, max_retries: int = 3) -> str:
|
| 157 |
+
"""Query the LLM with retry logic"""
|
| 158 |
for attempt in range(max_retries):
|
| 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:
|
|
|
|
| 170 |
print(f" β³ Rate limit, waiting {wait}s...")
|
| 171 |
time.sleep(wait)
|
| 172 |
else:
|
| 173 |
+
print(f" β LLM error: {e}")
|
| 174 |
+
return "unknown"
|
| 175 |
return "unknown"
|
| 176 |
|
| 177 |
def clean_answer(self, answer: str) -> str:
|
| 178 |
+
"""Clean up the answer to exact match format"""
|
| 179 |
+
if not answer:
|
| 180 |
+
return "unknown"
|
| 181 |
+
|
| 182 |
# Remove common prefixes
|
| 183 |
prefixes = [
|
| 184 |
+
"Answer:", "The answer is:", "The answer is", "A:", "**Answer:**",
|
| 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 |
+
# Remove markdown formatting
|
| 195 |
answer = answer.replace("**", "").replace("```", "").strip()
|
|
|
|
| 196 |
|
| 197 |
+
# Remove surrounding quotes
|
| 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 |
+
"""Process a question and return the answer"""
|
| 215 |
+
|
| 216 |
+
# === STEP 1: Handle reversed text ===
|
| 217 |
+
if is_reversed_text(question):
|
| 218 |
+
decoded = reverse_string(question)
|
| 219 |
+
print(f" [Reversed text detected, decoded]")
|
| 220 |
+
question = decoded
|
| 221 |
+
|
| 222 |
+
# === STEP 2: Get associated file ===
|
| 223 |
+
file_info = get_task_file(task_id) if task_id else {"has_file": False}
|
| 224 |
+
file_context = ""
|
| 225 |
+
|
| 226 |
+
if file_info.get("has_file"):
|
| 227 |
+
file_type = file_info.get("file_type", "")
|
| 228 |
+
filename = file_info.get("filename", "")
|
| 229 |
+
print(f" [File: {filename} ({file_type})]")
|
| 230 |
|
| 231 |
+
if file_type == "python":
|
| 232 |
+
# Execute Python code and get output
|
| 233 |
+
code = file_info.get("content", "")
|
| 234 |
+
output = execute_python_code(code)
|
| 235 |
+
print(f" [Python output: {output[:50]}...]")
|
| 236 |
+
file_context = f"Python code output:\n{output}"
|
| 237 |
+
|
| 238 |
+
elif file_type == "excel":
|
| 239 |
+
df = file_info.get("dataframe")
|
| 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 file_type in ["image", "audio"]:
|
| 250 |
+
file_context = f"[This task has a {file_type} file which cannot be processed]"
|
| 251 |
+
|
| 252 |
+
# === STEP 3: Web search if needed ===
|
| 253 |
+
search_context = ""
|
| 254 |
+
needs_search = not file_info.get("has_file") or file_info.get("file_type") in ["image", "audio"]
|
| 255 |
|
| 256 |
+
if needs_search:
|
|
|
|
| 257 |
search_triggers = [
|
| 258 |
+
"who is", "who was", "who did", "who nominated", "who played",
|
| 259 |
+
"what is", "what was", "what are",
|
| 260 |
+
"how many", "how much",
|
| 261 |
+
"where ", "when ",
|
| 262 |
+
"surname", "first name", "name of",
|
| 263 |
+
"album", "studio album", "mercedes sosa",
|
| 264 |
+
"actor", "actress", "movie", "film",
|
| 265 |
+
"wikipedia", "article",
|
| 266 |
+
"athlete", "pitcher", "yankee", "player",
|
| 267 |
+
"country", "competition", "malko",
|
| 268 |
+
"veterinarian", "equine"
|
| 269 |
]
|
| 270 |
|
| 271 |
if any(t in question.lower() for t in search_triggers):
|
| 272 |
+
# Create focused search query
|
| 273 |
+
search_query = question[:120]
|
| 274 |
+
results = web_search(search_query)
|
| 275 |
+
if results:
|
| 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.ask_llm(prompt)
|
|
|
|
|
|
|
| 302 |
return self.clean_answer(answer)
|
| 303 |
|
| 304 |
|
|
|
|
| 316 |
if not os.environ.get("GROQ_API_KEY"):
|
| 317 |
return "β Add GROQ_API_KEY to secrets!", None
|
| 318 |
|
| 319 |
+
print(f"\n{'='*50}")
|
| 320 |
+
print(f"User: {username}")
|
| 321 |
+
print(f"{'='*50}")
|
| 322 |
|
| 323 |
try:
|
| 324 |
agent = BasicAgent()
|
|
|
|
| 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 |
task_id = q.get("task_id")
|
| 341 |
question = q.get("question", "")
|
| 342 |
|
| 343 |
+
print(f"\n[{i+1}/{len(questions)}] {question[:60]}...")
|
| 344 |
|
| 345 |
answer = agent(question, task_id)
|
| 346 |
+
print(f" β Answer: {answer}")
|
| 347 |
|
| 348 |
answers.append({"task_id": task_id, "submitted_answer": answer})
|
| 349 |
+
results.append({"#": i+1, "Question": question[:50]+"...", "Answer": answer})
|
| 350 |
|
|
|
|
| 351 |
if i < len(questions) - 1:
|
|
|
|
| 352 |
time.sleep(DELAY_BETWEEN_QUESTIONS)
|
| 353 |
|
| 354 |
total = time.time() - start
|
| 355 |
+
print(f"\nβ±οΈ Total: {total/60:.1f} min")
|
| 356 |
|
| 357 |
try:
|
| 358 |
result = requests.post(
|
|
|
|
| 369 |
correct = result.get('correct_count', 0)
|
| 370 |
total_q = result.get('total_attempted', 0)
|
| 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! 30%+ achieved!" if score >= 30 else f"π Need {30-score}% more to pass"
|
| 375 |
|
| 376 |
return status, pd.DataFrame(results)
|
| 377 |
except Exception as e:
|
|
|
|
| 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 Evaluation", variant="primary", size="lg")
|
| 401 |
+
status = gr.Textbox(label="Status", lines=6)
|
| 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()
|