File size: 1,851 Bytes
9f50319 | 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 | import os
import json
import pandas as pd
from huggingface_hub import list_repo_files, hf_hub_download
def load_data(file_path):
"""Load data based on file extension"""
ext = os.path.splitext(file_path)[1].lower()
if ext == '.json':
with open(file_path, 'r', encoding='utf-8') as f:
return json.load(f)
elif ext == '.csv':
return pd.read_csv(file_path)
elif ext == '.jsonl':
data = []
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
data.append(json.loads(line))
return data
elif ext == '.parquet':
return pd.read_parquet(file_path)
else:
raise ValueError(f"Unsupported file extension: {ext}")
def download_and_load_datasets():
repo_id = "Antislab/LLM4PH"
files = list_repo_files(repo_id=repo_id, repo_type="dataset")
downloaded_files = []
for file in files:
if file.startswith("datasets/"):
local_path = os.path.join(".", file)
if not os.path.exists(local_path):
print(f"Downloading {file}...")
hf_hub_download(
repo_id=repo_id,
filename=file,
repo_type="dataset",
local_dir="."
)
else:
print(f"File {file} already exists, skipping download.")
downloaded_files.append(local_path)
# Load all downloaded files
loaded_data = {}
for file_path in downloaded_files:
try:
loaded_data[file_path] = load_data(file_path)
print(f"Successfully loaded {file_path}")
except Exception as e:
print(f"Error loading {file_path}: {str(e)}")
return loaded_data
if __name__ == "__main__":
data = download_and_load_datasets() |