Spaces:
Sleeping
Sleeping
File size: 7,805 Bytes
38ae75d 66ca941 f91b841 66ca941 38ae75d 66ca941 38ae75d 66ca941 c6233c8 66ca941 a84be04 fea0964 66ca941 a84be04 6e6c6a9 a84be04 66ca941 38ae75d 66ca941 38ae75d 66ca941 a84be04 38ae75d a84be04 38ae75d 66ca941 38ae75d a84be04 |
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
import gradio as gr
import torch
import numpy as np
import pandas as pd
from datetime import datetime, timedelta
import os
from huggingface_hub import hf_hub_download
# Import from your project's modules
from models import SASRec
from data_prepare import SASRecDataModule, prepare_data
from utils import load_item_properties, load_category_tree, get_popular_items
# --- Global variables to hold loaded artifacts ---
# This prevents reloading the model and data on every prediction.
MODEL = None
DATAMODULE = None
ITEM_CATEGORY_MAP = None
CATEGORY_PARENT_MAP = None
POPULAR_ITEMS = None
# --- Data Loading and Preparation Functions ---
def load_artifacts():
"""
Downloads data from Hugging Face Hub, then loads all necessary artifacts
(model, data, mappings) into global variables.
This function is called only once when the app starts.
"""
global MODEL, DATAMODULE, ITEM_CATEGORY_MAP, CATEGORY_PARENT_MAP, POPULAR_ITEMS
print("--- Loading all artifacts for the Gradio app ---")
# Configuration
CHECKPOINT_PATH = "checkpoints/sasrec-epoch=06-val_hitrate@10=0.3629.ckpt"
DATA_REPO_ID = "Deathshot78/RetailRocket-Recommender-Data"
# FIX: Define the correct, full path to where the data will be after download.
# The nested structure comes from the path within the HF Hub dataset repo.
DATA_FOLDER = "data/RetailRocket-Recommender-Data/data/"
# --- Download Data from Hugging Face Hub ---
print(f"Downloading data from Hugging Face Hub repo: {DATA_REPO_ID}")
os.makedirs(DATA_FOLDER, exist_ok=True)
files_to_download = [
"events.csv", "item_properties_part1.csv",
"item_properties_part2.csv", "category_tree.csv"
]
for filename in files_to_download:
hf_hub_download(
repo_id=DATA_REPO_ID,
# The path to the file within the dataset repo
filename=f"data/RetailRocket-Recommender-Data/data/{filename}",
local_dir=".", # Download to the root of the Space, preserving structure
repo_type="dataset"
)
print("All data files downloaded successfully.")
# --- End of Download Logic ---
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
print(f"Loading model from checkpoint: {CHECKPOINT_PATH}...")
MODEL = SASRec.load_from_checkpoint(CHECKPOINT_PATH)
MODEL.to(device)
MODEL.eval()
print("Preparing data from downloaded files...")
# Use the corrected DATA_FOLDER path
train_set, validation_set, test_set = prepare_data(data_folder=DATA_FOLDER)
DATAMODULE = SASRecDataModule(train_set, validation_set, test_set)
DATAMODULE.setup()
print("Loading item and category maps...")
# Use the corrected DATA_FOLDER path
ITEM_CATEGORY_MAP = load_item_properties(data_folder=DATA_FOLDER)
CATEGORY_PARENT_MAP = load_category_tree(data_folder=DATA_FOLDER)
print("Calculating popular items for cold-start users...")
POPULAR_ITEMS = get_popular_items(train_set, k=10)
print("--- Artifacts loaded successfully. Ready to serve recommendations. ---")
def get_recommendations(visitor_id_str):
"""
The main prediction function for the Gradio interface.
Takes a visitor ID string, gets recommendations, and formats them for display.
"""
try:
visitor_id = int(visitor_id_str)
except (ValueError, TypeError):
return pd.DataFrame(), pd.DataFrame(), "Please enter a valid numerical Visitor ID."
user_history_ids = DATAMODULE.user_history.get(visitor_id)
def format_to_df(item_list):
data = []
for rank, item_id in enumerate(item_list, 1):
category_id = ITEM_CATEGORY_MAP.get(item_id, 'N/A')
parent_id = CATEGORY_PARENT_MAP.get(category_id, 'N/A') if pd.notna(category_id) else 'N/A'
data.append([rank, item_id, category_id, parent_id])
return pd.DataFrame(data, columns=['Rank', 'Item ID', 'Category ID', 'Parent ID'])
# --- Cold-Start User (Fallback to Popularity) ---
if user_history_ids is None:
history_df = pd.DataFrame(columns=['Rank', 'Item ID', 'Category ID', 'Parent ID'])
recs_df = format_to_df(POPULAR_ITEMS)
message = f"User {visitor_id} is new. Showing Top 10 popular items as a fallback."
return history_df, recs_df, message
# --- Existing User (Use SASRec Model) ---
history_df = format_to_df(user_history_ids)
history_indices = [DATAMODULE.item_map[i] for i in user_history_ids if i in DATAMODULE.item_map]
if not history_indices:
message = "None of this user's historical items are in the model's vocabulary."
return history_df, pd.DataFrame(), message
max_len = DATAMODULE.max_len
input_seq = history_indices[-max_len:]
padded_input = np.zeros(max_len, dtype=np.int64)
padded_input[-len(input_seq):] = input_seq
input_tensor = torch.LongTensor(np.array([padded_input]))
input_tensor = input_tensor.to(MODEL.device)
with torch.no_grad():
logits = MODEL(input_tensor)
last_item_logits = logits[0, -1, :]
top_indices = torch.topk(last_item_logits, 10).indices.tolist()
recommended_item_ids = [DATAMODULE.inverse_item_map[idx] for idx in top_indices if idx in DATAMODULE.inverse_item_map]
recs_df = format_to_df(recommended_item_ids)
message = f"Showing personalized SASRec recommendations for user {visitor_id}."
return history_df, recs_df, message
# --- Main Execution Block ---
if __name__ == "__main__":
# Load all artifacts once at startup
load_artifacts()
# Find some valid example users to show in the UI
users_in_train_history = set(DATAMODULE.user_history.keys())
users_in_test_set = set(DATAMODULE.test_df['visitorid'].unique())
valid_example_users = list(users_in_train_history.intersection(users_in_test_set))
# Convert numpy types to standard Python int for Gradio compatibility
example_list = [int(u) for u in valid_example_users[:4]] + [-999]
# Create and launch the Gradio interface
with gr.Blocks(theme=gr.themes.Soft(), title="SASRec Recommender") as iface:
gr.Markdown(
"""
# SASRec Sequential Recommender System
An interactive demo of a state-of-the-art recommender system trained on the RetailRocket dataset.
"""
)
with gr.Row():
with gr.Column(scale=1):
visitor_id_input = gr.Number(
label="Enter Visitor ID",
info="Enter a user's numerical ID to get recommendations."
)
submit_button = gr.Button("Get Recommendations", variant="primary")
gr.Examples(
examples=example_list,
inputs=visitor_id_input,
label="Example User IDs (Click to try)"
)
with gr.Column(scale=3):
status_message = gr.Textbox(label="Status", interactive=False)
with gr.Tabs():
with gr.TabItem("Top 10 Recommendations"):
recs_output = gr.DataFrame(label="Recommended Items")
with gr.TabItem("User's Recent History"):
history_output = gr.DataFrame(label="Interaction History")
submit_button.click(
fn=get_recommendations,
inputs=visitor_id_input,
outputs=[history_output, recs_output, status_message]
)
# For local testing, this creates a shareable link.
# On Hugging Face Spaces, this is not strictly necessary but doesn't hurt.
iface.launch(share=True)
|