Skip to content

Commit

Permalink
#53 partially done. Need to add asking to model
Browse files Browse the repository at this point in the history
  • Loading branch information
sbgonenc committed Dec 20, 2022
1 parent 84fd096 commit 5474939
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 65 deletions.
15 changes: 4 additions & 11 deletions API/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,10 @@ def get_recommended_movies(user_id):
and deliver it to recommender model to process
also triggers sending recommender outputs to front
"""
from connect_db import MongoDB
##Ask to MongoDB if user exists
user_info = MongoDB("user1", "1234").get_user(user_id)
if user_info:
##from process_data.recommend_movies import recommender
##return recommender(user_id)
return {"movie_ids": [2], "movie_titles": ["something in the way"], "ratings": [3.6]}
return {}
from recommend_movies import RecommendMovies
return RecommendMovies(user_id, None, None).recommend()

@app.post("/rating")
def post_user_rating(user_id, movie_id, rating):
from connect_db import MongoDB
##Ask to MongoDB if user exists
return MongoDB("user1", "1234").post_user_rating(user_id, movie_id, rating)
from recommend_movies import RecommendMovies
return RecommendMovies(user_id, None, None).rate_movie(movie_id, rating)
8 changes: 7 additions & 1 deletion API/connect_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class MongoDB:
"""
This is the main class for getting and posting information to our mongo database
"""
def __init__(self, db_access_key, db_pass, db="spotlight_main", collection="main",):
def __init__(self, db_access_key="user1", db_pass="1234", db="spotlight_main", collection="main",):
self.client = self.get_mongo_client(db_access_key, db_pass)
self.db = self.client[db]
self.collection = self.db[collection]
Expand All @@ -26,6 +26,9 @@ def insert_many_entries(self, entries:List[Dict]):

def delete_entry(self, entry:Dict):
return self.collection.delete_one(entry, comment=f"deleted {entry}")

def get_one_info(self, query:Dict):
return self.collection.find_one(query)

def get_info(self, query:Dict):
return [entry for entry in self.collection.find(query)]
Expand All @@ -45,3 +48,6 @@ def post_user_rating(self, user_id, movie_id, rating):

def delete_user_entry(self, user_id, movie_id):
return self.delete_entry({"user_id": user_id, "movie_id": movie_id})

def get_movie(self, movie_id):
return self.get_one_info({"movie_id": movie_id})
50 changes: 50 additions & 0 deletions API/recommend_movies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from connect_db import MongoDB
from typing import List, Dict
class RecommendMovies:
MONGO_ACCESS = MongoDB()

def __init__(self, user_id, explicit_model, sequential_model):
self.user_id = user_id
self.user_data:List[Dict] = self.get_user_data(user_id)
self.explicit_model = explicit_model
self.sequential_model= sequential_model

def recommend(self):
if not self.user_data:
return None
previous_movies = self.get_previously_watched_movies()
exp_recommend_movies = self.get_recommendations_from_explicit_model()
seq_recommend_movies = self.get_recommendations_from_sequential_model()
#exp_recommend_movies = set()
filtered_movies = self.filter_previously_watched_movies(exp_recommend_movies, previous_movies)
return self.get_movie_title_and_genre(filtered_movies)

def rate_movie(self, movie_id, rating):
if not self.user_data:
return None
self.MONGO_ACCESS.post_user_rating(self.user_id, movie_id, rating=rating)
return True
def get_user_data(self,user_id):
return self.MONGO_ACCESS.get_user(user_id)
def get_recommendations_from_explicit_model(self):
##TODO: load explicit model, ask user_id, get movie ids
##TODO: connect it to the explicit model thing
return ["12", "11", "99"]
#return self.explicit_model(self.user_id)

def get_recommendations_from_sequential_model(self):
##TODO: load sequential model, ask user_id, get movie ids
pass
#return self.sequential_model(self.user_id)

def get_previously_watched_movies(self):
return [movie_data["movie_id"] for movie_data in self.user_data]
@staticmethod
def filter_previously_watched_movies(recommended_movies, previous_movies):
return list(set(recommended_movies).difference(set(previous_movies)))
def get_movie_title_and_genre(self, movie_ids:List):
recommended_movies = []
for movie_id in movie_ids:
movie_dict = self.MONGO_ACCESS.get_movie(movie_id)
recommended_movies.append({"title":movie_dict["title"], "genres":movie_dict["genres"]})
return recommended_movies
53 changes: 0 additions & 53 deletions process_data/process_user_prediction.py

This file was deleted.

0 comments on commit 5474939

Please sign in to comment.