-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (47 loc) · 1.7 KB
/
main.py
File metadata and controls
56 lines (47 loc) · 1.7 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
# main.py
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
import analyzer
import os
import tempfile
app = FastAPI(title="MIRa Core API", description="A simple API for Music Information Retrieval Analysis.")
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"], # Allow all methods
allow_headers=["*"], # Allow all headers
)
@app.post("/analyze")
async def analyze_music_file(file: UploadFile = File(...)):
"""
Endpoint that accepts an audio file upload and returns its analysis.
"""
print(f"Received file: {file.filename}")
# Validate file type
if not file.content_type.startswith('audio/'):
return JSONResponse(
status_code=400,
content={"error": "Please upload an audio file (e.g., MP3, WAV)"}
)
# Create a temporary file
try:
suffix = os.path.splitext(file.filename)[1]
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
content = await file.read()
tmp.write(content)
tmp_file_path = tmp.name
# Analyze the file
analysis_results = analyzer.analyze_audio(tmp_file_path)
except Exception as e:
return JSONResponse(status_code=500, content={"error": f"Analysis failed: {str(e)}"})
finally:
# Clean up temporary file
if 'tmp_file_path' in locals():
os.unlink(tmp_file_path)
return analysis_results
@app.get("/")
async def root():
return {"message": "Welcome to the Music Analysis API. Use POST /analyze to analyze audio files."}