Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model backend #46

Open
wants to merge 101 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
c5fc2f7
created static folder
raghavnarula Oct 8, 2023
9e15d9e
linked static to html
raghavnarula Oct 8, 2023
3c4f130
changes
raghavnarula Oct 8, 2023
1fe9d4d
Separated inline styles into different file
AtharvaThorve Oct 8, 2023
5ab4e3f
Merge branch 'master' of https://github.com/raghavnarula/MovieRecomme…
AtharvaThorve Oct 8, 2023
6fb50c9
added classes to div tags
raghavnarula Oct 8, 2023
3884fe2
Fixed landing page
AtharvaThorve Oct 8, 2023
cc004e1
Provide 5 movies as recommendations to the user
AtharvaThorve Oct 8, 2023
2aee9a3
Updated gitignore to ignore experiment results
AtharvaThorve Oct 8, 2023
59b7fe4
Feedback button disabled
AtharvaThorve Oct 8, 2023
b9f3f32
Cleaned HTML code
AtharvaThorve Oct 8, 2023
b704c98
Added alert on feedback submission
AtharvaThorve Oct 8, 2023
af37fd1
remove movies from list
raghavnarula Oct 8, 2023
8976f4f
Merge branch 'master' of github.com:raghavnarula/MovieRecommender
raghavnarula Oct 8, 2023
7ebed9c
Added star based rating
AtharvaThorve Oct 9, 2023
2231497
added loading page
raghavnarula Oct 9, 2023
6c352f1
Added loader while movies are being recommended
AtharvaThorve Oct 10, 2023
be54832
Update README.md
AtharvaThorve Oct 10, 2023
786ce66
Refactored code for loading page
AtharvaThorve Oct 10, 2023
2b8a523
Refactored backend to render correct html page
AtharvaThorve Oct 10, 2023
e2adac0
Removed unnecessary code from js file
AtharvaThorve Oct 10, 2023
af99e6a
Removed unnecessary js from html file
AtharvaThorve Oct 10, 2023
a2de3e9
Merge pull request #3 from raghavnarula/refactor
raghavnarula Oct 10, 2023
b5f3f91
restructured the files
raghavnarula Oct 10, 2023
fd4fb39
more restructuring
raghavnarula Oct 10, 2023
acc5c02
Merge pull request #5 from raghavnarula/ui
AtharvaThorve Oct 10, 2023
08f9d14
Deleted duplicate files
AtharvaThorve Oct 10, 2023
6f8fc99
Merge pull request #6 from raghavnarula/refactor
AtharvaThorve Oct 10, 2023
135a595
Item-Based.py file updated
divitkalathil Oct 10, 2023
7af89ee
Item-Based.py file updated
divitkalathil Oct 10, 2023
5e7f24e
Updated app.py
divitkalathil Oct 10, 2023
63f6fa2
Commit Item Based
Mrityunjay243 Oct 10, 2023
8b998c2
Updated workflow tool
AtharvaThorve Oct 10, 2023
59bafda
Updated OS in workflow
AtharvaThorve Oct 10, 2023
930fdbe
Updated python version
AtharvaThorve Oct 10, 2023
ea158b5
Fixed workflow
AtharvaThorve Oct 10, 2023
35ca981
Fixed test case
AtharvaThorve Oct 10, 2023
468d9d1
Added Badge for test workflow
AtharvaThorve Oct 10, 2023
5fb52ef
Updated Index.js
divitkalathil Oct 10, 2023
d73d74b
Merge pull request #8 from raghavnarula/workflows
divitkalathil Oct 10, 2023
7868f6f
changed UI for landing_page
raghavnarula Oct 10, 2023
0017c3c
create code coverage file
AtharvaThorve Oct 10, 2023
bf9a69b
Merge pull request #9 from raghavnarula/workflows
raghavnarula Oct 10, 2023
c01f22a
Merge pull request #10 from raghavnarula/ui
AtharvaThorve Oct 10, 2023
6e75439
gitignore
raghavnarula Oct 10, 2023
16339d1
Update README.md
AtharvaThorve Oct 11, 2023
328a366
added DOI tag
raghavnarula Oct 11, 2023
294637c
Merge branch 'master' into model-backend
Mrityunjay243 Oct 13, 2023
a47ac71
Restructured the code and bugfixed
AtharvaThorve Oct 13, 2023
9a976cb
Merge pull request #15 from raghavnarula/bugFix
Mrityunjay243 Oct 13, 2023
64b2c38
Merge branch 'master' into model-backend
Mrityunjay243 Oct 13, 2023
8f2425f
Added view comment button
AtharvaThorve Oct 14, 2023
38bcaa8
Added method to handle view comment route
AtharvaThorve Oct 14, 2023
a888c2c
Increased star size
AtharvaThorve Oct 14, 2023
7dbcbab
Templating added
AtharvaThorve Oct 14, 2023
d9c8e8c
Update README.md
raghavnarula Oct 14, 2023
bdd8111
changes
raghavnarula Oct 14, 2023
7322d18
deleted files
raghavnarula Oct 14, 2023
5253f85
Merge branch 'master' into ui
raghavnarula Oct 14, 2023
31d20b9
Merge pull request #18 from raghavnarula/ui
raghavnarula Oct 14, 2023
66f5f7a
comment feature for predicted movie
raghavnarula Oct 14, 2023
b7803c9
removed data from comments.csv
raghavnarula Oct 14, 2023
20263f2
Merge pull request #19 from raghavnarula/ui
AtharvaThorve Oct 14, 2023
16ed60a
Updating Ratings.csv based on user selection
divitkalathil Oct 14, 2023
daffceb
Merge branch 'model-backend' of https://github.com/raghavnarula/Movie…
divitkalathil Oct 14, 2023
7cb4864
updated tests
Mrityunjay243 Oct 14, 2023
6468a97
Merge branch 'model-backend' of https://github.com/raghavnarula/Movie…
Mrityunjay243 Oct 14, 2023
1be433c
updated requirements.txt
Mrityunjay243 Oct 14, 2023
e0f9ae9
final updated tests
Mrityunjay243 Oct 15, 2023
1f5b8cd
Added view comment button
AtharvaThorve Oct 14, 2023
23ac25a
Added method to handle view comment route
AtharvaThorve Oct 14, 2023
4ffb30f
Increased star size
AtharvaThorve Oct 14, 2023
ac2641e
Templating added
AtharvaThorve Oct 14, 2023
bd7f1c7
Added css
AtharvaThorve Oct 14, 2023
7d092c5
Updated file name
AtharvaThorve Oct 14, 2023
d1ec24b
Displaying comments
AtharvaThorve Oct 14, 2023
303c835
Refactored
AtharvaThorve Oct 15, 2023
b8c4a59
Display comments
AtharvaThorve Oct 15, 2023
8134d2f
Added view comments
AtharvaThorve Oct 15, 2023
95f39e5
Integrated set comments with view comments
AtharvaThorve Oct 15, 2023
e9e2c75
Merge branch 'view-comments' of https://github.com/raghavnarula/Movie…
AtharvaThorve Oct 15, 2023
20b0b66
Merge branch 'master' into model-backend
Mrityunjay243 Oct 15, 2023
d016940
Formatted files
AtharvaThorve Oct 15, 2023
ddcd775
Fixed tests
AtharvaThorve Oct 15, 2023
55b730b
Deleted junk files
Mrityunjay243 Oct 15, 2023
94fbeee
Merge pull request #16 from raghavnarula/model-backend
divitkalathil Oct 15, 2023
2b534e4
Merge branch 'master' into view-comments
AtharvaThorve Oct 15, 2023
93f31e4
Fixed errors and improved code quality
AtharvaThorve Oct 15, 2023
9d2da9e
Deleted junk files
AtharvaThorve Oct 15, 2023
d8ede63
Merge pull request #17 from raghavnarula/view-comments
Mrityunjay243 Oct 15, 2023
b210500
improved padding
raghavnarula Oct 15, 2023
cc4b868
added username and emailId field
raghavnarula Oct 15, 2023
4f08524
Removed useless comments
AtharvaThorve Oct 15, 2023
638cc0a
Update README.md
AtharvaThorve Oct 15, 2023
3e7f9e6
Update README.md
AtharvaThorve Oct 15, 2023
b33f287
sentiment analysis code added
Mrityunjay243 Oct 15, 2023
e107370
sentiment analysis code added
Mrityunjay243 Oct 15, 2023
7ef8ebb
Fixed and Refactored sentiment analysis
AtharvaThorve Oct 16, 2023
dcc92b2
Formatted code, added tests, update requirements.txt
AtharvaThorve Oct 16, 2023
9d43e3a
Updated setup file
AtharvaThorve Oct 16, 2023
4260687
Merge branch 'model-backend' of https://github.com/raghavnarula/Movie…
AtharvaThorve Oct 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: test
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ["3.9", "3.10"]
os: [ubuntu-latest, macOS-latest, windows-latest]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Installing and Running tests
run: |
pip install -r requirements.txt
black .
cd test
coverage run -m test_prediction
coverage run -m -a test_search
coverage run -m -a test_sentiment
coverage json
env:
CI: true
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,6 @@ dmypy.json
.DS_Store
.vscode



