<jupyter_start><jupyter_text>Setting Dataset From Kaggle<jupyter_code>! pip install -q kaggle
from google.colab import files
files.upload()
! mkdir ~/.kaggle
! cp kaggle.json ~/.kaggle/
! chmod 600 ~/.kaggle/kaggle.json
! kaggle datasets list
! kaggle datasets download -d uwrfkaggler/ravdess-emotional-speech-audio
! mkdir ravdess-emotional-speech-audio
! unzip ravdess-emotional-speech-audio.zip -d ravdess-emotional-speech-audio<jupyter_output>Archive:  ravdess-emotional-speech-audio.zip
  inflating: ravdess-emotional-speech-audio/Actor_01/03-01-01-01-01-01-01.wav  
  inflating: ravdess-emotional-speech-audio/Actor_01/03-01-01-01-01-02-01.wav  
  inflating: ravdess-emotional-speech-audio/Actor_01/03-01-01-01-02-01-01.wav  
  inflating: ravdess-emotional-speech-audio/Actor_01/03-01-01-01-02-02-01.wav  
  inflating: ravdess-emotional-speech-audio/Actor_01/03-01-02-01-01-01-01.wav  
  inflating: ravdess-emotional-speech-audio/Actor_01/03-01-02-01-01-02-01.wav  
  inflating: ravdess-emotional-speech-audio/Actor_01/03-01-02-01-02-01-01.wav  
  inflating: ravdess-emotional-speech-audio/Actor_01/03-01-02-01-02-02-01.wav  
  inflating: ravdess-emotional-speech-audio/Actor_01/03-01-02-02-01-01-01.wav  
  inflating: ravdess-emotional-speech-audio/Actor_01/03-01-02-02-01-02-01.wav  
  inflating: ravdess-emotional-speech-audio/Actor_01/03-01-02-02-02-01-01.wav  
  inflating: ravdess-emotional-speech-audio/Actor_01/03-01-02-02-02-02-01.w[...]<jupyter_text>Import libraries and Dataset<jupyter_code>import pandas as pd
import librosa
import os, glob
import numpy as np
from tqdm import tqdm
from sklearn.model_selection import train_test_split
import warnings
warnings.filterwarnings('ignore')
for dirname, _, filenames in os.walk('/ravdess-emotional-speech-audio/'):
    for filename in filenames:
        print(os.path.join(dirname, filename))<jupyter_output><empty_output><jupyter_text>Preprocessing Function to extract features<jupyter_code>def extract_feature(file_name):
    X, sample_rate = librosa.load(file_name)
    stft=np.abs(librosa.stft(X))
    result=np.array([])
    mfccs=np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0)
    result=np.hstack((result, mfccs))
    chroma=np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)
    result=np.hstack((result, chroma))
    mel=np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)
    result=np.hstack((result, mel))
    return result<jupyter_output><empty_output><jupyter_text>Dictionary of emotions<jupyter_code>emotions={
  '01':'neutral',
  '02':'calm',
  '03':'happy',
  '04':'sad',
  '05':'angry',
  '06':'fearful',
  '07':'disgust',
  '08':'surprised'
}

def gender(g):
    if int(g[0:2]) % 2 == 0:
        return 'female'
    else:
        return 'male'<jupyter_output><empty_output><jupyter_text>Function to load data<jupyter_code>def load_data(test_size=0.2):
    x,y=[],[]
    for file in tqdm(glob.glob("ravdess-emotional-speech-audio/Actor_*/*.wav")):
        file_name=os.path.basename(file)
        emotion=emotions[file_name.split("-")[2]] + '_' + gender(file_name.split("-")[-1])
        feature=extract_feature(file)
        x.append(feature)
        y.append(emotion)
    return train_test_split(np.array(x), y, test_size=test_size, random_state=1)<jupyter_output><empty_output><jupyter_text>Split dataset for training and validating<jupyter_code>X_train, X_val, y_train, y_val = load_data()<jupyter_output>100%|██████████| 1440/1440 [06:12<00:00,  3.86it/s]<jupyter_text>Check shapes<jupyter_code>print((X_train.shape[0], X_val.shape[0]))<jupyter_output>(1152, 288)<jupyter_text>Scale data<jupyter_code>from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_val = scaler.transform(X_val)
