Install the dependencies in your local environment.
pip install -r requirements.txt
train_model.py
import sysimport joblibfrom lightfm import LightFMfrom lightfm.datasets import fetch_movielensif__name__=="__main__": no_components =int(sys.argv[1])print(f"Number of components is set to {no_components}")# Load the MovieLens 100k dataset. Only five# star ratings are treated as positive. data =fetch_movielens(min_rating=5.0)# Instantiate and train the model model =LightFM(no_components=no_components, loss='warp') model.fit(data['train'], epochs=30, num_threads=2)# Save the model joblib.dump(model, "model.joblib")
src/func_main.py
import joblibimport numpy as npfrom lightfm import LightFM# Load model oncemodel: LightFM = joblib.load("/model/files/model.joblib")# Get all item idsitem_ids = np.arange(0, 1682)defget_top_rank_item(user_id):# Calculate scores per item id y = model.predict(user_ids=[user_id], item_ids=item_ids)# Pick top 3 top_3 = y.argsort()[:-4:-1]# Return {'top_1': ..., 'top_2': ..., 'top_3': ...}returndict([(f"top_{i +1}", item_id) for i, item_id inenumerate(top_3)])
We train and upload our model with 5 components as movie_rec:v1
pythontrain_model.py5hsapply-fserving.yaml
Upload Model B
Next, we train and upload a new version of our original model with 20 components as movie_rec:v2
pythontrain_model.py20hsapply-fserving.yaml
We can check that we have multiple versions of our model by running:
hs model list
Create an Application
To create an A/B deployment we need to create an Application with a single execution stage consisting of two model variants. These model variants are our Model A and Model B correspondingly.
The following code will create such an application: