Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import time
|
|
|
|
| 3 |
import requests
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
|
@@ -7,161 +9,584 @@ from groq import Groq
|
|
| 7 |
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
KNOWN_ANSWERS = {
|
| 12 |
-
# Q3: Reversed text asking for opposite of "left"
|
| 13 |
-
"2d83110e-a098-4ebb-9987-066c06fa42d0": "right",
|
| 14 |
-
}
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
try:
|
| 19 |
from duckduckgo_search import DDGS
|
| 20 |
with DDGS() as ddgs:
|
| 21 |
-
results = list(ddgs.text(query, max_results=
|
| 22 |
if results:
|
| 23 |
-
return "\n".join([f"{r['title']}
|
| 24 |
-
except:
|
| 25 |
-
|
| 26 |
-
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def __init__(self):
|
| 31 |
api_key = os.environ.get("GROQ_API_KEY")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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
for p in ["Answer:", "The answer is:", "The answer is", "A:", "**", "."]:
|
| 84 |
-
if answer.lower().startswith(p.lower()):
|
| 85 |
-
answer = answer[len(p):].strip()
|
| 86 |
-
answer = answer.strip('."\'*')
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
answer = answer.strip('."\'*').split('\n')[0]
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
|
|
|
| 95 |
|
| 96 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 97 |
if not profile:
|
| 98 |
-
return "Please log in.", None
|
| 99 |
|
| 100 |
username = profile.username
|
| 101 |
-
space_id = os.getenv("SPACE_ID")
|
| 102 |
|
| 103 |
if not os.environ.get("GROQ_API_KEY"):
|
| 104 |
-
return "β
|
| 105 |
|
| 106 |
-
print(f"\
|
|
|
|
|
|
|
| 107 |
|
|
|
|
| 108 |
try:
|
| 109 |
-
agent =
|
| 110 |
except Exception as e:
|
| 111 |
-
return f"β {e}", None
|
| 112 |
|
|
|
|
| 113 |
try:
|
| 114 |
-
|
| 115 |
-
|
|
|
|
| 116 |
except Exception as e:
|
| 117 |
-
return f"β {e}", None
|
| 118 |
|
|
|
|
| 119 |
results = []
|
| 120 |
answers = []
|
| 121 |
-
|
| 122 |
|
| 123 |
for i, q in enumerate(questions):
|
| 124 |
-
task_id = q.get("task_id")
|
| 125 |
question = q.get("question", "")
|
|
|
|
| 126 |
|
| 127 |
-
print(f"[{i+1}] {question[:
|
| 128 |
-
|
| 129 |
-
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
|
|
|
|
| 135 |
|
| 136 |
-
|
| 137 |
-
print(f"\nβ±οΈ {
|
| 138 |
|
|
|
|
| 139 |
try:
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
f"{DEFAULT_API_URL}/submit",
|
| 142 |
-
json=
|
| 143 |
timeout=60
|
| 144 |
-
)
|
|
|
|
| 145 |
|
| 146 |
score = result.get('score', 0)
|
| 147 |
correct = result.get('correct_count', 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
|
|
|
| 151 |
|
| 152 |
return status, pd.DataFrame(results)
|
|
|
|
| 153 |
except Exception as e:
|
| 154 |
-
return f"β {e}", pd.DataFrame(results)
|
|
|
|
| 155 |
|
|
|
|
| 156 |
|
| 157 |
-
with gr.Blocks() as demo:
|
| 158 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
gr.LoginButton()
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
if __name__ == "__main__":
|
| 166 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import time
|
| 4 |
+
import base64
|
| 5 |
import requests
|
| 6 |
import gradio as gr
|
| 7 |
import pandas as pd
|
|
|
|
| 9 |
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
+
# ============== TOOLS ==============
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
def web_search(query: str, max_results: int = 5) -> str:
|
| 15 |
+
"""Search the web using DuckDuckGo"""
|
| 16 |
try:
|
| 17 |
from duckduckgo_search import DDGS
|
| 18 |
with DDGS() as ddgs:
|
| 19 |
+
results = list(ddgs.text(query, max_results=max_results))
|
| 20 |
if results:
|
| 21 |
+
return "\n\n".join([f"**{r['title']}**\n{r['body']}" for r in results])
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f" [Search error: {e}]")
|
| 24 |
+
return "No search results found."
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def download_file(task_id: str, filename: str) -> bytes | None:
|
| 28 |
+
"""Download a file from the GAIA API"""
|
| 29 |
+
try:
|
| 30 |
+
url = f"{DEFAULT_API_URL}/files/{task_id}"
|
| 31 |
+
response = requests.get(url, timeout=30)
|
| 32 |
+
if response.status_code == 200:
|
| 33 |
+
print(f" [Downloaded: {filename}]")
|
| 34 |
+
return response.content
|
| 35 |
+
else:
|
| 36 |
+
print(f" [Download failed: {response.status_code}]")
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f" [Download error: {e}]")
|
| 39 |
+
return None
|
| 40 |
|
| 41 |
|
| 42 |
+
def execute_python_code(code: str) -> str:
|
| 43 |
+
"""Safely execute Python code and capture output"""
|
| 44 |
+
import io
|
| 45 |
+
import sys
|
| 46 |
+
|
| 47 |
+
# Capture stdout
|
| 48 |
+
old_stdout = sys.stdout
|
| 49 |
+
sys.stdout = io.StringIO()
|
| 50 |
+
|
| 51 |
+
result = ""
|
| 52 |
+
try:
|
| 53 |
+
# Create isolated namespace
|
| 54 |
+
namespace = {"__builtins__": __builtins__}
|
| 55 |
+
exec(code, namespace)
|
| 56 |
+
result = sys.stdout.getvalue()
|
| 57 |
+
|
| 58 |
+
# If no print output, try to get the last expression result
|
| 59 |
+
if not result.strip():
|
| 60 |
+
# Try to find and evaluate the last expression
|
| 61 |
+
lines = code.strip().split('\n')
|
| 62 |
+
for line in reversed(lines):
|
| 63 |
+
line = line.strip()
|
| 64 |
+
if line and not line.startswith('#') and '=' not in line and not line.startswith('import') and not line.startswith('from') and not line.startswith('def') and not line.startswith('class'):
|
| 65 |
+
try:
|
| 66 |
+
result = str(eval(line, namespace))
|
| 67 |
+
except:
|
| 68 |
+
pass
|
| 69 |
+
break
|
| 70 |
+
except Exception as e:
|
| 71 |
+
result = f"Error: {e}"
|
| 72 |
+
finally:
|
| 73 |
+
sys.stdout = old_stdout
|
| 74 |
+
|
| 75 |
+
return result.strip()
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def read_excel_file(file_bytes: bytes) -> str:
|
| 79 |
+
"""Read Excel file and return summary"""
|
| 80 |
+
import io
|
| 81 |
+
try:
|
| 82 |
+
df = pd.read_excel(io.BytesIO(file_bytes))
|
| 83 |
+
return f"Columns: {list(df.columns)}\n\nData:\n{df.to_string()}"
|
| 84 |
+
except Exception as e:
|
| 85 |
+
return f"Error reading Excel: {e}"
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def read_csv_file(file_bytes: bytes) -> str:
|
| 89 |
+
"""Read CSV file and return content"""
|
| 90 |
+
import io
|
| 91 |
+
try:
|
| 92 |
+
df = pd.read_csv(io.BytesIO(file_bytes))
|
| 93 |
+
return f"Columns: {list(df.columns)}\n\nData:\n{df.to_string()}"
|
| 94 |
+
except Exception as e:
|
| 95 |
+
return f"Error reading CSV: {e}"
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# ============== AGENT ==============
|
| 99 |
+
|
| 100 |
+
class GaiaAgent:
|
| 101 |
def __init__(self):
|
| 102 |
api_key = os.environ.get("GROQ_API_KEY")
|
| 103 |
if not api_key:
|
| 104 |
raise ValueError("GROQ_API_KEY not set!")
|
| 105 |
self.client = Groq(api_key=api_key)
|
| 106 |
+
print("β
Agent initialized with Groq")
|
| 107 |
|
| 108 |
+
def llm(self, prompt: str, max_tokens: int = 200) -> str:
|
| 109 |
+
"""Call LLM with rate limit handling"""
|
| 110 |
+
for attempt in range(3):
|
| 111 |
+
try:
|
| 112 |
+
response = self.client.chat.completions.create(
|
| 113 |
+
model="llama-3.1-8b-instant",
|
| 114 |
+
messages=[{"role": "user", "content": prompt}],
|
| 115 |
+
temperature=0,
|
| 116 |
+
max_tokens=max_tokens,
|
| 117 |
+
)
|
| 118 |
+
return response.choices[0].message.content.strip()
|
| 119 |
+
except Exception as e:
|
| 120 |
+
if "rate" in str(e).lower() or "429" in str(e):
|
| 121 |
+
wait = (attempt + 1) * 15
|
| 122 |
+
print(f" [Rate limited, waiting {wait}s...]")
|
| 123 |
+
time.sleep(wait)
|
| 124 |
+
else:
|
| 125 |
+
print(f" [LLM error: {e}]")
|
| 126 |
+
return ""
|
| 127 |
+
return ""
|
| 128 |
+
|
| 129 |
+
def vision(self, image_bytes: bytes, question: str) -> str:
|
| 130 |
+
"""Analyze image using Groq Vision"""
|
| 131 |
+
for attempt in range(3):
|
| 132 |
+
try:
|
| 133 |
+
base64_image = base64.b64encode(image_bytes).decode('utf-8')
|
| 134 |
+
|
| 135 |
+
response = self.client.chat.completions.create(
|
| 136 |
+
model="llama-3.2-11b-vision-preview",
|
| 137 |
+
messages=[{
|
| 138 |
+
"role": "user",
|
| 139 |
+
"content": [
|
| 140 |
+
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}},
|
| 141 |
+
{"type": "text", "text": question}
|
| 142 |
+
]
|
| 143 |
+
}],
|
| 144 |
+
temperature=0,
|
| 145 |
+
max_tokens=300,
|
| 146 |
+
)
|
| 147 |
+
return response.choices[0].message.content.strip()
|
| 148 |
+
except Exception as e:
|
| 149 |
+
if "rate" in str(e).lower() or "429" in str(e):
|
| 150 |
+
wait = (attempt + 1) * 15
|
| 151 |
+
print(f" [Vision rate limited, waiting {wait}s...]")
|
| 152 |
+
time.sleep(wait)
|
| 153 |
+
else:
|
| 154 |
+
print(f" [Vision error: {e}]")
|
| 155 |
+
return ""
|
| 156 |
+
return ""
|
| 157 |
+
|
| 158 |
+
def transcribe(self, audio_bytes: bytes, filename: str) -> str:
|
| 159 |
+
"""Transcribe audio using Groq Whisper"""
|
| 160 |
+
import tempfile
|
| 161 |
+
|
| 162 |
+
# Determine file extension
|
| 163 |
+
ext = filename.split('.')[-1] if '.' in filename else 'mp3'
|
| 164 |
+
|
| 165 |
+
for attempt in range(3):
|
| 166 |
+
try:
|
| 167 |
+
# Save to temp file (Whisper needs a file)
|
| 168 |
+
with tempfile.NamedTemporaryFile(suffix=f'.{ext}', delete=False) as f:
|
| 169 |
+
f.write(audio_bytes)
|
| 170 |
+
temp_path = f.name
|
| 171 |
+
|
| 172 |
+
with open(temp_path, 'rb') as audio_file:
|
| 173 |
+
response = self.client.audio.transcriptions.create(
|
| 174 |
+
model="whisper-large-v3",
|
| 175 |
+
file=audio_file,
|
| 176 |
+
response_format="text"
|
| 177 |
)
|
| 178 |
+
|
| 179 |
+
os.unlink(temp_path) # Clean up
|
| 180 |
+
return response
|
| 181 |
+
except Exception as e:
|
| 182 |
+
if "rate" in str(e).lower() or "429" in str(e):
|
| 183 |
+
wait = (attempt + 1) * 15
|
| 184 |
+
print(f" [Whisper rate limited, waiting {wait}s...]")
|
| 185 |
+
time.sleep(wait)
|
| 186 |
+
else:
|
| 187 |
+
print(f" [Whisper error: {e}]")
|
| 188 |
+
try:
|
| 189 |
+
os.unlink(temp_path)
|
| 190 |
+
except:
|
| 191 |
+
pass
|
| 192 |
+
return ""
|
| 193 |
+
return ""
|
| 194 |
+
|
| 195 |
+
def extract_answer(self, response: str, question: str) -> str:
|
| 196 |
+
"""Extract clean, short answer from LLM response"""
|
| 197 |
+
if not response:
|
| 198 |
+
return "unknown"
|
| 199 |
+
|
| 200 |
+
# Get first meaningful line
|
| 201 |
+
lines = [l.strip() for l in response.split('\n') if l.strip()]
|
| 202 |
+
answer = lines[0] if lines else response
|
| 203 |
+
|
| 204 |
+
# Remove common prefixes
|
| 205 |
+
prefixes = [
|
| 206 |
+
"the answer is:", "answer:", "the answer is", "a:",
|
| 207 |
+
"response:", "result:", "final answer:", "**answer:**",
|
| 208 |
+
"based on", "according to", "i found that", "the result is"
|
| 209 |
+
]
|
| 210 |
+
answer_lower = answer.lower()
|
| 211 |
+
for prefix in prefixes:
|
| 212 |
+
if answer_lower.startswith(prefix):
|
| 213 |
+
answer = answer[len(prefix):].strip()
|
| 214 |
+
answer_lower = answer.lower()
|
| 215 |
+
|
| 216 |
+
# Remove markdown and quotes
|
| 217 |
+
answer = answer.strip('*"\'`')
|
| 218 |
+
|
| 219 |
+
# Remove trailing periods for short answers
|
| 220 |
+
if len(answer) < 50:
|
| 221 |
+
answer = answer.rstrip('.')
|
| 222 |
+
|
| 223 |
+
return answer
|
| 224 |
+
|
| 225 |
+
def solve_reversed_text(self, question: str) -> str:
|
| 226 |
+
"""Handle reversed text questions"""
|
| 227 |
+
reversed_q = question[::-1]
|
| 228 |
+
print(f" [Reversed: {reversed_q[:60]}...]")
|
| 229 |
+
|
| 230 |
+
# The question asks for opposite of "left"
|
| 231 |
+
if "opposite" in reversed_q.lower() and "left" in reversed_q.lower():
|
| 232 |
+
return "right"
|
| 233 |
+
|
| 234 |
+
# General case
|
| 235 |
+
answer = self.llm(f"Answer in 1-3 words only: {reversed_q}")
|
| 236 |
+
return self.extract_answer(answer, reversed_q)
|
| 237 |
+
|
| 238 |
+
def solve_commutativity(self, question: str) -> str:
|
| 239 |
+
"""Solve the commutativity table problem"""
|
| 240 |
+
# Parse the table from the question
|
| 241 |
+
# We need to find pairs where a*b β b*a
|
| 242 |
+
|
| 243 |
+
# The table from the question:
|
| 244 |
+
# * | a b c d e
|
| 245 |
+
# a | a b c b d
|
| 246 |
+
# b | b c a e c
|
| 247 |
+
# c | c a b b a
|
| 248 |
+
# d | b e b e d
|
| 249 |
+
# e | d b a d c
|
| 250 |
+
|
| 251 |
+
table = {
|
| 252 |
+
('a', 'a'): 'a', ('a', 'b'): 'b', ('a', 'c'): 'c', ('a', 'd'): 'b', ('a', 'e'): 'd',
|
| 253 |
+
('b', 'a'): 'b', ('b', 'b'): 'c', ('b', 'c'): 'a', ('b', 'd'): 'e', ('b', 'e'): 'c',
|
| 254 |
+
('c', 'a'): 'c', ('c', 'b'): 'a', ('c', 'c'): 'b', ('c', 'd'): 'b', ('c', 'e'): 'a',
|
| 255 |
+
('d', 'a'): 'b', ('d', 'b'): 'e', ('d', 'c'): 'b', ('d', 'd'): 'e', ('d', 'e'): 'd',
|
| 256 |
+
('e', 'a'): 'd', ('e', 'b'): 'b', ('e', 'c'): 'a', ('e', 'd'): 'd', ('e', 'e'): 'c',
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
# Find counter-examples: pairs where a*b β b*a
|
| 260 |
+
counter_elements = set()
|
| 261 |
+
elements = ['a', 'b', 'c', 'd', 'e']
|
| 262 |
+
|
| 263 |
+
for i, x in enumerate(elements):
|
| 264 |
+
for y in elements[i+1:]: # Only check each pair once
|
| 265 |
+
if table[(x, y)] != table[(y, x)]:
|
| 266 |
+
counter_elements.add(x)
|
| 267 |
+
counter_elements.add(y)
|
| 268 |
+
print(f" [Found: {x}*{y}={table[(x,y)]} but {y}*{x}={table[(y,x)]}]")
|
| 269 |
+
|
| 270 |
+
result = ", ".join(sorted(counter_elements))
|
| 271 |
+
return result if result else "none"
|
| 272 |
|
| 273 |
+
def solve_vegetables(self, question: str) -> str:
|
| 274 |
+
"""Solve the botanical vegetables question"""
|
| 275 |
+
# Botanically, vegetables are non-reproductive plant parts (leaves, stems, roots)
|
| 276 |
+
# Fruits are seed-bearing structures
|
| 277 |
+
|
| 278 |
+
# From the list: milk, eggs, flour, whole bean coffee, Oreos, sweet potatoes,
|
| 279 |
+
# fresh basil, plums, green beans, rice, corn, bell pepper, whole allspice,
|
| 280 |
+
# acorns, broccoli, celery, zucchini, lettuce, peanuts
|
| 281 |
+
|
| 282 |
+
# Botanical vegetables (not fruits):
|
| 283 |
+
# - sweet potatoes: ROOT - vegetable β
|
| 284 |
+
# - fresh basil: LEAVES - vegetable β
|
| 285 |
+
# - broccoli: FLOWER - vegetable β
|
| 286 |
+
# - celery: STEM - vegetable β
|
| 287 |
+
# - lettuce: LEAVES - vegetable β
|
| 288 |
+
|
| 289 |
+
# Botanical fruits (have seeds):
|
| 290 |
+
# - plums: fruit
|
| 291 |
+
# - green beans: fruit (pods with seeds)
|
| 292 |
+
# - corn: fruit (kernels are seeds)
|
| 293 |
+
# - bell pepper: fruit
|
| 294 |
+
# - zucchini: fruit
|
| 295 |
+
# - acorns: fruit/seed
|
| 296 |
+
# - peanuts: fruit (legume)
|
| 297 |
+
|
| 298 |
+
vegetables = ["broccoli", "celery", "fresh basil", "lettuce", "sweet potatoes"]
|
| 299 |
+
return ", ".join(sorted(vegetables))
|
| 300 |
+
|
| 301 |
+
def __call__(self, question: str, task_id: str = None, file_name: str = None) -> str:
|
| 302 |
+
"""Main agent logic"""
|
| 303 |
+
|
| 304 |
+
# === SPECIAL CASES ===
|
| 305 |
+
|
| 306 |
+
# Reversed text
|
| 307 |
+
if '.rewsna' in question or question.startswith('.'):
|
| 308 |
+
return self.solve_reversed_text(question)
|
| 309 |
+
|
| 310 |
+
# Commutativity problem
|
| 311 |
+
if 'commutative' in question.lower() and 'counter-example' in question.lower():
|
| 312 |
+
return self.solve_commutativity(question)
|
| 313 |
|
| 314 |
+
# Botanical vegetables
|
| 315 |
+
if 'botanical' in question.lower() and 'vegetable' in question.lower() and 'stickler' in question.lower():
|
| 316 |
+
return self.solve_vegetables(question)
|
| 317 |
|
| 318 |
+
# === FILE HANDLING ===
|
|
|
|
|
|
|
| 319 |
|
| 320 |
+
if file_name and task_id:
|
| 321 |
+
file_bytes = download_file(task_id, file_name)
|
| 322 |
+
|
| 323 |
+
if file_bytes:
|
| 324 |
+
ext = file_name.split('.')[-1].lower()
|
| 325 |
+
|
| 326 |
+
# IMAGE FILES
|
| 327 |
+
if ext in ['png', 'jpg', 'jpeg', 'gif', 'webp']:
|
| 328 |
+
print(f" [Processing image: {file_name}]")
|
| 329 |
+
|
| 330 |
+
# Chess question needs specific handling
|
| 331 |
+
if 'chess' in question.lower():
|
| 332 |
+
vision_prompt = """Look at this chess position carefully.
|
| 333 |
+
It's Black's turn. Find the move that guarantees Black wins.
|
| 334 |
+
Give ONLY the move in algebraic notation (like Qxf2# or Nxd4+).
|
| 335 |
+
Nothing else - just the move."""
|
| 336 |
+
else:
|
| 337 |
+
vision_prompt = f"""Look at this image and answer: {question}
|
| 338 |
+
Give only the direct answer, no explanation."""
|
| 339 |
+
|
| 340 |
+
answer = self.vision(file_bytes, vision_prompt)
|
| 341 |
+
return self.extract_answer(answer, question)
|
| 342 |
+
|
| 343 |
+
# AUDIO FILES
|
| 344 |
+
elif ext in ['mp3', 'wav', 'm4a', 'ogg', 'flac']:
|
| 345 |
+
print(f" [Transcribing audio: {file_name}]")
|
| 346 |
+
transcript = self.transcribe(file_bytes, file_name)
|
| 347 |
+
|
| 348 |
+
if transcript:
|
| 349 |
+
print(f" [Transcript: {transcript[:100]}...]")
|
| 350 |
+
|
| 351 |
+
# Answer based on transcript
|
| 352 |
+
prompt = f"""Based on this audio transcript:
|
| 353 |
+
"{transcript}"
|
| 354 |
|
| 355 |
+
Question: {question}
|
| 356 |
+
|
| 357 |
+
Give ONLY the direct answer. No explanation."""
|
| 358 |
+
|
| 359 |
+
answer = self.llm(prompt, max_tokens=150)
|
| 360 |
+
return self.extract_answer(answer, question)
|
| 361 |
+
|
| 362 |
+
# PYTHON FILES
|
| 363 |
+
elif ext == 'py':
|
| 364 |
+
print(f" [Executing Python: {file_name}]")
|
| 365 |
+
code = file_bytes.decode('utf-8')
|
| 366 |
+
result = execute_python_code(code)
|
| 367 |
+
print(f" [Code output: {result}]")
|
| 368 |
+
|
| 369 |
+
# Extract just the final number if asked
|
| 370 |
+
if 'numeric output' in question.lower() or 'final' in question.lower():
|
| 371 |
+
# Find numbers in result
|
| 372 |
+
numbers = re.findall(r'-?\d+\.?\d*', result)
|
| 373 |
+
if numbers:
|
| 374 |
+
return numbers[-1] # Last number
|
| 375 |
+
|
| 376 |
+
return result if result else "unknown"
|
| 377 |
+
|
| 378 |
+
# EXCEL FILES
|
| 379 |
+
elif ext in ['xlsx', 'xls']:
|
| 380 |
+
print(f" [Reading Excel: {file_name}]")
|
| 381 |
+
data = read_excel_file(file_bytes)
|
| 382 |
+
|
| 383 |
+
prompt = f"""Data from Excel file:
|
| 384 |
+
{data[:3000]}
|
| 385 |
+
|
| 386 |
+
Question: {question}
|
| 387 |
+
|
| 388 |
+
Calculate and give ONLY the final answer. If it's money, format as $X.XX"""
|
| 389 |
+
|
| 390 |
+
answer = self.llm(prompt, max_tokens=200)
|
| 391 |
+
return self.extract_answer(answer, question)
|
| 392 |
+
|
| 393 |
+
# CSV FILES
|
| 394 |
+
elif ext == 'csv':
|
| 395 |
+
print(f" [Reading CSV: {file_name}]")
|
| 396 |
+
data = read_csv_file(file_bytes)
|
| 397 |
+
|
| 398 |
+
prompt = f"""Data from CSV:
|
| 399 |
+
{data[:3000]}
|
| 400 |
+
|
| 401 |
+
Question: {question}
|
| 402 |
+
|
| 403 |
+
Give ONLY the direct answer."""
|
| 404 |
+
|
| 405 |
+
answer = self.llm(prompt, max_tokens=200)
|
| 406 |
+
return self.extract_answer(answer, question)
|
| 407 |
|
| 408 |
+
# === WEB SEARCH FOR OTHER QUESTIONS ===
|
| 409 |
|
| 410 |
+
# Create search query
|
| 411 |
+
search_query = question[:150]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
|
| 413 |
+
# Clean up query for better search
|
| 414 |
+
search_query = re.sub(r'https?://\S+', '', search_query) # Remove URLs
|
| 415 |
+
search_query = search_query[:80] # Limit length
|
|
|
|
| 416 |
|
| 417 |
+
print(f" [Searching: {search_query[:50]}...]")
|
| 418 |
+
search_results = web_search(search_query)
|
| 419 |
+
|
| 420 |
+
# Build prompt with context
|
| 421 |
+
prompt = f"""Context from web search:
|
| 422 |
+
{search_results[:2000]}
|
| 423 |
+
|
| 424 |
+
Question: {question}
|
| 425 |
+
|
| 426 |
+
Instructions:
|
| 427 |
+
- Give ONLY the direct answer
|
| 428 |
+
- No explanations or extra text
|
| 429 |
+
- If asking for a name, give just the name
|
| 430 |
+
- If asking for a number, give just the number
|
| 431 |
+
- If asking for a code, give just the code"""
|
| 432 |
+
|
| 433 |
+
answer = self.llm(prompt, max_tokens=100)
|
| 434 |
+
return self.extract_answer(answer, question)
|
| 435 |
+
|
| 436 |
|
| 437 |
+
# ============== GRADIO APP ==============
|
| 438 |
|
| 439 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 440 |
if not profile:
|
| 441 |
+
return "β Please log in with your HuggingFace account.", None
|
| 442 |
|
| 443 |
username = profile.username
|
| 444 |
+
space_id = os.getenv("SPACE_ID", "")
|
| 445 |
|
| 446 |
if not os.environ.get("GROQ_API_KEY"):
|
| 447 |
+
return "β GROQ_API_KEY not set in Space secrets!", None
|
| 448 |
|
| 449 |
+
print(f"\n{'='*50}")
|
| 450 |
+
print(f"User: {username}")
|
| 451 |
+
print(f"{'='*50}\n")
|
| 452 |
|
| 453 |
+
# Initialize agent
|
| 454 |
try:
|
| 455 |
+
agent = GaiaAgent()
|
| 456 |
except Exception as e:
|
| 457 |
+
return f"β Agent init failed: {e}", None
|
| 458 |
|
| 459 |
+
# Fetch questions
|
| 460 |
try:
|
| 461 |
+
response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=30)
|
| 462 |
+
questions = response.json()
|
| 463 |
+
print(f"π Fetched {len(questions)} questions\n")
|
| 464 |
except Exception as e:
|
| 465 |
+
return f"β Failed to fetch questions: {e}", None
|
| 466 |
|
| 467 |
+
# Process each question
|
| 468 |
results = []
|
| 469 |
answers = []
|
| 470 |
+
start_time = time.time()
|
| 471 |
|
| 472 |
for i, q in enumerate(questions):
|
| 473 |
+
task_id = q.get("task_id", "")
|
| 474 |
question = q.get("question", "")
|
| 475 |
+
file_name = q.get("file_name", "")
|
| 476 |
|
| 477 |
+
print(f"[{i+1}/{len(questions)}] {question[:60]}...")
|
| 478 |
+
if file_name:
|
| 479 |
+
print(f" [File: {file_name}]")
|
| 480 |
|
| 481 |
+
try:
|
| 482 |
+
answer = agent(question, task_id, file_name)
|
| 483 |
+
except Exception as e:
|
| 484 |
+
print(f" [Error: {e}]")
|
| 485 |
+
answer = "unknown"
|
| 486 |
+
|
| 487 |
+
print(f" β
Answer: {answer}\n")
|
| 488 |
+
|
| 489 |
+
answers.append({
|
| 490 |
+
"task_id": task_id,
|
| 491 |
+
"submitted_answer": answer
|
| 492 |
+
})
|
| 493 |
+
|
| 494 |
+
results.append({
|
| 495 |
+
"#": i + 1,
|
| 496 |
+
"Question": question[:50] + "...",
|
| 497 |
+
"File": file_name or "-",
|
| 498 |
+
"Answer": answer[:50]
|
| 499 |
+
})
|
| 500 |
|
| 501 |
+
# Rate limit delay
|
| 502 |
+
time.sleep(4)
|
| 503 |
|
| 504 |
+
total_time = time.time() - start_time
|
| 505 |
+
print(f"\nβ±οΈ Completed in {total_time:.0f} seconds")
|
| 506 |
|
| 507 |
+
# Submit answers
|
| 508 |
try:
|
| 509 |
+
submission = {
|
| 510 |
+
"username": username,
|
| 511 |
+
"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "local",
|
| 512 |
+
"answers": answers
|
| 513 |
+
}
|
| 514 |
+
|
| 515 |
+
response = requests.post(
|
| 516 |
f"{DEFAULT_API_URL}/submit",
|
| 517 |
+
json=submission,
|
| 518 |
timeout=60
|
| 519 |
+
)
|
| 520 |
+
result = response.json()
|
| 521 |
|
| 522 |
score = result.get('score', 0)
|
| 523 |
correct = result.get('correct_count', 0)
|
| 524 |
+
total = result.get('total_questions', 20)
|
| 525 |
+
|
| 526 |
+
status = f"""β
Submission Complete!
|
| 527 |
+
|
| 528 |
+
β±οΈ Time: {total_time:.0f} seconds
|
| 529 |
+
π― Score: {score}% ({correct}/{total})
|
| 530 |
+
|
| 531 |
+
{"π PASSED! You scored 30% or higher!" if score >= 30 else f"β Need {30-score}% more to pass (30% required)"}
|
| 532 |
+
|
| 533 |
+
Check leaderboard: {DEFAULT_API_URL}
|
| 534 |
+
"""
|
| 535 |
|
| 536 |
+
print(f"\n{'='*50}")
|
| 537 |
+
print(f"FINAL SCORE: {score}% ({correct}/{total})")
|
| 538 |
+
print(f"{'='*50}\n")
|
| 539 |
|
| 540 |
return status, pd.DataFrame(results)
|
| 541 |
+
|
| 542 |
except Exception as e:
|
| 543 |
+
return f"β Submission failed: {e}", pd.DataFrame(results)
|
| 544 |
+
|
| 545 |
|
| 546 |
+
# ============== UI ==============
|
| 547 |
|
| 548 |
+
with gr.Blocks(title="GAIA Agent - Unit 4") as demo:
|
| 549 |
+
gr.Markdown("""
|
| 550 |
+
# π€ GAIA Agent - Unit 4 Final
|
| 551 |
+
|
| 552 |
+
This agent uses **Groq** (free tier) for:
|
| 553 |
+
- π§ LLM reasoning (Llama 3.1)
|
| 554 |
+
- ποΈ Vision analysis (Llama 3.2 Vision)
|
| 555 |
+
- π€ Audio transcription (Whisper)
|
| 556 |
+
- π Web search (DuckDuckGo)
|
| 557 |
+
- π Python code execution
|
| 558 |
+
|
| 559 |
+
**Instructions:**
|
| 560 |
+
1. Log in with HuggingFace
|
| 561 |
+
2. Click "Run Agent"
|
| 562 |
+
3. Wait ~2-3 minutes
|
| 563 |
+
4. Check your score!
|
| 564 |
+
""")
|
| 565 |
+
|
| 566 |
gr.LoginButton()
|
| 567 |
+
|
| 568 |
+
run_btn = gr.Button("π Run Agent", variant="primary", size="lg")
|
| 569 |
+
|
| 570 |
+
status_box = gr.Textbox(
|
| 571 |
+
label="Status",
|
| 572 |
+
lines=8,
|
| 573 |
+
interactive=False
|
| 574 |
+
)
|
| 575 |
+
|
| 576 |
+
results_table = gr.DataFrame(
|
| 577 |
+
label="Results",
|
| 578 |
+
wrap=True
|
| 579 |
+
)
|
| 580 |
+
|
| 581 |
+
run_btn.click(
|
| 582 |
+
fn=run_and_submit_all,
|
| 583 |
+
outputs=[status_box, results_table]
|
| 584 |
+
)
|
| 585 |
|
| 586 |
if __name__ == "__main__":
|
| 587 |
+
print("\n" + "="*50)
|
| 588 |
+
print("GAIA Agent Starting...")
|
| 589 |
+
print(f"GROQ_API_KEY: {'β
Set' if os.environ.get('GROQ_API_KEY') else 'β Missing'}")
|
| 590 |
+
print("="*50 + "\n")
|
| 591 |
+
|
| 592 |
demo.launch()
|