-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (37 loc) · 1.39 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
48
49
import os
from fastapi.middleware.cors import CORSMiddleware
from shared.logger import logger
from shared.models import MusicTasteRequestBody
from shared.format_info_prompt import format_info_prompt
from music_suggestions.chain import music_suggestions_chain
from music_taste.chain import music_taste_chain
from fastapi import FastAPI
app = FastAPI()
origin = os.environ.get('FRONTEND_URL', "http://localhost:5432")
logger.info('CORS origin: %s', origin)
app.add_middleware(
CORSMiddleware,
allow_origins=[origin],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/health")
def health():
return {"status": "ok"}
@app.post('/taste')
def taste(data: MusicTasteRequestBody):
logger.info('Taste request: %s', data.dump_formatted())
prompt = format_info_prompt(data.tracks, data.authors, personal=False)
logger.debug('Taste prompt: %s', prompt)
output = music_taste_chain.invoke({"input": prompt})
logger.info('Taste output: %s', output)
return output
@app.post('/suggestions')
def taste(data: MusicTasteRequestBody):
logger.info('Suggestions request: %s', data.dump_formatted())
prompt = format_info_prompt(data.tracks, data.authors, personal=True)
logger.debug('Suggestions prompt: %s', prompt)
output = music_suggestions_chain.invoke({"input": prompt})
logger.info('Suggestions output: %s', output)
return output