File size: 879 Bytes
5517e3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, File, UploadFile
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
import numpy as np

app = FastAPI()

mapper = ["angry", "disgust", "fear", "happy",
          "neutral", "other", "sad", "surprised", "unknown"]

inference_pipeline = pipeline(
    task=Tasks.emotion_recognition,
    model="iic/emotion2vec_base_finetuned", model_revision="v2.0.4")


@app.post("/emotion_recognition")
async def emotion_recognition(audio_file: UploadFile = File(...)):
    audio_bytes = await audio_file.read()
    rec_result = inference_pipeline(
        audio_bytes, output_dir="./outputs", granularity="utterance", extract_embedding=False)
    max_emotion_score = np.argmax(rec_result[0]["scores"])
    return {
        "emotion": mapper[max_emotion_score],
        "confidence":rec_result[0]["scores"][max_emotion_score]
    }