gabejavitt commited on
Commit
3cf07b9
Β·
verified Β·
1 Parent(s): 5949398

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -10
app.py CHANGED
@@ -1539,17 +1539,46 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
1539
 
1540
  try:
1541
  file_response = requests.get(file_download_url, timeout=15)
1542
- if file_response.status_code == 200:
1543
- # File exists, save it
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1544
  with open(local_file_path, 'wb') as f:
1545
- f.write(file_response.content)
1546
- print(f"βœ… Downloaded file for {task_id}")
1547
- else:
1548
- # No file for this task, that's fine
1549
- local_file_path = None
1550
- except:
1551
- local_file_path = None
1552
-
 
1553
  try:
1554
  # Pass file_path to agent
1555
  submitted_answer = agent(question_text, local_file_path)
 
1539
 
1540
  try:
1541
  file_response = requests.get(file_download_url, timeout=15)
1542
+ if file_response.status_code == 200:
1543
+ # Get filename from Content-Disposition header if available
1544
+ filename = None
1545
+ if 'Content-Disposition' in file_response.headers:
1546
+ cd = file_response.headers['Content-Disposition']
1547
+ filename_match = re.findall('filename="?([^"]+)"?', cd)
1548
+ if filename_match:
1549
+ filename = filename_match[0]
1550
+
1551
+ # If no filename, use task_id with extension from Content-Type
1552
+ if not filename:
1553
+ content_type = file_response.headers.get('Content-Type', '')
1554
+ ext_map = {
1555
+ 'image/png': '.png',
1556
+ 'image/jpeg': '.jpg',
1557
+ 'image/gif': '.gif',
1558
+ 'audio/mpeg': '.mp3',
1559
+ 'audio/wav': '.wav',
1560
+ 'text/plain': '.txt',
1561
+ 'text/csv': '.csv',
1562
+ 'application/pdf': '.pdf',
1563
+ 'text/x-python': '.py',
1564
+ 'application/x-python-code': '.py',
1565
+ }
1566
+ ext = ext_map.get(content_type, '')
1567
+ filename = f"{task_id}{ext}"
1568
+
1569
+ # Save to current directory
1570
+ local_file_path = filename
1571
+
1572
  with open(local_file_path, 'wb') as f:
1573
+ for chunk in response.iter_content(chunk_size=8192):
1574
+ f.write(chunk)
1575
+
1576
+ file_size = os.path.getsize(local_file_path)
1577
+ abs_path = os.path.abspath(local_file_path)
1578
+
1579
+ print(f"βœ… Downloaded: {filename} ({file_size} bytes)")
1580
+ print(f" Saved to: {abs_path}")
1581
+
1582
  try:
1583
  # Pass file_path to agent
1584
  submitted_answer = agent(question_text, local_file_path)