File size: 1,160 Bytes
96d23a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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())}