From 4eaa907a9edc400d8c6c71ab5ad48d5fd366d054 Mon Sep 17 00:00:00 2001
From: Nikanorova Darya <darya.nikanorova@corp.mail.ru>
Date: Thu, 7 Nov 2024 13:18:55 +0300
Subject: [PATCH] Init week 8 seminar

---
 botify/botify/config.json   |  4 ++++
 botify/botify/experiment.py |  6 ++++--
 botify/botify/server.py     | 29 +++++++++++++++++++++++------
 sim/sim/envs/config.py      |  2 +-
 4 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/botify/botify/config.json b/botify/botify/config.json
index a49786c..849ce7c 100644
--- a/botify/botify/config.json
+++ b/botify/botify/config.json
@@ -23,6 +23,9 @@
   "REDIS_RECOMMENDATIONS_GCF_HOST": "redis",
   "REDIS_RECOMMENDATIONS_GCF_PORT": 6379,
   "REDIS_RECOMMENDATIONS_GCF_DB": 7,
+  "REDIS_TRACKS_WITH_DIVERSE_RECS_HOST": "redis",
+  "REDIS_TRACKS_WITH_DIVERSE_RECS_PORT": 6379,
+  "REDIS_TRACKS_WITH_DIVERSE_RECS_DB": 8,
   "TRACKS_CATALOG": "./data/tracks.json",
   "TOP_TRACKS": "./data/top_tracks.json",
   "RECOMMENDATIONS_UB_FILE_PATH": "./data/recommendations_ub.json",
@@ -31,6 +34,7 @@
   "RECOMMENDATIONS_DSSM_FILE_PATH": "./data/recommendations_dssm.json",
   "RECOMMENDATIONS_CONTEXTUAL_FILE_PATH": "./data/recommendations_contextual.json",
   "RECOMMENDATIONS_GCF_FILE_PATH": "./data/recommendations_gcf.json",
+  "TRACKS_WITH_DIVERSE_RECS_CATALOG_FILE_PATH": "./data/recommendations_20_5.json",
   "DATA_LOG_FILE": "./log/data.json",
   "DATA_LOG_FILE_MAX_BYTES": 104857600,
   "DATA_LOG_FILE_BACKUP_COPIES": 10
diff --git a/botify/botify/experiment.py b/botify/botify/experiment.py
index 5d764d2..c86e2b6 100644
--- a/botify/botify/experiment.py
+++ b/botify/botify/experiment.py
@@ -22,6 +22,7 @@ class Split(Enum):
     FOUR_WAY = 4
     FIVE_WAY = 5
     SEVEN_WAY = 7
+    EIGHT_WAY = 8
     NINE_WAY = 9
 
 
@@ -61,7 +62,7 @@ class Experiments:
     A static container for all the existing experiments.
     """
 
-    # TODO Seminar 7 step 3: configure AB
+    # TODO Seminar 8 step 3: configure AB
     AA = Experiment("AA", Split.HALF_HALF)
     STICKY_ARTIST = Experiment("STICKY_ARTIST", Split.HALF_HALF)
     TOP_POP = Experiment("TOP_POP", Split.FOUR_WAY)
@@ -70,6 +71,7 @@ class Experiments:
     DSSM = Experiment("DSSM", Split.HALF_HALF)
     CONTEXTUAL_DSSM_LFM = Experiment("CONTEXTUAL_DSSM_LFM", Split.FOUR_WAY)
     GCF = Experiment("GCF", Split.HALF_HALF)
+    ALL = Experiment("ALL", Split.EIGHT_WAY)
 
     def __init__(self):
-        self.experiments = [Experiments.GCF]
+        self.experiments = [Experiments.ALL]
diff --git a/botify/botify/server.py b/botify/botify/server.py
index 8976efe..0ecf198 100644
--- a/botify/botify/server.py
+++ b/botify/botify/server.py
@@ -34,7 +34,8 @@ recommendations_lfm = Redis(app, config_prefix="REDIS_RECOMMENDATIONS_LFM")
 recommendations_dssm = Redis(app, config_prefix="REDIS_RECOMMENDATIONS_DSSM")
 recommendations_contextual = Redis(app, config_prefix="REDIS_RECOMMENDATIONS_CONTEXTUAL")
 recommendations_gcf = Redis(app, config_prefix="REDIS_RECOMMENDATIONS_GCF")
-# TODO Seminar 7 step 1: create a redis db for GCF recs
+# TODO Seminar 8 step 1: create a redis db for DIV recs
+recommendations_div = Redis(app, config_prefix="REDIS_TRACKS_WITH_DIVERSE_RECS")
 
 data_logger = DataLogger(app)
 
@@ -60,7 +61,12 @@ catalog.upload_recommendations(
 catalog.upload_recommendations(
     recommendations_gcf.connection, "RECOMMENDATIONS_GCF_FILE_PATH",
 )
-# TODO Seminar 7 step 2: upload GCF recs
+# TODO Seminar 8 step 2: upload DIV recs
+catalog.upload_recommendations(
+    recommendations_div, "TRACKS_WITH_DIVERSE_RECS_CATALOG_FILE_PATH",
+    key_object='track', key_recommendations='recommendations'
+)
+
 
 top_tracks = TopPop.load_from_json(app.config["TOP_TRACKS"])
 
@@ -92,15 +98,26 @@ class NextTrack(Resource):
 
         args = parser.parse_args()
 
-        # TODO Seminar 7 step 4: wire AB
+        # TODO Seminar 8 step 4: wire AB
         fallback = Random(tracks_redis.connection)
-        treatment = Experiments.GCF.assign(user)
-
+        treatment = Experiments.ALL.assign(user)
 
         if treatment == Treatment.T1:
+            recommender = StickyArtist(tracks_redis.connection, artists_redis.connection, catalog)
+        elif treatment == Treatment.T2:
+            recommender = TopPop(catalog.top_tracks[:100], fallback)
+        elif treatment == Treatment.T3:
+            recommender = Indexed(recommendations_lfm.connection, catalog, fallback)
+        elif treatment == Treatment.T4:
+            recommender = Indexed(recommendations_dssm.connection, catalog, fallback)
+        elif treatment == Treatment.T5:
+            recommender = Contextual(recommendations_contextual.connection, catalog, fallback)
+        elif treatment == Treatment.T6:
             recommender = Indexed(recommendations_gcf.connection, catalog, fallback)
+        elif treatment == Treatment.T7:
+            recommender = Contextual(recommendations_div.connection, catalog, fallback)
         else:
-            recommender = Indexed(recommendations_lfm.connection, catalog, fallback)
+            recommender = fallback
 
         recommendation = recommender.recommend_next(user, args.track, args.time)
 
diff --git a/sim/sim/envs/config.py b/sim/sim/envs/config.py
index 9d8fe4d..d5ad175 100644
--- a/sim/sim/envs/config.py
+++ b/sim/sim/envs/config.py
@@ -16,7 +16,7 @@ class UserCatalogConfig:
     default_consume_bias: float = 5.0
     default_consume_sharpness: float = 1.0
     default_session_budget: int = 5
-    default_artist_discount_gamma: float = 0.5
+    default_artist_discount_gamma: float = 0.8
 
 
 @dataclass()
-- 
GitLab