Spaces:
Runtime error
Runtime error
Update utils.py
Browse files
utils.py
CHANGED
@@ -1,55 +1,71 @@
|
|
1 |
-
import requests
|
2 |
-
import os
|
3 |
-
import json
|
4 |
-
|
5 |
-
# API base URL
|
6 |
-
BASE_URL = "https://agents-course-unit4-scoring.hf.space"
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
"""
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
"""
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
if
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
return
|
41 |
-
|
42 |
-
|
43 |
-
def
|
44 |
-
"""
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
|
5 |
+
# API base URL
|
6 |
+
BASE_URL = "https://agents-course-unit4-scoring.hf.space"
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
def get_hf_username() -> str:
|
12 |
+
"""
|
13 |
+
Get the Hugging Face username from the environment (used in Spaces).
|
14 |
+
"""
|
15 |
+
return os.environ.get("SPACE_AUTHOR_NAME", "unknown-user")
|
16 |
+
|
17 |
+
|
18 |
+
def get_code_link() -> str:
|
19 |
+
"""
|
20 |
+
Get the link to the current Space repository.
|
21 |
+
"""
|
22 |
+
repo_id = os.environ.get("SPACE_ID", "unknown-repo")
|
23 |
+
return f"https://huggingface.co/spaces/{repo_id}"
|
24 |
+
|
25 |
+
def download_task_file(task_id, save_dir="downloads"):
|
26 |
+
"""
|
27 |
+
Downloads a file associated with a task from the GAIA evaluation API.
|
28 |
+
"""
|
29 |
+
os.makedirs(save_dir, exist_ok=True)
|
30 |
+
url = f"{BASE_URL}/files/{task_id}"
|
31 |
+
response = requests.get(url)
|
32 |
+
|
33 |
+
if response.status_code == 200:
|
34 |
+
filename = os.path.join(save_dir, task_id)
|
35 |
+
with open(filename, "wb") as f:
|
36 |
+
f.write(response.content)
|
37 |
+
return filename
|
38 |
+
else:
|
39 |
+
print(f"Failed to download file for task {task_id}")
|
40 |
+
return None
|
41 |
+
|
42 |
+
|
43 |
+
def format_answer(agent_output):
|
44 |
+
"""
|
45 |
+
Format the agent's response to meet submission requirements:
|
46 |
+
- Do NOT include 'FINAL ANSWER'
|
47 |
+
- Must be a concise string or comma-separated list
|
48 |
+
"""
|
49 |
+
if isinstance(agent_output, str):
|
50 |
+
return agent_output.strip()
|
51 |
+
elif isinstance(agent_output, list):
|
52 |
+
return ", ".join(map(str, agent_output))
|
53 |
+
elif isinstance(agent_output, (int, float)):
|
54 |
+
return str(agent_output)
|
55 |
+
else:
|
56 |
+
return str(agent_output)
|
57 |
+
|
58 |
+
|
59 |
+
def log_submission(task_id, answer, reasoning_trace=None, save_path="submission_log.jsonl"):
|
60 |
+
"""
|
61 |
+
Log the task_id and answer for debugging/submission traceability.
|
62 |
+
"""
|
63 |
+
entry = {
|
64 |
+
"task_id": task_id,
|
65 |
+
"submitted_answer": answer,
|
66 |
+
}
|
67 |
+
if reasoning_trace:
|
68 |
+
entry["reasoning_trace"] = reasoning_trace
|
69 |
+
|
70 |
+
with open(save_path, "a") as f:
|
71 |
+
f.write(json.dumps(entry) + "\n")
|