|
import os |
|
import requests |
|
from huggingface_hub import HfApi |
|
from dotenv import load_dotenv |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
HF_TOKEN = os.getenv("HF_TOKEN") |
|
|
|
|
|
if not HF_TOKEN: |
|
raise ValueError("HF_TOKEN not found in environment variables. Please set it in a .env file.") |
|
|
|
|
|
api = HfApi() |
|
|
|
|
|
file_url = "https://physionet.org/static/published-projects/ptb-xl/ptb-xl-a-large-publicly-available-electrocardiography-dataset-1.0.3.zip" |
|
filename = "ptb-xl-a-large-publicly-available-electrocardiography-dataset-1.0.3.zip" |
|
|
|
|
|
repo_id = "Nayefleb/ECG" |
|
repo_type = "space" |
|
|
|
def download_and_upload_file(): |
|
try: |
|
|
|
temp_path = f"/tmp/{filename}" |
|
|
|
|
|
print(f"Downloading {filename} from {file_url}...") |
|
response = requests.get(file_url, stream=True) |
|
response.raise_for_status() |
|
with open(temp_path, "wb") as f: |
|
for chunk in response.iter_content(chunk_size=8192): |
|
f.write(chunk) |
|
print(f"Downloaded {filename} to temporary storage.") |
|
|
|
|
|
print(f"Uploading {filename} to {repo_id}...") |
|
api.upload_file( |
|
path_or_fileobj=temp_path, |
|
path_in_repo=filename, |
|
repo_id=repo_id, |
|
repo_type=repo_type, |
|
token=HF_TOKEN |
|
) |
|
print(f"Successfully uploaded {filename} to {repo_id}") |
|
|
|
|
|
os.remove(temp_path) |
|
print("Cleaned up temporary file.") |
|
|
|
except Exception as e: |
|
print(f"Error: {str(e)}") |
|
|
|
if __name__ == "__main__": |
|
download_and_upload_file() |