Omnitopia commited on
Commit
94c31f9
·
verified ·
1 Parent(s): e324889

download_file change

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -17,19 +17,29 @@ from my_tools import my_tool_list
17
 
18
  def download_file(task_id, filename, save_dir="attachments"):
19
  os.makedirs(save_dir, exist_ok=True)
20
- url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
21
  save_path = os.path.join(save_dir, filename)
22
  print(f"[DEBUG] Try download: url={url} save_path={save_path}")
23
  try:
24
- resp = requests.get(url, timeout=15)
25
  print(f"[DEBUG] HTTP {resp.status_code} for {url}")
26
  resp.raise_for_status()
27
- with open(save_path, "wb") as f:
 
 
 
 
28
  f.write(resp.content)
29
  print(f"Downloaded attachment for task {task_id} -> {save_path}")
30
  return save_path
 
 
 
 
 
 
31
  except Exception as e:
32
- print(f"Attachment download failed for {task_id}: {e}")
33
  return None
34
 
35
  class BasicAgent:
@@ -142,7 +152,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
142
  results_log = []
143
  answers_payload = []
144
  print(f"Running agent on {len(questions_data)} questions...")
145
- for idx, item in enumerate(questions_data[:1]):
146
  task_id = item.get("task_id")
147
  question_text = item.get("question")
148
  file_list = item.get("files", [])
 
17
 
18
  def download_file(task_id, filename, save_dir="attachments"):
19
  os.makedirs(save_dir, exist_ok=True)
20
+ url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}/{filename}"
21
  save_path = os.path.join(save_dir, filename)
22
  print(f"[DEBUG] Try download: url={url} save_path={save_path}")
23
  try:
24
+ resp = requests.get(url, timeout=30)
25
  print(f"[DEBUG] HTTP {resp.status_code} for {url}")
26
  resp.raise_for_status()
27
+ if resp.headers.get('content-type', '').startswith('text/html'):
28
+ print(f"Warning: Received HTML response, file might not exist: {filename}")
29
+ return None
30
+
31
+ with open(save_path, "wb") as f:
32
  f.write(resp.content)
33
  print(f"Downloaded attachment for task {task_id} -> {save_path}")
34
  return save_path
35
+ except requests.exceptions.HTTPError as e:
36
+ if e.response.status_code == 404:
37
+ print(f"File not found: {filename} for task {task_id}")
38
+ else:
39
+ print(f"HTTP error downloading {filename}: {e}")
40
+ return None
41
  except Exception as e:
42
+ print(f"Attachment download failed for {task_id}/{filename}: {e}")
43
  return None
44
 
45
  class BasicAgent:
 
152
  results_log = []
153
  answers_payload = []
154
  print(f"Running agent on {len(questions_data)} questions...")
155
+ for idx, item in enumerate(questions_data[3:4]):
156
  task_id = item.get("task_id")
157
  question_text = item.get("question")
158
  file_list = item.get("files", [])