wine-text-126k / upload_to_hf.py
cipher982's picture
Upload folder using huggingface_hub
58cb8a8 verified
#!/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()