File size: 1,038 Bytes
388bbfc
 
 
 
 
e67d2cf
388bbfc
 
 
e67d2cf
388bbfc
 
 
 
 
 
 
e67d2cf
 
388bbfc
 
e67d2cf
388bbfc
e67d2cf
 
388bbfc
 
 
e67d2cf
 
 
 
 
 
 
 
388bbfc
 
 
 
 
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
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from transformers import pipeline
import uvicorn
import tempfile
import torchaudio

app = FastAPI()

# Allow CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

# Load model
pipe = pipeline("audio-classification", model="superb/wav2vec2-base-superb-er")

@app.post("/predict")
async def predict(file: UploadFile = File(...)):
    try:
        # Save uploaded file to a temp file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
            tmp.write(await file.read())
            tmp_path = tmp.name

        # Load and preprocess audio
        waveform, sample_rate = torchaudio.load(tmp_path)

        # Get prediction
        result = pipe(tmp_path)

        # Get top prediction label
        top_emotion = result[0]["label"].lower()

        return {"emotion": top_emotion}

    except Exception as e:
        return {"error": str(e)}