File size: 1,257 Bytes
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
43
44
45
46
47
48
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from transformers import pipeline
import uvicorn
import tempfile

# Initialize FastAPI
app = FastAPI()

# Enable CORS for all origins (so Render or any client can access it)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Load the pretrained speech emotion recognition pipeline
emotion_pipeline = pipeline(
    "audio-classification",
    model="ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition"
)

# Health check route
@app.get("/")
def read_root():
    return {"message": "HF Space is live!"}

# Predict route
@app.post("/predict")
async def predict_emotion(file: UploadFile = File(...)):
    try:
        # Save the uploaded audio file to a temporary location
        with tempfile.NamedTemporaryFile(delete=False) as tmp:
            tmp.write(await file.read())
            tmp_path = tmp.name

        # Run emotion prediction
        result = emotion_pipeline(tmp_path)
        top_emotion = result[0]['label']

        return {"emotion": top_emotion}

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