naman1102 commited on
Commit
793736b
·
1 Parent(s): 7ea58f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -256,7 +256,16 @@ class BasicAgent:
256
 
257
  # Get content type from response headers first, fallback to URL-based detection
258
  kind = response.headers.get("Content-Type", "")
259
- if not kind: # fallback if header missing
 
 
 
 
 
 
 
 
 
260
  kind = mimetypes.guess_type(state["file_url"])[0] or ""
261
  print(f"Detected file type: {kind}")
262
 
@@ -266,7 +275,7 @@ class BasicAgent:
266
  elif "video" in kind:
267
  print("Processing as video...")
268
  answer = video_label_bytes(data)
269
- elif kind.endswith("spreadsheet") or state["file_url"].endswith((".xlsx", ".csv")):
270
  print("Processing as spreadsheet...")
271
  answer = sheet_answer_bytes(data, state["question"])
272
  elif state["file_url"].endswith(".py"):
@@ -384,11 +393,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
384
 
385
  # Handle file URL with conditional fallback to generic attachment endpoint
386
  raw_url = item.get("file_url") or ""
387
- if not raw_url and "attached" in item["question"].lower():
388
- raw_url = f"/files/{task_id}" # generic attachment endpoint
389
-
390
- # Only construct file_url if we have a raw_url
391
- file_url = f"{api_url}{raw_url}" if raw_url else "" # empty means "no file"
392
 
393
  answer = agent(
394
  question=item.get("question", ""),
@@ -440,6 +447,17 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
440
  except Exception as e:
441
  return f"Submission Failed: {str(e)}", pd.DataFrame(results_log)
442
 
 
 
 
 
 
 
 
 
 
 
 
443
  # --- Build Gradio Interface using Blocks ---
444
  with gr.Blocks() as demo:
445
  gr.Markdown("# Basic Agent Evaluation Runner")
 
256
 
257
  # Get content type from response headers first, fallback to URL-based detection
258
  kind = response.headers.get("Content-Type", "")
259
+ if kind in ("application/octet-stream", ""):
260
+ # rough sniff: look at the first few bytes
261
+ sig = data[:4]
262
+ if sig.startswith(b"\x89PNG"):
263
+ kind = "image/png"
264
+ elif sig.startswith(b"\xFF\xD8"):
265
+ kind = "image/jpeg"
266
+ elif sig[:2] == b"PK": # XLSX = ZIP
267
+ kind = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
268
+ elif not kind: # fallback if header missing
269
  kind = mimetypes.guess_type(state["file_url"])[0] or ""
270
  print(f"Detected file type: {kind}")
271
 
 
275
  elif "video" in kind:
276
  print("Processing as video...")
277
  answer = video_label_bytes(data)
278
+ elif "spreadsheet" in kind or "excel" in kind:
279
  print("Processing as spreadsheet...")
280
  answer = sheet_answer_bytes(data, state["question"])
281
  elif state["file_url"].endswith(".py"):
 
393
 
394
  # Handle file URL with conditional fallback to generic attachment endpoint
395
  raw_url = item.get("file_url") or ""
396
+ if not raw_url: # field missing
397
+ raw_url = attachment_url(task_id, api_url, sess) or ""
398
+ file_url = raw_url # already absolute
 
 
399
 
400
  answer = agent(
401
  question=item.get("question", ""),
 
447
  except Exception as e:
448
  return f"Submission Failed: {str(e)}", pd.DataFrame(results_log)
449
 
450
+ def attachment_url(task_id: str, api_url: str, sess: requests.Session) -> str | None:
451
+ """Probe if a task has an attachment, return URL if it exists."""
452
+ probe = f"{api_url}/files/{task_id}"
453
+ try:
454
+ r = sess.head(probe, timeout=10)
455
+ if r.status_code == 200:
456
+ return probe # attachment exists
457
+ except requests.RequestException:
458
+ pass
459
+ return None # no file
460
+
461
  # --- Build Gradio Interface using Blocks ---
462
  with gr.Blocks() as demo:
463
  gr.Markdown("# Basic Agent Evaluation Runner")