Download model/recommender.py from Adpacci/cinematch: direct link, hf CLI and curl.
- Browser
- Download file 2.77 kB
-
https://huggingface.co/spaces/Adpacci/cinematch/resolve/main/model/recommender.py
- Command line
-
hf download hf://spaces/Adpacci/cinematch/model/recommender.py
-
curl -L -o recommender.py https://huggingface.co/spaces/Adpacci/cinematch/resolve/main/model/recommender.py
2.77 kB
| import pandas as pd | |
| import ast | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| from sklearn.metrics.pairwise import cosine_similarity | |
| import requests | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| API_KEY = os.getenv("TMDB_API_KEY") | |
| def load_data(): | |
| movies = pd.read_csv("data/tmdb_5000_movies.csv") | |
| credits = pd.read_csv("data/tmdb_5000_credits.csv") | |
| df = movies.merge(credits, on="title") | |
| df = df[["title", "overview", "genres", "keywords", "cast", "crew"]] | |
| return df | |
| def parse_list(text): | |
| try: | |
| items = ast.literal_eval(text) | |
| return [item["name"] for item in items] | |
| except: | |
| return [] | |
| def get_director(text): | |
| try: | |
| crew = ast.literal_eval(text) | |
| for person in crew: | |
| if person["job"] == "Director": | |
| return person["name"] | |
| except: | |
| pass | |
| return "" | |
| def build_tags(df): | |
| df["genres"] = df["genres"].apply(parse_list) | |
| df["keywords"] = df["keywords"].apply(parse_list) | |
| df["cast"] = df["cast"].apply(parse_list) | |
| df["cast"] = df["cast"].apply(lambda x: x[:3]) # 3 acteurs max | |
| df["director"] = df["crew"].apply(get_director) | |
| df["tags"] = ( | |
| df["overview"].fillna("") + " " + | |
| df["genres"].apply(lambda x: " ".join(x)) + " " + | |
| df["keywords"].apply(lambda x: " ".join(x)) + " " + | |
| df["cast"].apply(lambda x: " ".join(x)) + " " + | |
| df["director"] + " " + | |
| df["title"] | |
| ) | |
| return df[["title", "tags"]] | |
| def build_model(df): | |
| tfidf = TfidfVectorizer(stop_words="english") | |
| matrix = tfidf.fit_transform(df["tags"]) | |
| similarity = cosine_similarity(matrix) | |
| return similarity | |
| def recommend(title, df, similarity): | |
| idx = df[df["title"] == title].index[0] | |
| scores = list(enumerate(similarity[idx])) | |
| scores = sorted(scores, key=lambda x:x[1],reverse=True) | |
| scores = scores[1:6] | |
| indices = [i[0] for i in scores] | |
| return df["title"].iloc[indices].tolist() | |
| def get_movie_details(title): | |
| url = f"https://api.themoviedb.org/3/search/movie?api_key={API_KEY}&query={title}" | |
| response = requests.get(url) | |
| data = response.json() | |
| if data["results"]: | |
| movie = data["results"][0] | |
| poster = f"https://image.tmdb.org/t/p/w500{movie['poster_path']}" if movie.get("poster_path") else None | |
| rating = movie.get("vote_average", "N/A") | |
| year = movie.get("release_date", "")[:4] | |
| return {"poster": poster, "rating": rating, "year": year} | |
| return {"poster": None, "rating": "N/A", "year": ""} | |
| if __name__ == "__main__": | |
| df = load_data() | |
| df = build_tags(df) | |
| similarity = build_model(df) | |
| print(recommend("Avatar", df, similarity)) | |
| print(get_movie_details("Avatar")) |