Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- app.py +25 -0
- movielist_images.pkl +3 -0
- programmeRecommendation.py +99 -0
- requirements.txt +6 -0
- similarity.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
from programmeRecommendation import recommendProgrammes
|
4 |
+
import uvicorn
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
origins = ["*"]
|
8 |
+
|
9 |
+
#handle CORS issue
|
10 |
+
app.add_middleware(
|
11 |
+
CORSMiddleware,
|
12 |
+
allow_origins=origins,
|
13 |
+
allow_credentials=True,
|
14 |
+
allow_methods=["*"],
|
15 |
+
allow_headers=["*"]
|
16 |
+
)
|
17 |
+
|
18 |
+
@app.post("/cmsai/recommendmovies")
|
19 |
+
def recommendMovies(scheduleDate : str, genre: list[str], referenceProgrammeTitle : str):
|
20 |
+
moviesInRecommendationList = recommendProgrammes(scheduleDate, genre, referenceProgrammeTitle)
|
21 |
+
return moviesInRecommendationList
|
22 |
+
|
23 |
+
if __name__ == '__main__':
|
24 |
+
recommendMovies('2024-12-01', ["Drama","Fantasy"], 'Halloween')
|
25 |
+
#uvicorn.run(app= app)
|
movielist_images.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:089bcdb5b5601e51a8826d04d1b96e9d8c17bbf8f9aa7c61ed8d5a13d4185ec6
|
3 |
+
size 2285443
|
programmeRecommendation.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import pickle
|
3 |
+
from pathlib import Path
|
4 |
+
import joblib
|
5 |
+
import os
|
6 |
+
import requests
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
os.environ['CALENDARIFIC_API_KEY'] = os.getenv("CALENDARIFIC_API_KEY")
|
13 |
+
|
14 |
+
currentPath = Path(__file__).resolve().parent
|
15 |
+
modelfilePath = currentPath / "similarity.pkl"
|
16 |
+
moviesfilePath = currentPath / "movielist_images.pkl"
|
17 |
+
|
18 |
+
with open(modelfilePath, 'rb') as file:
|
19 |
+
model = pickle.load(file)
|
20 |
+
|
21 |
+
with open(moviesfilePath,'rb') as file:
|
22 |
+
movieslist = joblib.load(file)
|
23 |
+
|
24 |
+
def recommendMoviesByTitle(programmeTitle):
|
25 |
+
recommended_movies = []
|
26 |
+
if programmeTitle in movieslist['programmetitle'].values:
|
27 |
+
movie_index = movieslist[movieslist['programmetitle'] == programmeTitle].index[0]
|
28 |
+
sim_score = model[movie_index]
|
29 |
+
movie_list = sorted(list(enumerate(sim_score)), reverse = True, key = lambda x: x[1])[1:11]
|
30 |
+
for i in movie_list:
|
31 |
+
recommended_movies.append({'programmetitle':movieslist.iloc[i[0]].programmetitle, 'imageurl': movieslist.iloc[i[0]].imagepath})
|
32 |
+
else:
|
33 |
+
return("No movies to recommend by keyword...")
|
34 |
+
return recommended_movies
|
35 |
+
|
36 |
+
COUNTRY_CODE = "US"
|
37 |
+
def get_holidaysByScheduleDate(scheduleDate):
|
38 |
+
key = os.environ["CALENDARIFIC_API_KEY"]
|
39 |
+
current_date = pd.to_datetime(scheduleDate,errors="coerce")
|
40 |
+
year = current_date.year
|
41 |
+
month = current_date.month
|
42 |
+
holidaylist = []
|
43 |
+
|
44 |
+
endpoint_url = f"https://calendarific.com/api/v2/holidays?api_key={key}&country={COUNTRY_CODE}&year={year}&month={month}"
|
45 |
+
response = requests.get(endpoint_url)
|
46 |
+
if response.status_code == 200:
|
47 |
+
holidays = response.json()['response']['holidays']
|
48 |
+
holidaylist = list(map(lambda x: x['description'],holidays))
|
49 |
+
|
50 |
+
return list(set(holidaylist))
|
51 |
+
else:
|
52 |
+
return []
|
53 |
+
|
54 |
+
def recommendMoviesByKeywordsAndGenres(keywordToSearchBy, genres, referenceProgrammeTitle):
|
55 |
+
recommended_movies = []
|
56 |
+
filtered_movies = []
|
57 |
+
# Convert the list of genres to lowercase for consistent comparison
|
58 |
+
genres = [genre.lower().strip() for genre in genres]
|
59 |
+
|
60 |
+
if keywordToSearchBy:
|
61 |
+
# Step 1: Filter the DataFrame to find movies that match the given keyword
|
62 |
+
filtered_movies = movieslist[movieslist['Name'].str.contains(keywordToSearchBy, case=False)]
|
63 |
+
|
64 |
+
if len(filtered_movies) == 0:
|
65 |
+
return recommendMoviesByTitle(referenceProgrammeTitle)
|
66 |
+
|
67 |
+
# Step 2: Further filter the movies based on the list of genres
|
68 |
+
filtered_movies = filtered_movies[filtered_movies['genredescription'].apply(
|
69 |
+
lambda genre_list: all(genre.lower() in [g.lower().strip() for g in genre_list.split(',')] for genre in genres))]
|
70 |
+
|
71 |
+
if filtered_movies.empty:
|
72 |
+
return("No movies found with the given keyword and genres.")
|
73 |
+
|
74 |
+
movies = filtered_movies.sample(len(filtered_movies))
|
75 |
+
|
76 |
+
recommended_movies.append({'programmetitle':movies.iloc[0]['programmetitle'],'imageurl': movies.iloc[0]['imagepath']})
|
77 |
+
|
78 |
+
movie_index = movies.index[0]
|
79 |
+
sim_score = model[movie_index]
|
80 |
+
|
81 |
+
movie_list = sorted(list(enumerate(sim_score)), reverse=True, key=lambda x: x[1])[1:10]
|
82 |
+
|
83 |
+
for i in movie_list:
|
84 |
+
recommended_movies.append({'programmetitle':movieslist.iloc[i[0]].programmetitle,'imageurl': movieslist.iloc[i[0]].imagepath})
|
85 |
+
return recommended_movies
|
86 |
+
|
87 |
+
|
88 |
+
def recommendProgrammes(scheduleDate : str, genres: list, referenceProgrammeTitle : str):
|
89 |
+
holidaylist = get_holidaysByScheduleDate(scheduleDate)
|
90 |
+
eventName = ""
|
91 |
+
if any("Christmas" in sentence for sentence in holidaylist):
|
92 |
+
eventName = "Christmas"
|
93 |
+
elif any("New year" in sentence for sentence in holidaylist):
|
94 |
+
eventName = "New year"
|
95 |
+
|
96 |
+
return recommendMoviesByKeywordsAndGenres(eventName,genres=genres, referenceProgrammeTitle = referenceProgrammeTitle)
|
97 |
+
|
98 |
+
|
99 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
pandas
|
4 |
+
joblib
|
5 |
+
python-dotenv
|
6 |
+
requests
|
similarity.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e79d90530995aca5f5770d12487c64dd33d4857930eb35d9864c99935a4a2d04
|
3 |
+
size 595953451
|