Spaces:
Runtime error
Runtime error
fixing
Browse files
app.py
CHANGED
@@ -1,307 +1,473 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
|
|
|
|
4 |
import json
|
5 |
import re
|
6 |
-
import
|
7 |
-
import
|
8 |
-
from
|
9 |
-
|
10 |
-
from typing import List, Dict, Optional, Tuple
|
11 |
-
from dotenv import load_dotenv
|
12 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
13 |
-
import torch
|
14 |
-
import time
|
15 |
-
import gc
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
MODEL_NAME = "microsoft/Phi-3-mini-4k-instruct"
|
21 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
22 |
|
23 |
# --- Constants ---
|
24 |
-
|
25 |
-
MAX_TOKENS = 256
|
26 |
-
TIMEOUT_PER_QUESTION = 45
|
27 |
-
MAX_RESULT_LENGTH = 500
|
28 |
-
MAX_ATTEMPTS = 2
|
29 |
-
|
30 |
-
# --- Model Initialization ---
|
31 |
-
print("Initializing model with fixed cache configuration...")
|
32 |
-
start_time = time.time()
|
33 |
-
|
34 |
-
model = AutoModelForCausalLM.from_pretrained(
|
35 |
-
MODEL_NAME,
|
36 |
-
trust_remote_code=True,
|
37 |
-
torch_dtype=torch.float32,
|
38 |
-
device_map="auto",
|
39 |
-
low_cpu_mem_usage=True
|
40 |
-
)
|
41 |
-
|
42 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
43 |
-
MODEL_NAME,
|
44 |
-
use_fast=True,
|
45 |
-
trust_remote_code=True
|
46 |
-
)
|
47 |
-
|
48 |
-
if tokenizer.pad_token is None:
|
49 |
-
tokenizer.pad_token = tokenizer.eos_token
|
50 |
-
|
51 |
-
print(f"Model loaded in {time.time() - start_time:.2f} seconds")
|
52 |
|
53 |
-
|
54 |
-
def
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
def calculator(expression: str) -> str:
|
82 |
-
try:
|
83 |
-
expression = re.sub(r'[^\d+\-*/().^%,\s]', '', expression)
|
84 |
-
if not expression:
|
85 |
-
return "Invalid empty expression"
|
86 |
-
return str(numexpr.evaluate(expression))
|
87 |
-
except Exception as e:
|
88 |
-
return f"Calculation error: {str(e)}"
|
89 |
-
|
90 |
-
def read_webpage(url: str) -> str:
|
91 |
-
try:
|
92 |
-
if not re.match(r'^https?://', url):
|
93 |
-
return "Invalid URL format"
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
-
|
117 |
-
class GAIA_Agent:
|
118 |
-
def __init__(self):
|
119 |
-
self.tools = TOOLS
|
120 |
-
self.system_prompt = """You are an advanced problem solver. Follow these steps:
|
121 |
-
1. Analyze the question
|
122 |
-
2. Select the best tool
|
123 |
-
3. Execute with proper arguments
|
124 |
-
4. Interpret results
|
125 |
-
5. Provide final answer
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
|
|
131 |
|
132 |
-
|
133 |
-
{"tool": "tool_name", "args": {"arg": value}}```
|
134 |
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
-
|
138 |
-
start_time = time.time()
|
139 |
-
history = [f"Question: {question}"]
|
140 |
-
|
141 |
-
try:
|
142 |
-
for step in range(MAX_STEPS):
|
143 |
-
if time.time() - start_time > TIMEOUT_PER_QUESTION:
|
144 |
-
return "Timeout: Processing took too long"
|
145 |
-
|
146 |
-
prompt = self._build_prompt(history)
|
147 |
-
response = self._call_model(prompt)
|
148 |
-
|
149 |
-
if "Final Answer:" in response:
|
150 |
-
return response.split("Final Answer:")[-1].strip()[:500]
|
151 |
-
|
152 |
-
tool_call = self._parse_tool_call(response)
|
153 |
-
if tool_call:
|
154 |
-
tool_name, args = tool_call
|
155 |
-
observation = self._use_tool(tool_name, args)
|
156 |
-
history.append(f"Tool: {tool_name}")
|
157 |
-
history.append(f"Result: {observation[:300]}...")
|
158 |
-
else:
|
159 |
-
history.append(f"Thought: {response}")
|
160 |
-
|
161 |
-
gc.collect()
|
162 |
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
|
167 |
-
|
168 |
-
return f"<|system|>\n{self.system_prompt}<|end|>\n<|user|>\n" + "\n".join(history) + "<|end|>\n<|assistant|>"
|
169 |
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
prompt,
|
175 |
-
return_tensors="pt",
|
176 |
-
truncation=True,
|
177 |
-
max_length=3072,
|
178 |
-
padding=False
|
179 |
-
)
|
180 |
-
|
181 |
-
outputs = model.generate(
|
182 |
-
inputs.input_ids,
|
183 |
-
max_new_tokens=MAX_TOKENS,
|
184 |
-
temperature=0.3,
|
185 |
-
top_p=0.9,
|
186 |
-
do_sample=True,
|
187 |
-
pad_token_id=tokenizer.pad_token_id,
|
188 |
-
attention_mask=inputs.attention_mask
|
189 |
-
)
|
190 |
-
|
191 |
-
return tokenizer.decode(outputs[0], skip_special_tokens=True).split("<|assistant|>")[-1].strip()
|
192 |
-
except Exception as e:
|
193 |
-
if attempt < MAX_ATTEMPTS - 1:
|
194 |
-
time.sleep(0.5)
|
195 |
-
continue
|
196 |
-
return f"Model error: {str(e)}"
|
197 |
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
return None
|
207 |
-
if "tool" not in tool_call or "args" not in tool_call:
|
208 |
-
return None
|
209 |
-
if not isinstance(tool_call["args"], dict):
|
210 |
-
return None
|
211 |
-
|
212 |
-
return tool_call["tool"], tool_call["args"]
|
213 |
-
except:
|
214 |
-
return None
|
215 |
|
216 |
-
|
217 |
-
|
218 |
-
return f"Unknown tool: {tool_name}"
|
219 |
|
|
|
220 |
try:
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
|
228 |
-
return str(self.tools[tool_name](**args))[:MAX_RESULT_LENGTH]
|
229 |
except Exception as e:
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
def run_evaluation(profile: gr.OAuthProfile | None):
|
234 |
-
if not profile:
|
235 |
-
return "Please login first", None
|
236 |
-
|
237 |
-
agent = GAIA_Agent()
|
238 |
-
questions_url = f"{DEFAULT_API_URL}/questions"
|
239 |
-
submit_url = f"{DEFAULT_API_URL}/submit"
|
240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
try:
|
242 |
-
response = requests.get(questions_url, timeout=
|
243 |
response.raise_for_status()
|
244 |
questions_data = response.json()
|
245 |
if not questions_data:
|
246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
247 |
except Exception as e:
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
|
|
|
|
|
|
252 |
|
253 |
-
for i, item in enumerate(questions_data):
|
254 |
task_id = item.get("task_id")
|
255 |
-
|
256 |
|
257 |
-
if not task_id or
|
|
|
258 |
continue
|
259 |
|
260 |
-
print(f"Processing question {i
|
261 |
-
answer = agent(question)
|
262 |
|
263 |
-
|
264 |
-
|
265 |
-
"
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
}
|
275 |
|
|
|
|
|
|
|
276 |
try:
|
277 |
-
response = requests.post(submit_url, json=
|
278 |
response.raise_for_status()
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
284 |
except Exception as e:
|
285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
|
287 |
-
# --- Gradio Interface ---
|
288 |
-
with gr.Blocks(title="Fixed GAIA Agent", theme=gr.themes.Soft()) as demo:
|
289 |
-
gr.Markdown("# ๐ GAIA Agent Evaluation")
|
290 |
-
|
291 |
with gr.Row():
|
292 |
gr.LoginButton()
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
|
|
|
|
|
|
|
|
|
|
297 |
|
298 |
-
|
299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
300 |
outputs=[status_output, results_table]
|
301 |
)
|
302 |
|
303 |
if __name__ == "__main__":
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
+
import inspect
|
5 |
+
import pandas as pd
|
6 |
import json
|
7 |
import re
|
8 |
+
from typing import Dict, List, Any, Optional
|
9 |
+
import urllib.parse
|
10 |
+
from datetime import datetime
|
11 |
+
import math
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
# Transformers and torch imports
|
14 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
15 |
+
import torch
|
|
|
|
|
16 |
|
17 |
# --- Constants ---
|
18 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
class EnhancedGAIAAgent:
|
21 |
+
def __init__(self):
|
22 |
+
print("Initializing Enhanced GAIA Agent with Mistral-7B...")
|
23 |
+
|
24 |
+
# Initialize Mistral model
|
25 |
+
try:
|
26 |
+
print("Loading Mistral-7B-Instruct model...")
|
27 |
+
self.tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3")
|
28 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
29 |
+
"mistralai/Mistral-7B-Instruct-v0.3",
|
30 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
31 |
+
device_map="auto" if torch.cuda.is_available() else None
|
32 |
+
)
|
33 |
|
34 |
+
# Create pipeline for easier use
|
35 |
+
self.pipe = pipeline(
|
36 |
+
"text-generation",
|
37 |
+
model=self.model,
|
38 |
+
tokenizer=self.tokenizer,
|
39 |
+
max_new_tokens=512,
|
40 |
+
temperature=0.7,
|
41 |
+
do_sample=True,
|
42 |
+
pad_token_id=self.tokenizer.eos_token_id
|
43 |
+
)
|
44 |
+
print("โ
Mistral model loaded successfully!")
|
45 |
+
|
46 |
+
except Exception as e:
|
47 |
+
print(f"โ Error loading Mistral model: {e}")
|
48 |
+
print("Falling back to basic responses...")
|
49 |
+
self.pipe = None
|
50 |
+
|
51 |
+
# Tool functions for GAIA tasks
|
52 |
+
self.tools = {
|
53 |
+
"calculate": self._calculate,
|
54 |
+
"search_web": self._search_web,
|
55 |
+
"parse_data": self._parse_data,
|
56 |
+
"analyze_text": self._analyze_text,
|
57 |
+
"solve_math": self._solve_math
|
58 |
+
}
|
59 |
|
60 |
+
def _calculate(self, expression: str) -> str:
|
61 |
+
"""Safe calculator for mathematical expressions"""
|
62 |
+
try:
|
63 |
+
# Clean and validate expression
|
64 |
+
expression = re.sub(r'[^0-9+\-*/().\s]', '', expression)
|
65 |
+
result = eval(expression)
|
66 |
+
return str(result)
|
67 |
+
except Exception as e:
|
68 |
+
return f"Calculation error: {e}"
|
69 |
+
|
70 |
+
def _search_web(self, query: str) -> str:
|
71 |
+
"""Simulate web search (placeholder - you'd integrate real search API)"""
|
72 |
+
# This is a placeholder - integrate with actual search API
|
73 |
+
return f"Search results for '{query}': [This would contain real search results]"
|
74 |
+
|
75 |
+
def _parse_data(self, data: str) -> str:
|
76 |
+
"""Parse and analyze structured data"""
|
77 |
+
try:
|
78 |
+
# Try to parse as JSON
|
79 |
+
if data.strip().startswith('{') or data.strip().startswith('['):
|
80 |
+
parsed = json.loads(data)
|
81 |
+
return f"Parsed data structure with {len(parsed) if isinstance(parsed, (list, dict)) else 1} elements"
|
82 |
+
else:
|
83 |
+
# Basic text analysis
|
84 |
+
lines = data.split('\n')
|
85 |
+
return f"Text data with {len(lines)} lines, {len(data.split())} words"
|
86 |
+
except Exception as e:
|
87 |
+
return f"Data parsing error: {e}"
|
88 |
+
|
89 |
+
def _analyze_text(self, text: str) -> str:
|
90 |
+
"""Analyze text content"""
|
91 |
+
words = text.split()
|
92 |
+
sentences = text.split('.')
|
93 |
+
return f"Text analysis: {len(words)} words, {len(sentences)} sentences"
|
94 |
+
|
95 |
+
def _solve_math(self, problem: str) -> str:
|
96 |
+
"""Enhanced math problem solver"""
|
97 |
+
try:
|
98 |
+
# Extract numbers and operations
|
99 |
+
numbers = re.findall(r'-?\d+\.?\d*', problem)
|
100 |
|
101 |
+
# Handle common math patterns
|
102 |
+
if "percent" in problem.lower() or "%" in problem:
|
103 |
+
if len(numbers) >= 2:
|
104 |
+
base = float(numbers[0])
|
105 |
+
percent = float(numbers[1])
|
106 |
+
result = base * (percent / 100)
|
107 |
+
return str(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
+
if "average" in problem.lower() or "mean" in problem.lower():
|
110 |
+
if numbers:
|
111 |
+
nums = [float(n) for n in numbers]
|
112 |
+
return str(sum(nums) / len(nums))
|
113 |
+
|
114 |
+
# Default calculation
|
115 |
+
return self._calculate(" ".join(numbers))
|
116 |
+
|
117 |
+
except Exception as e:
|
118 |
+
return f"Math solving error: {e}"
|
119 |
+
|
120 |
+
def _generate_response(self, prompt: str) -> str:
|
121 |
+
"""Generate response using Mistral model"""
|
122 |
+
if not self.pipe:
|
123 |
+
return "Model not available - using fallback response."
|
124 |
|
125 |
+
try:
|
126 |
+
messages = [
|
127 |
+
{"role": "user", "content": prompt}
|
128 |
+
]
|
129 |
|
130 |
+
response = self.pipe(messages, max_new_tokens=512, temperature=0.7)
|
131 |
+
|
132 |
+
# Extract the generated text
|
133 |
+
if response and len(response) > 0:
|
134 |
+
generated_text = response[0]['generated_text']
|
135 |
+
# Get only the assistant's response (after the user message)
|
136 |
+
if isinstance(generated_text, list):
|
137 |
+
# Find the assistant's response
|
138 |
+
for msg in generated_text:
|
139 |
+
if msg.get('role') == 'assistant':
|
140 |
+
return msg.get('content', '')
|
141 |
+
elif isinstance(generated_text, str):
|
142 |
+
return generated_text
|
143 |
+
else:
|
144 |
+
return str(generated_text)
|
145 |
+
|
146 |
+
return "No response generated."
|
147 |
+
|
148 |
+
except Exception as e:
|
149 |
+
print(f"Error generating response: {e}")
|
150 |
+
return f"Error in response generation: {e}"
|
151 |
+
|
152 |
+
def _detect_task_type(self, question: str) -> str:
|
153 |
+
"""Detect the type of task to apply appropriate strategy"""
|
154 |
+
question_lower = question.lower()
|
155 |
+
|
156 |
+
if any(word in question_lower for word in ["calculate", "compute", "math", "+", "-", "*", "/", "="]):
|
157 |
+
return "calculation"
|
158 |
+
elif any(word in question_lower for word in ["search", "find", "lookup", "google"]):
|
159 |
+
return "search"
|
160 |
+
elif any(word in question_lower for word in ["data", "csv", "json", "table", "parse"]):
|
161 |
+
return "data_analysis"
|
162 |
+
elif any(word in question_lower for word in ["percent", "%", "average", "mean", "sum"]):
|
163 |
+
return "math_word_problem"
|
164 |
+
else:
|
165 |
+
return "general_reasoning"
|
166 |
+
|
167 |
+
def __call__(self, question: str) -> str:
|
168 |
+
print(f"Agent processing question (first 100 chars): {question[:100]}...")
|
169 |
+
|
170 |
+
# Detect task type
|
171 |
+
task_type = self._detect_task_type(question)
|
172 |
+
print(f"Detected task type: {task_type}")
|
173 |
+
|
174 |
+
# Build enhanced prompt based on task type
|
175 |
+
if task_type == "calculation":
|
176 |
+
enhanced_prompt = f"""
|
177 |
+
You are a precise mathematical assistant. Solve this step-by-step:
|
178 |
|
179 |
+
Question: {question}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
+
Provide a clear, accurate answer. If calculation is needed, show your work.
|
182 |
+
Answer:"""
|
183 |
+
|
184 |
+
elif task_type == "math_word_problem":
|
185 |
+
enhanced_prompt = f"""
|
186 |
+
You are solving a math word problem. Break it down step by step:
|
187 |
|
188 |
+
Question: {question}
|
|
|
189 |
|
190 |
+
Steps:
|
191 |
+
1. Identify what is being asked
|
192 |
+
2. Extract the relevant numbers
|
193 |
+
3. Determine the operation needed
|
194 |
+
4. Calculate the result
|
195 |
+
5. Provide the final answer
|
196 |
|
197 |
+
Answer:"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
+
elif task_type == "data_analysis":
|
200 |
+
enhanced_prompt = f"""
|
201 |
+
You are analyzing data. Approach this systematically:
|
202 |
|
203 |
+
Question: {question}
|
|
|
204 |
|
205 |
+
Consider:
|
206 |
+
- What type of data is involved?
|
207 |
+
- What analysis is needed?
|
208 |
+
- What tools or methods should be used?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
|
210 |
+
Provide a clear, structured answer.
|
211 |
+
Answer:"""
|
212 |
+
|
213 |
+
else:
|
214 |
+
enhanced_prompt = f"""
|
215 |
+
You are a helpful assistant that provides accurate, well-reasoned answers.
|
216 |
+
|
217 |
+
Question: {question}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
|
219 |
+
Think through this step-by-step and provide a clear, comprehensive answer.
|
220 |
+
Answer:"""
|
|
|
221 |
|
222 |
+
# Generate response using the model
|
223 |
try:
|
224 |
+
response = self._generate_response(enhanced_prompt)
|
225 |
+
|
226 |
+
# Post-process response for specific task types
|
227 |
+
if task_type in ["calculation", "math_word_problem"]:
|
228 |
+
# Try to extract and verify any calculations
|
229 |
+
numbers_in_response = re.findall(r'-?\d+\.?\d*', response)
|
230 |
+
if numbers_in_response:
|
231 |
+
# Attempt to verify calculation if simple enough
|
232 |
+
pass
|
233 |
+
|
234 |
+
print(f"Agent returning response (first 100 chars): {response[:100]}...")
|
235 |
+
return response.strip()
|
236 |
|
|
|
237 |
except Exception as e:
|
238 |
+
print(f"Error in agent processing: {e}")
|
239 |
+
fallback_response = self._handle_fallback(question, task_type)
|
240 |
+
return fallback_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
|
242 |
+
def _handle_fallback(self, question: str, task_type: str) -> str:
|
243 |
+
"""Provide fallback responses when the main model fails"""
|
244 |
+
if task_type == "calculation":
|
245 |
+
# Try to extract and calculate simple expressions
|
246 |
+
try:
|
247 |
+
numbers = re.findall(r'-?\d+\.?\d*', question)
|
248 |
+
if len(numbers) >= 2:
|
249 |
+
if "+" in question:
|
250 |
+
result = sum(float(n) for n in numbers)
|
251 |
+
return f"The sum is {result}"
|
252 |
+
elif "*" in question or "multiply" in question.lower():
|
253 |
+
result = 1
|
254 |
+
for n in numbers:
|
255 |
+
result *= float(n)
|
256 |
+
return f"The product is {result}"
|
257 |
+
except:
|
258 |
+
pass
|
259 |
+
|
260 |
+
return f"I understand you're asking about: {question}. This appears to be a {task_type} task. Let me provide my best analysis based on the available information."
|
261 |
+
|
262 |
+
|
263 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
264 |
+
"""
|
265 |
+
Fetches all questions, runs the EnhancedGAIAAgent on them, submits all answers,
|
266 |
+
and displays the results.
|
267 |
+
"""
|
268 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
269 |
+
space_id = os.getenv("SPACE_ID")
|
270 |
+
|
271 |
+
if profile:
|
272 |
+
username = f"{profile.username}"
|
273 |
+
print(f"User logged in: {username}")
|
274 |
+
else:
|
275 |
+
print("User not logged in.")
|
276 |
+
return "Please Login to Hugging Face with the button.", None
|
277 |
+
|
278 |
+
api_url = DEFAULT_API_URL
|
279 |
+
questions_url = f"{api_url}/questions"
|
280 |
+
submit_url = f"{api_url}/submit"
|
281 |
+
|
282 |
+
# 1. Instantiate Enhanced Agent
|
283 |
+
try:
|
284 |
+
print("Initializing Enhanced GAIA Agent...")
|
285 |
+
agent = EnhancedGAIAAgent()
|
286 |
+
print("โ
Agent initialized successfully!")
|
287 |
+
except Exception as e:
|
288 |
+
print(f"โ Error instantiating agent: {e}")
|
289 |
+
return f"Error initializing agent: {e}", None
|
290 |
+
|
291 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
292 |
+
print(f"Agent code URL: {agent_code}")
|
293 |
+
|
294 |
+
# 2. Fetch Questions
|
295 |
+
print(f"Fetching questions from: {questions_url}")
|
296 |
try:
|
297 |
+
response = requests.get(questions_url, timeout=15)
|
298 |
response.raise_for_status()
|
299 |
questions_data = response.json()
|
300 |
if not questions_data:
|
301 |
+
print("Fetched questions list is empty.")
|
302 |
+
return "Fetched questions list is empty or invalid format.", None
|
303 |
+
print(f"โ
Fetched {len(questions_data)} questions.")
|
304 |
+
except requests.exceptions.RequestException as e:
|
305 |
+
print(f"โ Error fetching questions: {e}")
|
306 |
+
return f"Error fetching questions: {e}", None
|
307 |
+
except requests.exceptions.JSONDecodeError as e:
|
308 |
+
print(f"โ Error decoding JSON response from questions endpoint: {e}")
|
309 |
+
return f"Error decoding server response for questions: {e}", None
|
310 |
except Exception as e:
|
311 |
+
print(f"โ An unexpected error occurred fetching questions: {e}")
|
312 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
313 |
+
|
314 |
+
# 3. Run Enhanced Agent
|
315 |
+
results_log = []
|
316 |
+
answers_payload = []
|
317 |
+
print(f"๐ Running enhanced agent on {len(questions_data)} questions...")
|
318 |
|
319 |
+
for i, item in enumerate(questions_data, 1):
|
320 |
task_id = item.get("task_id")
|
321 |
+
question_text = item.get("question")
|
322 |
|
323 |
+
if not task_id or question_text is None:
|
324 |
+
print(f"โ ๏ธ Skipping item with missing task_id or question: {item}")
|
325 |
continue
|
326 |
|
327 |
+
print(f"๐ Processing question {i}/{len(questions_data)} (ID: {task_id})")
|
|
|
328 |
|
329 |
+
try:
|
330 |
+
submitted_answer = agent(question_text)
|
331 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
332 |
+
results_log.append({
|
333 |
+
"Task ID": task_id,
|
334 |
+
"Question": question_text[:200] + "..." if len(question_text) > 200 else question_text,
|
335 |
+
"Submitted Answer": submitted_answer[:300] + "..." if len(submitted_answer) > 300 else submitted_answer
|
336 |
+
})
|
337 |
+
print(f"โ
Completed question {i}")
|
338 |
+
|
339 |
+
except Exception as e:
|
340 |
+
print(f"โ Error running agent on task {task_id}: {e}")
|
341 |
+
error_response = f"AGENT ERROR: {e}"
|
342 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": error_response})
|
343 |
+
results_log.append({
|
344 |
+
"Task ID": task_id,
|
345 |
+
"Question": question_text[:200] + "..." if len(question_text) > 200 else question_text,
|
346 |
+
"Submitted Answer": error_response
|
347 |
+
})
|
348 |
+
|
349 |
+
if not answers_payload:
|
350 |
+
print("โ Agent did not produce any answers to submit.")
|
351 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
352 |
+
|
353 |
+
# 4. Prepare Submission
|
354 |
+
submission_data = {
|
355 |
+
"username": username.strip(),
|
356 |
+
"agent_code": agent_code,
|
357 |
+
"answers": answers_payload
|
358 |
}
|
359 |
|
360 |
+
print(f"๐ค Submitting {len(answers_payload)} answers for user '{username}'...")
|
361 |
+
|
362 |
+
# 5. Submit
|
363 |
try:
|
364 |
+
response = requests.post(submit_url, json=submission_data, timeout=120) # Increased timeout
|
365 |
response.raise_for_status()
|
366 |
+
result_data = response.json()
|
367 |
+
|
368 |
+
final_status = (
|
369 |
+
f"๐ Submission Successful!\n"
|
370 |
+
f"User: {result_data.get('username')}\n"
|
371 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
372 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
373 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
374 |
+
)
|
375 |
+
|
376 |
+
print("โ
Submission successful!")
|
377 |
+
results_df = pd.DataFrame(results_log)
|
378 |
+
return final_status, results_df
|
379 |
+
|
380 |
+
except requests.exceptions.HTTPError as e:
|
381 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
382 |
+
try:
|
383 |
+
error_json = e.response.json()
|
384 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
385 |
+
except requests.exceptions.JSONDecodeError:
|
386 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
387 |
+
status_message = f"โ Submission Failed: {error_detail}"
|
388 |
+
print(status_message)
|
389 |
+
results_df = pd.DataFrame(results_log)
|
390 |
+
return status_message, results_df
|
391 |
+
|
392 |
except Exception as e:
|
393 |
+
status_message = f"โ An unexpected error occurred during submission: {e}"
|
394 |
+
print(status_message)
|
395 |
+
results_df = pd.DataFrame(results_log)
|
396 |
+
return status_message, results_df
|
397 |
+
|
398 |
+
|
399 |
+
# --- Build Gradio Interface using Blocks ---
|
400 |
+
with gr.Blocks(title="Enhanced GAIA Agent") as demo:
|
401 |
+
gr.Markdown("# ๐ Enhanced GAIA Agent with Mistral-7B")
|
402 |
+
gr.Markdown(
|
403 |
+
"""
|
404 |
+
**Enhanced Features:**
|
405 |
+
- ๐ง **Mistral-7B-Instruct** for advanced reasoning
|
406 |
+
- ๐ง **Tool Integration** for calculations and data processing
|
407 |
+
- ๐ **Task Type Detection** for optimized responses
|
408 |
+
- ๐ฏ **GAIA-Optimized** prompting strategies
|
409 |
+
|
410 |
+
**Instructions:**
|
411 |
+
1. Clone this space and ensure you have access to Mistral-7B-Instruct
|
412 |
+
2. Log in to your Hugging Face account using the button below
|
413 |
+
3. Click 'Run Enhanced Evaluation' to process all questions with the enhanced agent
|
414 |
+
|
415 |
+
**Note:** The enhanced agent uses Mistral-7B which requires significant computational resources.
|
416 |
+
Processing may take several minutes depending on the number of questions.
|
417 |
+
"""
|
418 |
+
)
|
419 |
|
|
|
|
|
|
|
|
|
420 |
with gr.Row():
|
421 |
gr.LoginButton()
|
422 |
+
|
423 |
+
with gr.Row():
|
424 |
+
run_button = gr.Button("๐ Run Enhanced Evaluation & Submit All Answers", variant="primary")
|
425 |
+
|
426 |
+
status_output = gr.Textbox(
|
427 |
+
label="๐ Run Status / Submission Result",
|
428 |
+
lines=8,
|
429 |
+
interactive=False
|
430 |
+
)
|
431 |
|
432 |
+
results_table = gr.DataFrame(
|
433 |
+
label="๐ Questions and Agent Answers",
|
434 |
+
wrap=True,
|
435 |
+
height=400
|
436 |
+
)
|
437 |
+
|
438 |
+
run_button.click(
|
439 |
+
fn=run_and_submit_all,
|
440 |
outputs=[status_output, results_table]
|
441 |
)
|
442 |
|
443 |
if __name__ == "__main__":
|
444 |
+
print("\n" + "="*50)
|
445 |
+
print("๐ ENHANCED GAIA AGENT STARTING")
|
446 |
+
print("="*50)
|
447 |
+
|
448 |
+
# Environment check
|
449 |
+
space_host = os.getenv("SPACE_HOST")
|
450 |
+
space_id = os.getenv("SPACE_ID")
|
451 |
+
|
452 |
+
if space_host:
|
453 |
+
print(f"โ
SPACE_HOST: {space_host}")
|
454 |
+
print(f"๐ Runtime URL: https://{space_host}.hf.space")
|
455 |
+
else:
|
456 |
+
print("โน๏ธ Running locally - SPACE_HOST not found")
|
457 |
+
|
458 |
+
if space_id:
|
459 |
+
print(f"โ
SPACE_ID: {space_id}")
|
460 |
+
print(f"๐ Repo URL: https://huggingface.co/spaces/{space_id}")
|
461 |
+
else:
|
462 |
+
print("โน๏ธ SPACE_ID not found")
|
463 |
+
|
464 |
+
# GPU/CPU check
|
465 |
+
if torch.cuda.is_available():
|
466 |
+
print(f"๐ฎ GPU Available: {torch.cuda.get_device_name()}")
|
467 |
+
print(f"๐พ GPU Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")
|
468 |
+
else:
|
469 |
+
print("๐ป Running on CPU (GPU not available)")
|
470 |
+
|
471 |
+
print("="*50)
|
472 |
+
print("๐ Launching Enhanced GAIA Agent Interface...")
|
473 |
+
demo.launch(debug=True, share=False)
|