from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware import sys, os from download_data import download_tmdb_data sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from model.recommender import load_data, build_tags, build_model, recommend, get_movie_details app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) POPULAR = [ "The Dark Knight", "Inception", "Interstellar", "The Godfather", "Pulp Fiction", "Fight Club", "The Matrix", "Goodfellas", "Schindler's List", "Avatar" ] @app.on_event("startup") async def startup(): global df, similarity download_tmdb_data() df = load_data() df = build_tags(df) similarity = build_model(df) @app.get("/recommend") def get_recommendations(title: str): titles = recommend(title, df, similarity) return [{"title": t, **get_movie_details(t)} for t in titles] @app.get("/popular") def get_popular(): return [{"title": t, **get_movie_details(t)} for t in POPULAR] @app.get("/movies") def get_movies(): return {"movies": sorted(df["title"].tolist())}