-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (29 loc) · 1.35 KB
/
main.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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
from lyrics_recommender import top_recommended_lyrics
# API
from fastapi import FastAPI, Request, Form
from fastapi.templating import Jinja2Templates
# In[ ]:
app = FastAPI()
templates = Jinja2Templates(directory="templates/")
@app.get("/")
async def root():
return "Welcome to the English Song Lyrics Recommender!"
# without html
@app.get("/top_recommended/{artist}_{song}_{n}")
async def get_top_recommended_lyrics(artist: str, song: str, n: int):
return top_recommended_lyrics(artist, song, n).transpose().to_dict()
# with html
@app.get("/top_html/{artist}_{song}_{n}")
async def get_top_recomm_lyrics(artist: str, song: str, n: int):
return {"Top recommended songs in the base:": top_recommended_lyrics(artist, song, n)}
@app.get("/top/{form}")
def form_post_top(request: Request):
result = "Type artist name (in lower case, with _ instead of spaces) and song name"
return templates.TemplateResponse('form_song_recommender.html', context={'request': request, 'result': result})
@app.post("/top/{form}")
def form_post_top(request: Request, artist: str = Form(...), song: str = Form(...), n: int = Form(...)):
result = top_recommended_lyrics(artist, song, n)
return templates.TemplateResponse('form_song_recommender.html', context={'request': request, 'result': result.to_html()})