-
Notifications
You must be signed in to change notification settings - Fork 0
/
film_api.py
55 lines (45 loc) · 1.44 KB
/
film_api.py
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
45
46
47
48
49
50
51
52
53
54
55
"""API for the film rental recommender system."""
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from recommender import (
postgres_db,
get_film_information,
get_past_rentals,
get_recommendations,
)
# init fastapi
app = FastAPI(
title="Film recommendation API",
summary="Recommend films based on past rentals",
version="0.1.0",
license_info={"name": "MIT", "url": "https://mit-license.org/"},
)
# Make resources in static folder available
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/")
def get_index():
"""# Serve the static landing page on root."""
return FileResponse(path="static/index.html")
@app.get("/customers/{customer_id}")
def get_customer_by_id(customer_id: int) -> list[dict]:
"""# Get customer information.
This endpoint queries the database and returns
customer information.
"""
return postgres_db.run_query(
f"""
select *
from customer
where customer_id={customer_id}
""",
).to_dicts()
@app.get("/recommend/{customer_id}")
def recommend(customer_id: int, n: int = 5):
"""# Get recommendations.
This endpoint queries the database for past
rentals and then returns a recommendation
"""
past_rentals = get_past_rentals(customer_id)
recs = get_recommendations(past_rentals, n_rec=n)
return get_film_information(recs)