Team Ai
Apppublic

SabadModi/MultilingualSentimentAnalysis

sourceHugging Faceapache-2.0updated 4y agoView on Hugging Face
0likes
app.py106 linesDownload Raw Back to root
1import gradio as gr2import tensorflow as tf 3import numpy as np4import librosa5import time6from transformers import pipeline7from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer8 9p = pipeline("automatic-speech-recognition")10 11def onlyEnglish(filename):12    model = tf.keras.models.load_model("models/English.hdf5")13    class_names = ['Anger','Anxious','Apologetic','Concerned','Encouraging','Excited','Happiness','Sadness']14    audio, sample_rate = librosa.load(filename, res_type='kaiser_fast') 15    mfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)16    mfccs_scaled_features = np.mean(mfccs_features.T,axis=0)17    mfccs_scaled_features=mfccs_scaled_features.reshape(1,-1)18    predicted_label=model.predict(mfccs_scaled_features)19    classes_x=np.argmax(predicted_label,axis=1)20    class_num = classes_x[0]21    return class_names[class_num]22 23def englishGerman(filename):24    model = tf.keras.models.load_model("models/English-German.hdf5")25    class_names = ['Anger','Happiness','Sadness']26    audio, sample_rate = librosa.load(filename, res_type='kaiser_fast') 27    mfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)28    mfccs_scaled_features = np.mean(mfccs_features.T,axis=0)29    mfccs_scaled_features=mfccs_scaled_features.reshape(1,-1)30    predicted_label=model.predict(mfccs_scaled_features)31    classes_x=np.argmax(predicted_label,axis=1)32    class_num = classes_x[0]33    return class_names[class_num]34 35def multiple(filename):36    model = tf.keras.models.load_model("models/Multiple.hdf5")37    class_names = ["Anger","Disgust","Fear","Happiness","Neutral","Sadness"]38    audio, sample_rate = librosa.load(filename, res_type='kaiser_fast') 39    mfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)40    mfccs_scaled_features = np.mean(mfccs_features.T,axis=0)41    mfccs_scaled_features=mfccs_scaled_features.reshape(1,-1)42    predicted_label=model.predict(mfccs_scaled_features)43    classes_x=np.argmax(predicted_label,axis=1)44    class_num = classes_x[0]45    return class_names[class_num]46 47#calculate the negative, positive, neutral and compound scores, plus verbal evaluation48def sentiment_vader(sentence):49 50    # Create a SentimentIntensityAnalyzer object.51    sid_obj = SentimentIntensityAnalyzer()52 53    sentiment_dict = sid_obj.polarity_scores(sentence)54    negative = sentiment_dict['neg']55    neutral = sentiment_dict['neu']56    positive = sentiment_dict['pos']57    compound = sentiment_dict['compound']58 59    if sentiment_dict['compound'] >= 0.05 :60        overall_sentiment = "Positive"61 62    elif sentiment_dict['compound'] <= - 0.05 :63        overall_sentiment = "Negative"64 65    else :66        overall_sentiment = "Neutral"67 68    return overall_sentiment69 70def transcribe(audio, state=""):71    time.sleep(3)72    text = p(audio)["text"]73    text = sentiment_vader(text)74    return text75 76with gr.Blocks() as demo:77    gr.Markdown("<h1 style='text-align: center'>Deployed 3 different Models for different types of languages.</h1><ol><li>Model 1 has 8 Classes of Emotions and is trained only on English Language. This model extracts features and trains a NN model upon that.</li><li>Model 2 has 3 Classes of Emotions and is trained only on English and German. This model extracts features and trains a NN model upon that.</li><li>Model 3 has 6 Classes of Emotions and is trained only on English, French, German and Mexican. This model extracts features and trains a NN model upon that.</li><li>Model 4 has 3 Classes of Emotions and is trained only on English. It converts Speech-to-Text and then performs Sentiment Analysis.</li></ol>")78    with gr.Tabs():79        with gr.TabItem("Only English Model"):80            english_input = gr.inputs.Audio(label="Input Audio", type="filepath")81            english_button = gr.Button("Submit")82            english_output = gr.outputs.Label(num_top_classes = 8)83            84        with gr.TabItem("English-German Model"):85            two_input = gr.inputs.Audio(label="Input Audio", type="filepath")86            two_button = gr.Button("Submit")87            two_output = gr.outputs.Label(num_top_classes = 3)88            89        with gr.TabItem("English-French-German-Mexican Model"):90            multiple_input = gr.inputs.Audio(label="Input Audio", type="filepath")91            multiple_button = gr.Button("Submit")92            multiple_output = gr.outputs.Label(num_top_classes = 6)93        94        with gr.TabItem("Speech to Text"):95            stt_input = gr.inputs.Audio(label="Input Audio", type="filepath")96            stt_button = gr.Button("Submit")97            stt_output = gr.outputs.Label(num_top_classes = 6)98 99    english_button.click(onlyEnglish, inputs=english_input, outputs=english_output)100    two_button.click(englishGerman, inputs=two_input, outputs=two_output)101    multiple_button.click(englishGerman, inputs=multiple_input, outputs=multiple_output)102    stt_button.click(transcribe, inputs=stt_input, outputs=stt_output)103 104demo.launch()105 106