print(f'Features extracted: {X_train.shape[1]}')<jupyter_output>Features extracted: 180<jupyter_text>Build Model using Scikit Learn Select model<jupyter_code>from sklearn.neural_network import MLPClassifier

model=MLPClassifier(alpha=0.01, batch_size=256, epsilon=1e-08, hidden_layer_sizes=(300,), learning_rate='adaptive', max_iter=500)
model.fit(X_train,y_train)
print(model.score(X_train, y_train))<jupyter_output>1.0<jupyter_text>Make predictions on validation set<jupyter_code>y_pred=model.predict(X_val)
print(model.score(X_val, y_val))
df=pd.DataFrame({'Actual': y_val, 'Predicted':y_pred})
df<jupyter_output><empty_output><jupyter_text>Make prediction on audio recording data<jupyter_code># Code for recording audio from the browser
from IPython.display import Javascript
from google.colab import output
from base64 import b64decode
import IPython
import uuid
from google.colab import output


class InvokeButton(object):
  def __init__(self, title, callback):
    self._title = title
    self._callback = callback

  def _repr_html_(self):
    from google.colab import output
    callback_id = 'button-' + str(uuid.uuid4())
    output.register_callback(callback_id, self._callback)

    template = """<button id="{callback_id}" style="cursor:pointer;background-color:#EEEEEE;border-color:#E0E0E0;padding:5px 15px;font-size:14px">{title}</button>
        <script>
          document.querySelector("#{callback_id}").onclick = (e) => {{
            google.colab.kernel.invokeFunction('{callback_id}', [], {{}})
            e.preventDefault();
          }};
        </script>"""
    html = template.format(title=self._title, callback_id=callback_id)
    return html

RECORD = """
const sleep  = time => new Promise(resolve => setTimeout(resolve, time))
const b2text = blob => new Promise(resolve => {
  const reader = new FileReader()
  reader.onloadend = e => resolve(e.srcElement.result)
  reader.readAsDataURL(blob)
})
var record = time => new Promise(async resolve => {
  stream = await navigator.mediaDevices.getUserMedia({ audio: true })
  recorder = new MediaRecorder(stream)
  chunks = []
  recorder.ondataavailable = e => chunks.push(e.data)
  recorder.start()
  await sleep(time)
  recorder.onstop = async ()=>{
    blob = new Blob(chunks)
    text = await b2text(blob)
    resolve(text)
  }
  recorder.stop()
})
"""

def record(sec=3):
  display(Javascript(RECORD))
  s = output.eval_js('record(%d)' % (sec*1000))
  b = b64decode(s.split(',')[1])
  with open('audio.wav','wb+') as f:
    f.write(b)
  return 'audio.wav'
import IPython.display as ipd

def synth():
  print("Now recording for 10 seconds, say what you will...")
  record(5)
  print("Audio recording complete")
  in_fpath = Path("audio.wav")
InvokeButton('Start recording', synth)
ipd.Audio("audio.wav")
audio=extract_feature("audio.wav")
x_audio = []
x_audio.append(audio)
np.array(x_audio)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_audio_val = scaler.transform(x_audio)
y_pred_audio=model.predict(X_audio_val)
hasil = str(y_pred_audio[0])
emotion_value = hasil.split("_")[0]
gender_value = hasil.split("_")[1]
print("The system detects that you are a",gender_value, "and your current emotions is", emotion_value)<jupyter_output>The system detects that you are a male and your current emotions is sad
{'fraction_non_alphanumeric': 0.1446682631356529, 'fraction_numerical': 0.04310466262853923, 'mean_word_length': 5.141868512110727, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '571726', 'license': 'MIT', 'n_tokens_mistral': 2869, 'n_tokens_neox': 2661, 'n_words': 425, 'path': 'emotion_and_gender_prediction_system.ipynb', 'repo_name': 'dindamajesty13/emotion-and-gender-prediction-system', 'size': '7099'}