File size: 1,169 Bytes
f4dbd54
2733ff4
 
f4dbd54
 
 
 
 
2733ff4
 
 
 
 
 
 
 
 
 
 
f4dbd54
2733ff4
f4dbd54
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import tempfile
import os
from speechbrain.inference.interfaces import foreign_class

# Initialize the classifier
classifier = foreign_class(source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP", pymodule_file="custom_interface.py", classname="CustomEncoderWav2vec2Classifier")

def save_uploaded_file(uploaded_file):
    temp_dir = tempfile.TemporaryDirectory()
    file_path = os.path.join(temp_dir.name, uploaded_file.name)
    with open(file_path, "wb") as f:
        f.write(uploaded_file.getbuffer())
    return file_path

def emotion(uploaded_file):
    if uploaded_file is not None:
        # Save the uploaded file to a temporary location
        file_path = save_uploaded_file(uploaded_file)
        # Classify the file
        out_prob, score, index, text_lab = classifier.classify_file(file_path)
        # Display the output
        st.write(text_lab)
    else:
        st.write("Please upload a file.")

def main():
    st.title("Emotion Recognition")
    uploaded_file = st.file_uploader("Upload audio file", type=["wav"])
    if uploaded_file is not None:
        emotion(uploaded_file)

if __name__ == "__main__":
    main()