mrpe24 commited on
Commit
baa217f
·
1 Parent(s): 55f782d

fixed file download

Browse files
Files changed (2) hide show
  1. agentcourse_unit4/api/agent_eval_api.py +14 -7
  2. app.py +3 -23
agentcourse_unit4/api/agent_eval_api.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from typing import List
2
 
3
  import requests
@@ -20,18 +21,24 @@ class AgentEvalApi:
20
  response.raise_for_status()
21
  return response.json(object_hook=to_question_response)
22
 
23
- def download_file(self, task_id: str) -> bytes:
24
- """Download the file associated with the given task_id"""
25
- response = requests.get(f"{self.files_url}/{task_id}", timeout=30)
26
- response.raise_for_status()
27
- return response.content
 
 
 
 
 
 
28
 
29
  def submit_answers(self, username: str, agent_code: str, answers: List[AnswerData]) -> SubmitAnswersResponse:
30
  """Submit answers from an agent, calculate score, and update leaderboard on HF"""
31
  request_body = {
32
  "username": username.strip(),
33
- "agent_code": agent_code.strip(),
34
- "answers": [{"task_id": a.task_id.strip(), "submitted_answer": a.answer.strip()} for a in answers]
35
  }
36
  response = requests.post(self.submit_url, json=request_body, timeout=60)
37
  response.raise_for_status()
 
1
+ import urllib.request
2
  from typing import List
3
 
4
  import requests
 
21
  response.raise_for_status()
22
  return response.json(object_hook=to_question_response)
23
 
24
+ def download_file(self, task_id: str) -> str:
25
+ """Download the file associated with the given task_id to temp dir and return path to file"""
26
+ file_path = None
27
+
28
+ try:
29
+ result = urllib.request.urlretrieve(f"{self.files_url}/{task_id}")
30
+ file_path = result[0]
31
+ except Exception as e:
32
+ print(f"Error downloading file: {str(e)}")
33
+
34
+ return file_path
35
 
36
  def submit_answers(self, username: str, agent_code: str, answers: List[AnswerData]) -> SubmitAnswersResponse:
37
  """Submit answers from an agent, calculate score, and update leaderboard on HF"""
38
  request_body = {
39
  "username": username.strip(),
40
+ "agent_code": agent_code,
41
+ "answers": [{"task_id": a.task_id, "submitted_answer": a.answer} for a in answers]
42
  }
43
  response = requests.post(self.submit_url, json=request_body, timeout=60)
44
  response.raise_for_status()
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import os
2
- import tempfile
3
  import time
4
 
5
  import gradio as gr
@@ -11,24 +10,6 @@ from agentcourse_unit4.api.answer_data import AnswerData
11
  from agentcourse_unit4.basic_agent import BasicAgent
12
 
13
 
14
- def save_to_file(content: str) -> str:
15
- """
16
- Create temporary file, save content to it and return absolute file path.
17
-
18
- Args:
19
- content: file content to save
20
- Returns:
21
- Path to file
22
- """
23
- temp_file = tempfile.NamedTemporaryFile(delete=False)
24
- filepath = temp_file.name
25
-
26
- with open(filepath, 'w') as f:
27
- f.write(content)
28
-
29
- return filepath
30
-
31
-
32
  def run_and_submit_all(profile: gr.OAuthProfile | None):
33
  """
34
  Fetches all questions, runs the BasicAgent on them, submits all answers,
@@ -83,14 +64,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
83
  task_id = item.task_id
84
  question_text = item.question
85
  file_name = item.file_name
 
86
  if not task_id or question_text is None:
87
  print(f"Skipping item with missing task_id or question: {item}")
88
  continue
 
89
  try:
90
- file_path = None
91
- if len(file_name) > 0:
92
- file_content = api_client.download_file(task_id)
93
- file_path = save_to_file(str(file_content))
94
  submitted_answer = agent.run(question_text, file_path)
95
  answers_payload.append(AnswerData(task_id=task_id, answer=submitted_answer))
96
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
1
  import os
 
2
  import time
3
 
4
  import gradio as gr
 
10
  from agentcourse_unit4.basic_agent import BasicAgent
11
 
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def run_and_submit_all(profile: gr.OAuthProfile | None):
14
  """
15
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
64
  task_id = item.task_id
65
  question_text = item.question
66
  file_name = item.file_name
67
+
68
  if not task_id or question_text is None:
69
  print(f"Skipping item with missing task_id or question: {item}")
70
  continue
71
+
72
  try:
73
+ file_path = api_client.download_file(task_id) if len(file_name) > 0 else None
 
 
 
74
  submitted_answer = agent.run(question_text, file_path)
75
  answers_payload.append(AnswerData(task_id=task_id, answer=submitted_answer))
76
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})