experiment_results
index.html
loadingHandler.css
18 changes: 0 additions & 18 deletions .travis.yml

This file was deleted.

91 changes: 65 additions & 26 deletions Code/prediction_scripts/item_based.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,79 @@
import pandas as pd
import warnings
import os
from sklearn.metrics.pairwise import cosine_similarity

app_dir = os.path.dirname(os.path.abspath(__file__))
code_dir = os.path.dirname(app_dir)
project_dir = os.path.dirname(code_dir)

warnings.filterwarnings("ignore")

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

def recommendForNewUser(user_rating):

def getSentimentScores(sentence):
sid_obj = SentimentIntensityAnalyzer()

sentiment_dict = sid_obj.polarity_scores(sentence)

if sentiment_dict["compound"] >= 0.05:
return "Supportive"

elif sentiment_dict["compound"] <= -0.05:
return "Critical"

else:
return "Neutral"


def recommendForNewUser(user_rating, num_recommendations=10):
ratings = pd.read_csv(project_dir + "/data/ratings.csv")
movies = pd.read_csv(project_dir + "/data/movies.csv")
user = pd.DataFrame(user_rating)
userMovieID = movies[movies["title"].isin(user["title"])]
userRatings = pd.merge(userMovieID, user)

moviesGenreFilled = movies.copy(deep=True)
copyOfMovies = movies.copy(deep=True)
for index, row in copyOfMovies.iterrows():
copyOfMovies.at[index, "genres"] = row["genres"].split("|")
for index, row in copyOfMovies.iterrows():
for genre in row["genres"]:
moviesGenreFilled.at[index, genre] = 1
moviesGenreFilled = moviesGenreFilled.fillna(0)

