File size: 4,722 Bytes
58cb8a8 |
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
#!/usr/bin/env python3
"""
Upload the wine text dataset to HuggingFace Hub.
Prerequisites:
1. Install required packages: pip install datasets huggingface_hub pyarrow
2. Login to HF Hub: huggingface-cli login
3. Replace YOUR_USERNAME with your actual HF username
Usage:
python upload_to_hf.py --username YOUR_USERNAME [--private]
"""
import argparse
import pandas as pd
from datasets import Dataset, Features, Value
from huggingface_hub import HfApi
import os
def create_dataset_features():
"""Define the dataset schema."""
return Features({
"id": Value("string"),
"name": Value("string"),
"description": Value("string"),
"price": Value("float32"),
"category": Value("string"),
"region": Value("string"),
"image_id": Value("string"),
})
def load_and_prepare_dataset(parquet_file="wine_text_126k.parquet"):
"""Load the parquet file and create a HuggingFace Dataset."""
print(f"Loading dataset from {parquet_file}...")
df = pd.read_parquet(parquet_file)
# Verify data quality
print(f"Dataset shape: {df.shape}")
print(f"Columns: {list(df.columns)}")
# Check for any missing values
missing = df.isnull().sum()
if missing.any():
print("Warning: Missing values found:")
print(missing[missing > 0])
else:
print("โ
No missing values found")
# Create HuggingFace Dataset with proper features
features = create_dataset_features()
dataset = Dataset.from_pandas(df, features=features, preserve_index=False)
print(f"โ
Created HuggingFace Dataset with {len(dataset):,} records")
return dataset
def upload_dataset(dataset, username, repo_name="wine-text-126k", private=False):
"""Upload the dataset to HuggingFace Hub."""
full_repo_name = f"{username}/{repo_name}"
print(f"\n๐ Uploading to HuggingFace Hub: {full_repo_name}")
print(f"Privacy: {'Private' if private else 'Public'}")
try:
# Create the repository (this will fail gracefully if it already exists)
api = HfApi()
try:
api.create_repo(
repo_id=full_repo_name,
repo_type="dataset",
private=private
)
print(f"โ
Created repository: {full_repo_name}")
except Exception as e:
if "already exists" in str(e).lower():
print(f"โน๏ธ Repository already exists: {full_repo_name}")
else:
print(f"โ ๏ธ Repository creation warning: {e}")
# Upload the dataset
print("๐ค Uploading dataset files...")
dataset.push_to_hub(
full_repo_name,
private=private,
commit_message="Initial upload of wine text dataset"
)
print(f"\n๐ Upload completed successfully!")
print(f"๐ Dataset URL: https://huggingface.co/datasets/{full_repo_name}")
print(f"\n๐ Usage:")
print(f'from datasets import load_dataset')
print(f'dataset = load_dataset("{full_repo_name}")')
return True
except Exception as e:
print(f"โ Upload failed: {e}")
print("\nTroubleshooting:")
print("1. Make sure you're logged in: huggingface-cli login")
print("2. Check your internet connection")
print("3. Verify the username is correct")
print("4. Ensure you have write permissions")
return False
def main():
parser = argparse.ArgumentParser(description="Upload wine dataset to HuggingFace Hub")
parser.add_argument("--username", required=True, help="Your HuggingFace username")
parser.add_argument("--private", action="store_true", help="Make the dataset private")
parser.add_argument("--parquet-file", default="wine_text_126k.parquet",
help="Path to the parquet file")
args = parser.parse_args()
# Check if parquet file exists
if not os.path.exists(args.parquet_file):
print(f"โ Error: Parquet file not found: {args.parquet_file}")
print("Make sure you're running this script from the wine-text-126k directory")
return
print("๐ท Wine Text Dataset - HuggingFace Upload")
print("=" * 50)
# Load and prepare dataset
dataset = load_and_prepare_dataset(args.parquet_file)
# Upload to HuggingFace
success = upload_dataset(dataset, args.username, private=args.private)
if success:
print("\nโ
All done! Your dataset is now live on HuggingFace Hub.")
print("Don't forget to update the README.md with your actual username!")
else:
print("\nโ Upload failed. Please check the error messages above.")
if __name__ == "__main__":
main() |