gabejavitt commited on
Commit
244a643
·
verified ·
1 Parent(s): f69dfab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -49
app.py CHANGED
@@ -1529,61 +1529,32 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
1529
  question_text = item.get("question")
1530
  correct_answer = item.get("answer", "N/A") # Get correct answer from API
1531
 
1532
- # Initialize file variables for the current question
1533
- # Try to download file for EVERY task (not just if file_path exists)
1534
- file_download_url = f"{DEFAULT_API_URL}/files/{task_id}"
1535
- local_file_path = None
1536
 
1537
  try:
1538
- file_response = requests.get(file_download_url, timeout=15)
1539
- if file_response.status_code == 200:
1540
- # Get filename from Content-Disposition header if available
1541
- filename = None
1542
- if 'Content-Disposition' in file_response.headers:
1543
- cd = file_response.headers['Content-Disposition']
1544
- filename_match = re.findall('filename="?([^"]+)"?', cd)
1545
- if filename_match:
1546
- filename = filename_match[0]
1547
 
1548
- # If no filename, use task_id with extension from Content-Type
1549
- if not filename:
1550
- content_type = file_response.headers.get('Content-Type', '')
1551
- ext_map = {
1552
- 'image/png': '.png',
1553
- 'image/jpeg': '.jpg',
1554
- 'image/gif': '.gif',
1555
- 'audio/mpeg': '.mp3',
1556
- 'audio/wav': '.wav',
1557
- 'text/plain': '.txt',
1558
- 'text/csv': '.csv',
1559
- 'application/pdf': '.pdf',
1560
- 'text/x-python': '.py',
1561
- 'application/x-python-code': '.py',
1562
- }
1563
- ext = ext_map.get(content_type, '')
1564
- filename = f"{task_id}{ext}"
1565
-
1566
- # Save to current directory
1567
- local_file_path = filename
1568
-
1569
- with open(local_file_path, 'wb') as f:
1570
- for chunk in file_response.iter_content(chunk_size=8192):
1571
- f.write(chunk)
1572
-
1573
- file_size = os.path.getsize(local_file_path)
1574
- abs_path = os.path.abspath(local_file_path)
1575
-
1576
- print(f"✅ Downloaded: {filename} ({file_size} bytes)")
1577
- print(f" Saved to: {abs_path}")
1578
- elif file_response.status_code == 404:
1579
- print(f"ℹ️ No file found for task {task_id} (404), proceeding without file.")
1580
  else:
1581
- print(f"⚠️ Warning: File download for {task_id} failed with status {file_response.status_code}")
1582
 
1583
  except Exception as e:
1584
- # Handles any other unexpected errors
1585
- print(f"\n❌ An unexpected error occurred: {e}")
1586
-
1587
  try:
1588
  # Pass file_path to agent
1589
  submitted_answer = agent(question_text, local_file_path)
 
1529
  question_text = item.get("question")
1530
  correct_answer = item.get("answer", "N/A") # Get correct answer from API
1531
 
1532
+ # Look for file locally in files/ directory
1533
+ local_file_path = None
1534
+ files_dir = "files"
 
1535
 
1536
  try:
1537
+ # Check if files directory exists
1538
+ if os.path.exists(files_dir):
1539
+ # Look for any file that starts with the task_id
1540
+ matching_files = [f for f in os.listdir(files_dir) if f.startswith(task_id)]
 
 
 
 
 
1541
 
1542
+ if matching_files:
1543
+ # Use the first matching file
1544
+ local_file_path = os.path.join(files_dir, matching_files[0])
1545
+ file_size = os.path.getsize(local_file_path)
1546
+ abs_path = os.path.abspath(local_file_path)
1547
+
1548
+ print(f"✅ Found file: {matching_files[0]} ({file_size} bytes)")
1549
+ print(f" Path: {abs_path}")
1550
+ else:
1551
+ print(f"ℹ️ No file found for task {task_id}, proceeding without file.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1552
  else:
1553
+ print(f"⚠️ Warning: '{files_dir}' directory not found.")
1554
 
1555
  except Exception as e:
1556
+ print(f"❌ Error looking for file: {e}")
1557
+
 
1558
  try:
1559
  # Pass file_path to agent
1560
  submitted_answer = agent(question_text, local_file_path)