-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
201 lines (163 loc) · 4.55 KB
/
test.py
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
193
194
195
196
197
198
199
200
201
import json
import redis
import requests
import pandas as pd
from typing import List, Dict, Any
def create_redis_client(
host: str,
port: int,
db: int
) -> redis.Redis:
"""
Create a Redis client.
Parameters:
- host (str): The Redis host.
- port (int): The Redis port.
- db (int): The Redis database.
Returns:
- (redis.Redis): The Redis client.
"""
redis_client = redis.Redis(
host = host,
port = port,
db = db
)
redis_client.ping()
return redis_client
def initialize_redis_db(
redis_client: redis.Redis,
dataset: str,
) -> None:
"""
Populate redis database with movies.
Parameters:
- redis_client (redis.Redis): Redis client.
- dataset (str): The dataset to load movies from.
"""
movies_df = pd.read_parquet(f'data/{dataset}-movies.parquet')
for _, row in movies_df.iterrows():
movie = row.to_dict()
movie_id = movie.get('movie_id')
movie.pop('movie_genres')
redis_client.set(
name = str(movie_id),
value = json.dumps(movie)
)
def retrieval_phase(
url: str,
user: Dict[str, Any],
approximate: bool = True,
top_k: int = 10,
) -> List[str|int]:
"""
Perform retrieval for a given user.
Parameters:
- url (str): Url of the api endpoint.
- user (Dict[str, Any]): User data.
- approximate (bool): Whether to use an approximate nearest neighbors
search or an exact search. Defaults to `True`.
- top_k (int): The number of items to retrieve. Defaults to `10`.
Returns:
- identifiers (list[str|int]): A list of item identifiers.
"""
response = requests.get(
url = url,
params = {
'approximate': approximate,
'top_k': top_k,
},
json = user
)
return response.json()
def get_movie(
redis_client: redis.Redis,
id: str,
) -> Dict[str, Any]:
"""
Get a movie from Redis by its id.
Parameters:
- redis_client (redis.Redis): Redis client.
- id (str): Movie id.
Returns:
- (Dict[str, Any]): Movie data.
"""
movie = redis_client.get(
name = str(id)
)
return json.loads(movie)
def get_movies(
redis_client: redis.Redis,
movie_ids: List[str],
) -> Dict[str, dict]:
"""
Get multiple movies from Redis by their ids.
Parameters:
- redis_client (redis.Redis): Redis client.
- movie_ids (List[str]): List of movie ids.
Returns:
- (Dict[str, dict]): Dictionary of movie data.
"""
movies: Dict[str, dict] = {}
for movie_id in movie_ids:
movie = get_movie(redis_client, movie_id)
movies[movie_id] = movie
return movies
def ranking_phase(
movies: Dict[str, dict],
user: Dict[str, Any],
url: str,
) -> Dict[str, float]:
"""
Perform ranking for a given user.
Parameters:
- movies (Dict[str, dict]): A dictionary of movie data.
- user (Dict[str, Any]): User data.
- url (str): Url of the api endpoint.
Returns:
- (Dict[str, float]): A dictionary of movie identifiers and their
corresponding scores.
"""
response = requests.get(
url = url,
json = {
'movies': movies,
'user': user,
}
)
return response.json()
if __name__ == '__main__':
user = {
'user_id': '138',
'user_gender': 1,
'user_zip_code': '53211',
'user_bucketized_age': 45.0,
'user_occupation_label': 4
}
# Create a Redis client
redis_client: redis.Redis = create_redis_client(
host = '0.0.0.0',
port = 6379,
db = 1
)
# Populate redis database with movies
initialize_redis_db(redis_client, '100k')
# Retrieval phase
movie_ids = retrieval_phase(
url = 'http://0.0.0.0:8000/api/v1/retrieval',
user = user,
approximate = True, # True: ScaNN, False: Brute
top_k = 10
)
print(f"{movie_ids = }")
# Get movies
movies = get_movies(
redis_client = redis_client,
movie_ids = movie_ids
)
# Ranking phase
movie_scores = ranking_phase(
movies = list(movies.values()),
user = user,
url = 'http://0.0.0.0:8000/api/v1/ranking',
)
print(movie_scores)