CoolFace
Apppublic

Layer7/audio_gender_sentiment_analysis

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py86 linesDownload Raw Back to root
1import gradio as gr2from keras.models import model_from_json3import matplotlib.pyplot as plt4import keras 5import pickle6import pandas as pd7import numpy as np8import librosa9import librosa.display10 11def transform_data(audio):12    # Lets transform the dataset so we can apply the predictions13    X, sample_rate = librosa.load(audio14                                ,res_type='kaiser_fast'15                                ,duration=2.516                                ,sr=4410017                                ,offset=0.518                                )19    20    sample_rate = np.array(sample_rate)21    mfccs = librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=30)22    mfccs = np.expand_dims(mfccs, axis=-1)23    return mfccs24 25def predict(newdf, loaded_model):26    # Apply predictions27    newdf= np.expand_dims(newdf, axis=0)28    # print("***HERRRREEEEE*** ", newdf.shape )29    newpred = loaded_model.predict(newdf, 30                            batch_size=16, 31                            verbose=1)32    return newpred33 34    35def get_label(newpred):36    filename = 'models/labels'37    infile = open(filename,'rb')38    lb = pickle.load(infile)39    infile.close()40 41    # Get the final predicted label42    final = newpred.argmax(axis=1)43    final = final.astype(int).flatten()44    final = (lb.inverse_transform((final)))45    return final46 47def load_model():48    # loading json and model architecture 49    json_file = open('models/model_json_conv2D.json', 'r')50    loaded_model_json = json_file.read()51    json_file.close()52    loaded_model = model_from_json(loaded_model_json)53 54    # load weights into new model55    loaded_model.load_weights("models/Emotion_Model_conv2D.h5")56    print("Loaded model from disk")57 58    # the optimiser59    opt = keras.optimizers.RMSprop(lr=0.00001, decay=1e-6)60    loaded_model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])61    return loaded_model62 63 64def main(audio):65    newdf = transform_data(audio)66    loaded_model = load_model()67    newpred = predict(newdf, loaded_model)68    final = get_label(newpred)69    return "Classification: " + final70 71demo = gr.Interface(72    title = "๐ŸŽ™๏ธ Audio Gender/Emotion Analysis ๐ŸŽ™๏ธ",73    description = "<h3>A Neural Network to classify the gender of the voice (male/female) and the emotion, such as: happy, angry, sad, etc. </h3> <br> <b>Record your voice</b>",74    allow_flagging = "never",75    fn = main,76    inputs=gr.Audio(77        sources=["microphone"],78        type="filepath",79    ), 80    outputs="text"81    )82    83 84 85if __name__ == "__main__":86    demo.launch(show_api=False)