SURESHBEEKHANI commited on
Commit
7adf8b5
·
verified ·
1 Parent(s): 509f367

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +0 -0
  2. app.py +60 -0
  3. requirements.txt +13 -0
  4. setup.py +43 -0
Dockerfile ADDED
File without changes
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Importing necessary libraries from Flask
2
+ from flask import Flask, render_template, request # Flask creates web applications; render_template serves HTML, and request handles form data.
3
+ import pickle # pickle is used for loading pre-trained models or data stored in serialized files.
4
+ import requests # requests is used to make HTTP requests to external APIs.
5
+
6
+ # Initializing a Flask application
7
+ app = Flask(__name__)
8
+
9
+ # Load the movie list and similarity matrix
10
+ movies = pickle.load(open('artifacts/model.pkl', 'rb')) # Loads the list of movies from a serialized file.
11
+ similarity = pickle.load(open('artifacts/similarity.pkl', 'rb')) # Loads the similarity matrix for movies, used for recommendations.
12
+
13
+ # Function to fetch the poster image of a movie from The Movie Database (TMDB) API
14
+ def fetch_poster(movie_id):
15
+ # TMDB API URL, dynamically formatted with a movie ID and API key to get movie details
16
+ url = f"https://api.themoviedb.org/3/movie/{movie_id}?api_key=8265bd1679663a7ea12ac168da84d2e8&language=en-US"
17
+ data = requests.get(url).json() # Sends a GET request to the URL and retrieves the response in JSON format.
18
+ poster_path = data.get('poster_path') # Extracts the poster path if available.
19
+ if poster_path: # Checks if a poster path exists for the movie.
20
+ return "https://image.tmdb.org/t/p/w500/" + poster_path # Returns the full URL for the poster image.
21
+ return None # If no poster is available, returns None.
22
+
23
+ # Function to recommend similar movies
24
+ def recommend(movie):
25
+ # Gets the index of the selected movie in the movies DataFrame based on the movie title
26
+ index = movies[movies['title'] == movie].index[0]
27
+ # Enumerates similarity scores with the selected movie and sorts them in descending order
28
+ distances = sorted(list(enumerate(similarity[index])), reverse=True, key=lambda x: x[1])
29
+ recommended_movie_names = [] # List to store recommended movie titles.
30
+ recommended_movie_posters = [] # List to store corresponding poster images.
31
+
32
+ # Loop to get the top 5 similar movies (ignoring the first entry, which is the movie itself)
33
+ for i in distances[1:5]:
34
+ movie_id = movies.iloc[i[0]].movie_id # Gets the movie ID for each recommended movie.
35
+ recommended_movie_posters.append(fetch_poster(movie_id)) # Fetches and appends the movie's poster.
36
+ recommended_movie_names.append(movies.iloc[i[0]].title) # Adds the movie title to recommendations.
37
+
38
+ return recommended_movie_names, recommended_movie_posters # Returns lists of recommended movie titles and posters.
39
+
40
+ # Route for the homepage, handling both GET and POST requests
41
+ @app.route('/', methods=['GET', 'POST'])
42
+ def index():
43
+ # When a movie is selected and form is submitted (POST request)
44
+ if request.method == 'POST':
45
+ selected_movie = request.form.get('movie') # Retrieves the selected movie from the form.
46
+ # Calls the recommend function and gets recommended movie names and posters
47
+ recommended_movie_names, recommended_movie_posters = recommend(selected_movie)
48
+ # Renders the HTML page with the selected movie, recommendations, and movie list for dropdown
49
+ return render_template('index.html', selected_movie=selected_movie,
50
+ recommended_movie_names=recommended_movie_names,
51
+ recommended_movie_posters=recommended_movie_posters,
52
+ movies=movies['title'].values,
53
+ zip=zip) # Adds zip function to enable pairing movie names with posters.
54
+
55
+ # For GET requests, render the page with the movie list only.
56
+ return render_template('index.html', movies=movies['title'].values)
57
+
58
+ # Running the application in debug mode for development (provides error details on crashes)
59
+ if __name__ == '__main__':
60
+ app.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ numpy
2
+ pandas
3
+ matplotlib
4
+ seaborn
5
+ scikit-learn
6
+ ipykernel
7
+ dill
8
+ flask
9
+ flask_cors
10
+ lime
11
+ gunicorn
12
+ nltk
13
+ #-e .
setup.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from setuptools import find_packages, setup
2
+ from typing import List
3
+
4
+ HYPEN_E_DOT = '-e .'
5
+
6
+ def get_requirements(file_path: str) -> List[str]:
7
+ '''
8
+ This function will return the list of requirements from the specified file.
9
+ '''
10
+ requirements = []
11
+ with open(file_path) as file_obj:
12
+ requirements = file_obj.readlines()
13
+ # Strip whitespace and newlines from each requirement
14
+ requirements = [req.strip() for req in requirements]
15
+
16
+ # Remove the editable install if present
17
+ if HYPEN_E_DOT in requirements:
18
+ requirements.remove(HYPEN_E_DOT)
19
+
20
+ return requirements
21
+
22
+ # Setup function to define package metadata and dependencies
23
+ setup(
24
+ name="Movie_Recommender_System_Content_Based_Filtering", # Updated project name
25
+ version="1.0.0",
26
+ packages=find_packages(),
27
+ # Package metadata
28
+ author="Suresh Beekhani",
29
+ author_email="sureshbeekhani26@gmail.com",
30
+ description="A tool for recommending movies based on content-based filtering techniques.",
31
+ long_description="This package provides a comprehensive model for recommending movies using content-based filtering and data analysis techniques.",
32
+ long_description_content_type="text/markdown",
33
+ url="https://github.com/SURESHBEEKHANI/Movie-Recommender-System-Content-Based-Filtering.git", # Updated URL to match your project
34
+ # Read dependencies from requirements.txt
35
+ install_requires=get_requirements('requirements.txt'),
36
+ # License and classification
37
+ license="MIT",
38
+ classifiers=[
39
+ "Programming Language :: Python :: 3.12",
40
+ "License :: OSI Approved :: MIT License",
41
+ "Operating System :: OS Independent",
42
+ ],
43
+ )