File size: 899 Bytes
3461076
53d28a7
3461076
 
 
 
 
 
 
53d28a7
 
 
 
 
 
 
 
3461076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41cba23
3461076
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
from fastapi import FastAPI, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
import shutil
import uuid
import os
from inference import predict

app = FastAPI(title="Audio Emotion Recognition API")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

UPLOAD_DIR = "/tmp"
os.makedirs(UPLOAD_DIR, exist_ok=True)


@app.get("/")
def root():
    return {
        "message": "Audio Emotion Recognition API is running"
    }


@app.get("/health")
def health():
    return {"status": "ok"}


@app.post("/predict")
async def predict_emotion(file: UploadFile = File(...)):
    file_path = f"{UPLOAD_DIR}/{uuid.uuid4()}.wav"

    with open(file_path, "wb") as buffer:
        shutil.copyfileobj(file.file, buffer)

    result = predict(file_path)
    os.remove(file_path)

    
    return result