userGenre = moviesGenreFilled[moviesGenreFilled.movieId.isin(userRatings.movieId)]
userGenre.drop(["movieId", "title", "genres"], axis=1, inplace=True)
userProfile = userGenre.T.dot(userRatings.rating.to_numpy())
moviesGenreFilled.set_index(moviesGenreFilled.movieId)
moviesGenreFilled.drop(["movieId", "title", "genres"], axis=1, inplace=True)

recommendations = (moviesGenreFilled.dot(userProfile)) / userProfile.sum()
joinMoviesAndRecommendations = movies.copy(deep=True)
joinMoviesAndRecommendations["recommended"] = recommendations
joinMoviesAndRecommendations.sort_values(
by="recommended", ascending=False, inplace=True

average_ratings = ratings.groupby("movieId")["rating"].mean().reset_index()
movies = movies.merge(average_ratings, on="movieId", how="left")
movies.rename(columns={"rating": "average_rating"}, inplace=True)

user_movie_titles = [movie["title"] for movie in user_rating]
userMovieID = movies[movies["title"].isin(user_movie_titles)]
userRatings = pd.merge(userMovieID, pd.DataFrame(user_rating))

genre_columns = movies["genres"].str.get_dummies("|")

moviesGenreFilled = pd.concat([movies, genre_columns], axis=1)

moviesGenreFilled.drop(["genres"], axis=1, inplace=True)

cosine_sim = cosine_similarity(genre_columns, genre_columns)

similarity_df = pd.DataFrame(
cosine_sim, index=movies["movieId"], columns=movies["movieId"]
)
return [x for x in joinMoviesAndRecommendations["title"]][:201]

similar_movies = {}
for movie_id in userRatings["movieId"]:
similar_scores = (
similarity_df.loc[movie_id]
.sort_values(ascending=False)[1 : num_recommendations + 1]
.index.tolist()
)
similar_movies[movie_id] = similar_scores

recommendations = []
for similar_movie_ids in similar_movies.values():
recommended_movies = []
for sim_movie_id in similar_movie_ids:
sim_movie_title = movies[movies["movieId"] == sim_movie_id]["title"].values[
0
]
recommended_movies.append(
{"title": sim_movie_title, "rating": 5.0}
) # Replace 5.0 with the actual rating
recommendations.extend(recommended_movies)

# Sort recommendations by some criteria, e.g., rating
recommendations.sort(key=lambda x: x["rating"], reverse=True)

# Return a list of recommended movies (titles and ratings)
return recommendations[:num_recommendations]
48 changes: 41 additions & 7 deletions Code/recommenderapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
from flask_cors import CORS, cross_origin
import json
import sys
import csv
import time

import os
import pandas as pd


sys.path.append("../../")
from Code.prediction_scripts.item_based import recommendForNewUser
from Code.prediction_scripts.item_based import getSentimentScores, recommendForNewUser
from search import Search
from comments import Comments

app = Flask(__name__)
app.secret_key = "secret key"
Expand All @@ -17,20 +21,27 @@

@app.route("/")
def landing_page():
return render_template("loading.html")


@app.route("/home")
def redirected():
return render_template("landing_page.html")


@app.route("/predict", methods=["POST"])
def predict():
data = json.loads(request.data) # contains movies
data1 = data["movie_list"]
data1 = [s[:-1] for s in data1]
training_data = []
for movie in data1:
movie_with_rating = {"title": movie, "rating": 5.0}
training_data.append(movie_with_rating)
recommendations = recommendForNewUser(training_data)
recommendations = recommendations[:10]
recommendations = recommendations[:5]
resp = {"recommendations": recommendations}
print(recommendations) # Add this line for debugging
return resp


Expand All @@ -50,16 +61,39 @@ def search():
@app.route("/feedback", methods=["POST"])
def feedback():
data = json.loads(request.data)
with open("experiment_results/feedback_{}.csv".format(int(time.time())), "w") as f:
for key in data.keys():
f.write("%s - %s\n" % (key, data[key]))
print(data)

comments = Comments()
comments.setComments(data)

app_dir = os.path.dirname(os.path.abspath(__file__))
code_dir = os.path.dirname(app_dir)
project_dir = os.path.dirname(code_dir)
movies = pd.read_csv(project_dir + "/data/movies.csv")
with open(project_dir + "/data/ratings.csv", "a") as f:
for key, value in data.items():
if type(data[key]) is list:
# Find the movieId corresponding to the movie title
movieId = movies.loc[movies["title"] == key, "movieId"].values[0]
rating = int(data[key][0])
userId = ""
timestamp = int(time.time())
if rating != 0:
f.write("{},{},{},{}\n".format(userId, movieId, rating, timestamp))

return data


@app.route("/comments/<movie>")
def comments(movie):
comments = Comments()
movie_entries = comments.getComments(movie)
return render_template("view_comments.html", movie_entries=movie_entries)


@app.route("/success")
def success():
return render_template("success.html")


if __name__ == "__main__":
app.run(port=5000, debug=True)
46 changes: 46 additions & 0 deletions Code/recommenderapp/comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pandas as pd

import os
import csv
import datetime

from Code.prediction_scripts.item_based import getSentimentScores

app_dir = os.path.dirname(os.path.abspath(__file__))
code_dir = os.path.dirname(app_dir)
project_dir = os.path.dirname(code_dir)
file_path = project_dir + "/data/comments.csv"


class Comments:
def __init__(self):
pass

def getComments(self, movie):
df = pd.read_csv(file_path)
movie_entries = df[df["movieName"] == movie]
return movie_entries

def setComments(self, data):
# Putting data into comments.csv
all_rows = []
username, emailId = data["username"], data["emailId"]
for key, value in data.items():
if type(value) is list:
if (
type(value[1]) is str and len(value[1]) > 0
): # Save only those fields that are populated
all_rows.append(
[
username,
emailId,
key,
value[1],
getSentimentScores(value[1]),
datetime.datetime.now(),
]
)
with open(file_path, mode="a", newline="") as file:
writer = csv.writer(file)
for row in all_rows:
writer.writerow(row)
Empty file.
8 changes: 0 additions & 8 deletions Code/recommenderapp/search.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import pandas as pd

# from app import app
from flask import jsonify, request, render_template
import sys
import os

app_dir = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -11,7 +8,6 @@


class Search:

df = pd.read_csv(project_dir + "/data/movies.csv")

def __init__(self):
Expand Down Expand Up @@ -48,7 +44,3 @@ def results(self, word):

def resultsTop10(self, word):
return self.results(word)[:10]


if __name__ == "__main__":
app.run()
26 changes: 26 additions & 0 deletions Code/recommenderapp/static/css/comments.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
body {
margin:100px;
background: #abbaab; /* fallback for old browsers */
background: -webkit-linear-gradient(to right, #ffffff, #abbaab); /* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to right, #ffffff, #abbaab); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}

.card-container {
display: flex;
align-items: center;
justify-content: center;
}

.card {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
transition: 0.3s;
width: 40%;
}

.card:hover {
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
}

.container {
padding: 2px 16px;
}
Loading