Download evaluate_code/load_datasets.py from Antislab/LLM4PH: direct link, hf CLI and curl.
- Browser
- Download file 1.85 kB
-
https://huggingface.co/datasets/Antislab/LLM4PH/resolve/main/evaluate_code/load_datasets.py
- Command line
-
hf download hf://datasets/Antislab/LLM4PH/evaluate_code/load_datasets.py
-
curl -L -o load_datasets.py https://huggingface.co/datasets/Antislab/LLM4PH/resolve/main/evaluate_code/load_datasets.py
1.85 kB
| 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() |