-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
120 lines (101 loc) · 4.41 KB
/
app.py
File metadata and controls
120 lines (101 loc) · 4.41 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
FastAPI web application for Music Playlist Generator
Provides REST API endpoints for playlist generation and licensing checks.
"""
from pydantic import BaseModel
from config.settings import Settings
from fastapi import FastAPI, HTTPException
from typing import Dict, Any, Optional, List
from fastapi.middleware.cors import CORSMiddleware
from src.services.licensing_checker import LicensingChecker
from src.services.playlist_generator import PlaylistGenerator
app = FastAPI(
title="Music Playlist Generator API",
description="Generate music playlists based on audio features with business licensing checks",
version="1.0.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Initialize settings
settings = Settings()
class PlaylistRequest(BaseModel):
audio_features: Dict[str, float]
length: int = 20
provider: str = "spotify"
check_licensing: bool = False
genres: Optional[List[str]] = None
@app.get("/")
async def root():
return {"message": "Music Playlist Generator API", "version": "1.0.0"}
@app.get("/health")
async def health_check():
return {"status": "healthy", "services": ["playlist_generator", "licensing_checker"]}
@app.post("/generate-playlist")
async def generate_playlist(request: PlaylistRequest):
"""Generate a playlist based on audio features and optional licensing checks."""
try:
async with PlaylistGenerator(settings) as playlist_generator:
# Generate playlist
playlist = await playlist_generator.generate_playlist(
audio_features=request.audio_features,
length=request.length,
provider=request.provider,
genre=request.genres[0] if request.genres else None
)
# Check licensing if requested
if request.check_licensing:
async with LicensingChecker(settings) as licensing_checker:
playlist = await licensing_checker.check_playlist_licensing(playlist)
return playlist.to_dict()
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/check-licensing")
async def check_track_licensing(track_info: Dict[str, str]):
"""Check licensing information for a specific track."""
try:
from src.models.track import Track
from src.models.audio_features import AudioFeatures
# Create a minimal track object for licensing check
track = Track(
id=track_info.get("id", "unknown"),
name=track_info.get("name", ""),
artists=[track_info.get("artist", "")],
album="",
duration_ms=0,
audio_features=AudioFeatures(),
provider="unknown"
)
async with LicensingChecker(settings) as licensing_checker:
licensing_info = await licensing_checker.check_track_licensing(track)
return licensing_info.to_dict()
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/providers")
async def get_supported_providers():
"""Get list of supported music providers."""
return {
"providers": [
{"id": "spotify", "name": "Spotify", "features": ["audio_features", "search", "playlists"]},
{"id": "apple_music", "name": "Apple Music", "features": ["search", "metadata"]},
]
}
@app.get("/audio-features/schema")
async def get_audio_features_schema():
"""Get the schema for audio features parameters."""
return {
"energy": {"type": "float", "range": [0.0, 1.0], "description": "Musical intensity and power"},
"valence": {"type": "float", "range": [0.0, 1.0], "description": "Musical positivity"},
"danceability": {"type": "float", "range": [0.0, 1.0], "description": "Rhythm and beat strength"},
"acousticness": {"type": "float", "range": [0.0, 1.0], "description": "Acoustic vs electronic"},
"instrumentalness": {"type": "float", "range": [0.0, 1.0], "description": "Vocal vs instrumental"},
"tempo": {"type": "float", "range": [50.0, 200.0], "description": "Beats per minute"},
"loudness": {"type": "float", "range": [-60.0, 0.0], "description": "Overall loudness in dB"}
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)