biplobgon/product-recommendation-system
0
1"""2data_prep.py3------------4Load and inspect the raw Retailrocket events dataset.5 6Data source priority:7 1. Local file at data/raw/events.csv (fast path – no network required)8 2. Google Cloud Storage bucket (auto-downloaded when local file is absent)9 10Set GCS_BUCKET_NAME (and optionally GCS_CREDENTIALS / GCS_PROJECT_ID) in a11.env file or as environment variables to configure the GCS connection.12See .env.example for details.13"""14 15import os16import sys17 18import pandas as pd19from dotenv import load_dotenv20 21load_dotenv()22 23# ---------------------------------------------------------------------------24# Resolve project root so the script works when called from any directory25# ---------------------------------------------------------------------------26_HERE = os.path.dirname(os.path.abspath(__file__))27_PROJECT_ROOT = os.path.dirname(_HERE)28EVENTS_PATH = os.path.join(_PROJECT_ROOT, "data", "raw", "events.csv")29 30 31def load_events() -> pd.DataFrame:32 """Return the events DataFrame, fetching from GCS if the file is missing."""33 if not os.path.exists(EVENTS_PATH):34 print(f"Local events file not found at {EVENTS_PATH}.")35 print("Attempting to download from Google Cloud Storage …")36 try:37 from gcs_loader import load_events as _gcs_load # noqa: PLC041538 except ImportError as exc:39 print(f"ERROR: Could not import gcs_loader: {exc}", file=sys.stderr)40 sys.exit(1)41 42 try:43 return _gcs_load(local_path=EVENTS_PATH)44 except FileNotFoundError as exc:45 print(f"ERROR: Dataset file not found in GCS: {exc}", file=sys.stderr)46 sys.exit(1)47 except Exception as exc: # noqa: BLE00148 # Covers google.auth.exceptions.DefaultCredentialsError and other49 # GCS / network failures with a helpful remediation hint.50 print(f"ERROR: Could not download events data from GCS: {exc}", file=sys.stderr)51 print(52 "Please ensure:\n"53 " 1. GCS_BUCKET_NAME is set correctly (see .env.example).\n"54 " 2. You are authenticated (gcloud auth application-default login\n"55 " or GCS_CREDENTIALS points to a valid service-account key).",56 file=sys.stderr,57 )58 sys.exit(1)59 60 return pd.read_csv(EVENTS_PATH)61 62 63if __name__ == "__main__":64 events = load_events()65 66 print(events.head())67 print(events.info())68 print(events["event"].value_counts())69 