Spaces:
Sleeping
Sleeping
File size: 2,277 Bytes
f66d8b7 46fb1b0 f66d8b7 46fb1b0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import os
import requests
import requests
import base64
def download_file(task_id: str):
# Construct the URL
url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
# Send the request to download the file
response = requests.get(url)
if response.status_code == 200:
# Get the content type from the response headers to determine the file extension
content_type = response.headers.get('Content-Type', '')
file_extension = ''
# Map content types to file extensions
if 'pdf' in content_type:
file_extension = '.pdf'
elif 'jpg' in content_type or 'jpeg' in content_type:
file_extension = '.jpg'
elif 'png' in content_type:
file_extension = '.png'
elif 'txt' in content_type:
file_extension = '.txt'
elif 'zip' in content_type:
file_extension = '.zip'
elif 'mp3' in content_type:
file_extension = '.mp3'
# Add more file types as necessary
# If the extension can't be determined, default to .bin
if not file_extension:
file_extension = '.bin'
# Set the path to the Downloads folder (adjust 'YourUsername' to your actual username)
save_path = os.path.join(os.path.expanduser('~'), 'Downloads', f"{task_id}_file{file_extension}")
# Save the file with the appropriate extension
with open(save_path, 'wb') as f:
f.write(response.content)
print(f"File successfully downloaded and saved as {save_path}")
else:
print(f"Failed to download the file. Status code: {response.status_code}")
def download_file_as_base64(task_id: str) -> str:
# Construct the URL
url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
# Send the request to download the file
response = requests.get(url)
if response.status_code == 200:
# Encode the content to Base64
encoded_bytes = base64.b64encode(response.content)
encoded_str = encoded_bytes.decode('utf-8') # Convert bytes to string
return encoded_str
else:
raise Exception(f"Failed to download the file. Status code: {response.status_code}")
|