shrutikaP8497 commited on
Commit
2f1b742
·
verified ·
1 Parent(s): 996d1a7

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +71 -55
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
- def download_task_file(task_id, save_dir="downloads"):
10
- """
11
- Downloads a file associated with a task from the GAIA evaluation API.
12
- """
13
- os.makedirs(save_dir, exist_ok=True)
14
- url = f"{BASE_URL}/files/{task_id}"
15
- response = requests.get(url)
16
-
17
- if response.status_code == 200:
18
- filename = os.path.join(save_dir, task_id)
19
- with open(filename, "wb") as f:
20
- f.write(response.content)
21
- return filename
22
- else:
23
- print(f"Failed to download file for task {task_id}")
24
- return None
25
-
26
-
27
- def format_answer(agent_output):
28
- """
29
- Format the agent's response to meet submission requirements:
30
- - Do NOT include 'FINAL ANSWER'
31
- - Must be a concise string or comma-separated list
32
- """
33
- if isinstance(agent_output, str):
34
- return agent_output.strip()
35
- elif isinstance(agent_output, list):
36
- return ", ".join(map(str, agent_output))
37
- elif isinstance(agent_output, (int, float)):
38
- return str(agent_output)
39
- else:
40
- return str(agent_output)
41
-
42
-
43
- def log_submission(task_id, answer, reasoning_trace=None, save_path="submission_log.jsonl"):
44
- """
45
- Log the task_id and answer for debugging/submission traceability.
46
- """
47
- entry = {
48
- "task_id": task_id,
49
- "submitted_answer": answer,
50
- }
51
- if reasoning_trace:
52
- entry["reasoning_trace"] = reasoning_trace
53
-
54
- with open(save_path, "a") as f:
55
- f.write(json.dumps(entry) + "\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")