File size: 981 Bytes
0db48f0
 
 
884312d
0db48f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b88b3e
0db48f0
 
 
 
2b88b3e
 
 
 
 
 
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
from fastapi import FastAPI
import gzip
import pickle
import uvicorn

app = FastAPI()

# Load model
with gzip.open("pivot.pkl.gz", "rb") as f:
    pivot = pickle.load(f)

with gzip.open("knn_model.pkl.gz", "rb") as f:
    model = pickle.load(f)


def recommend(anime_name):
    anime_name = anime_name.lower().strip()

    matches = [anime for anime in pivot.index if anime_name in anime.lower()]

    if not matches:
        return ["Anime not found"]

    anime_name = matches[0]

    index = pivot.index.get_loc(anime_name)

    distances, indices = model.kneighbors(
        pivot.iloc[index, :].values.reshape(1, -1),
        n_neighbors=6
    )

    return [pivot.index[i] for i in indices[0][1:]]


@app.get("/")
def home():
    return {"message": "API running"}


@app.get("/recommend")
def get_recommendations(anime: str):
    return {"recommendations": recommend(anime)}


# 🚀 THIS WAS MISSING
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)