AI Training - Tutorial - Deploy an app to classify sounds with pre-trained model using Streamlit

How to deploy a Streamlit app to classify sounds

Last updated 6th June, 2022.

Objective

The purpose of this tutorial is to deploy an application to classify sounds from a pre-trained model.

The use case is marine mammal sounds, a topic already covered in a previous tutorial.

In order to do this, you will use Streamlit, a Python framework that turns scripts into a shareable web application. You will also learn how to build and use a custom Docker image for a Streamlit application.

Overview of the app:

Overview

Requirements

Instructions

You are going to follow different steps to build your Streamlit application.

  • More information about Streamlit capabilities can be found here.
  • Direct link to the full python file can be found here here.

Write the Streamlit application

Create a Python file named app.py.

Inside that file, import your required modules.

import streamlit as st
import librosa
import csv
import os
import numpy as np
import pandas as pd
from PIL import Image
from keras.models import load_model
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import LabelEncoder

Create the function that allows to save the uploaded sounds in an Object Container.

def save_file(sound_file):
    # save your sound file in the right folder by following the path
    with open(os.path.join('audio_files/', sound_file.name),'wb') as f:
         f.write(sound_file.getbuffer())
    return sound_file.name

Create the function that allows to transform each sound into a csv file.

def transform_wav_to_csv(sound_saved):
    # define the column names
    header_test = 'filename length chroma_stft_mean chroma_stft_var rms_mean rms_var spectral_centroid_mean spectral_centroid_var spectral_bandwidth_mean \
        spectral_bandwidth_var rolloff_mean rolloff_var zero_crossing_rate_mean zero_crossing_rate_var harmony_mean harmony_var perceptr_mean perceptr_var tempo mfcc1_mean mfcc1_var mfcc2_mean \
        mfcc2_var mfcc3_mean mfcc3_var mfcc4_mean mfcc4_var'.split()
    # create the csv file
    file = open(f'csv_files/{os.path.splitext(sound_saved)[0]}.csv', 'w', newline = '')
    with file:
        writer = csv.writer(file)
        writer.writerow(header_test)      
    # calculate the value of the librosa parameters
    sound_name = f'audio_files/{sound_saved}'
    y, sr = librosa.load(sound_name, mono = True, duration = 30)
    chroma_stft = librosa.feature.chroma_stft(y = y, sr = sr)
    rmse = librosa.feature.rms(y = y)
    spec_cent = librosa.feature.spectral_centroid(y = y, sr = sr)
    spec_bw = librosa.feature.spectral_bandwidth(y = y, sr = sr)
    rolloff = librosa.feature.spectral_rolloff(y = y, sr = sr)
    zcr = librosa.feature.zero_crossing_rate(y)
    mfcc = librosa.feature.mfcc(y = y, sr = sr)
    to_append = f'{os.path.basename(sound_name)} {np.mean(chroma_stft)} {np.mean(rmse)} {np.mean(spec_cent)} {np.mean(spec_bw)} {np.mean(rolloff)} {np.mean(zcr)}'
    for e in mfcc:
        to_append += f' {np.mean(e)}'
    # fill in the csv file
    file = open(f'csv_files/{os.path.splitext(sound_saved)[0]}.csv', 'a', newline = '')
    with file:
        writer = csv.writer(file)
        writer.writerow(to_append.split())
    # create test dataframe
    df_test = pd.read_csv(f'csv_files/{os.path.splitext(sound_saved)[0]}.csv')
    # each time you add a sound, a line is added to the test.csv file
    # if you want to display the whole dataframe, you can deselect the following line
    #st.write(df_test)
    return df_test

Define the function that classifies the sounds from the previously trained model.

To learn more about how you can save a model with TensorFlow, please refer to the step 8 "Save the model for future inference" of the notebook.

Here you will use your trained model, then save it in an Object Container. Click here to learn more about Object Storage.

def classification(dataframe):
    # create a dataframe with the csv file of the data used for training and validation
    df = pd.read_csv('csv_files/data.csv')
    # OUTPUT: labels => last column
    labels_list = df.iloc[:,-1]
    # encode the labels (0 => 44)
    converter = LabelEncoder()
    y = converter.fit_transform(labels_list)
    # INPUTS: all other columns are inputs except the filename
    scaler = StandardScaler()
    X = scaler.fit_transform(np.array(df.iloc[:, 1:27]))
    X_test = scaler.transform(np.array(dataframe.iloc[:, 1:27]))
    # load the pretrained model
    model = load_model('saved_model/my_model')
    # generate predictions for test samples
    predictions = model.predict(X_test)
    # generate argmax for predictions
    classes = np.argmax(predictions, axis = 1)
    # transform class number into class name
    result = converter.inverse_transform(classes)
    return result

In the sidebar, if you have chosen Prediction, you will access the choice_prediction function.

It allows you to upload a sound file, convert it to a csv file and obtain the classification result by calling the functions defined previously.

def choice_prediction():
    st.write('# Prediction')
    st.write('### Choose a marine mammal sound file in .wav format')
    # upload sound
    uploaded_file = st.file_uploader(' ', type='wav')
    if uploaded_file is not None:  
        # view details
        file_details = {'filename':uploaded_file.name, 'filetype':uploaded_file.type, 'filesize':uploaded_file.size}
        st.write(file_details)
        # read and play the audio file
        st.write('### Play audio')
        audio_bytes = uploaded_file.read()
        st.audio(audio_bytes, format='audio/wav')
        # save_file function
        save_file(uploaded_file)
        # define the filename
        sound = uploaded_file.name
        # transform_wav_to_csv function
        transform_wav_to_csv(sound)
        st.write('### Classification results')
        # if you select the predict button
        if st.button('Predict'):
            # write the prediction: the prediction of the last sound sent corresponds to the first column
            st.write("The marine mammal is: ",  str(classification(transform_wav_to_csv(sound))).replace('[', '').replace(']', '').replace("'", '').replace('"', ''))
    else:
        st.write('The file has not been uploaded yet')
    return

All that remains is to define the main, where the sidebar and the home page are created.

if __name__ == '__main__':
    st.image(Image.open('logo_ovh.png'), width=200)
    st.write('___')
    # create a sidebar
    st.sidebar.title('Marine mammal sounds classification')
    select = st.sidebar.selectbox('', ['Marine mammals', 'Prediction'], key='1')
    st.sidebar.write(select)
    # if sidebar selection is "Prediction"
    if select=='Prediction':
        # choice_prediction function
        choice_prediction()
    # else: stay on the home page
    else:
        st.write('# Marine mammals')
        st.write('The different marine mammals studied are the following.')
        st.write('For more information, please refer to this [link](https://cis.whoi.edu/science/B/whalesounds/index.cfm).')
        st.image(Image.open('marine_mammal_animals.png'))

Write the requirements.txt file for the application

The requirements.txt file will allow us to write all the modules needed to make our application work. This file will be useful when writing the Dockerfile.

streamlit

Here we will mainly discuss how to write the app.py code, the requirements.txt file and the Dockerfile. If you want to see the whole code, please refer to the GitHub repository.

Write the Dockerfile for the application

Your Dockerfile should start with the the FROM instruction indicating the parent image to use. In our case we choose to start from the one-for-all OVHcloud image:

ovhcom/ai-training-one-for-all

Create the home directory and add your files to it:

WORKDIR /workspace
ADD . /workspace

Install the requirements.txt file which contains your needed Python modules using a pip install ... command:

RUN pip install -r requirements.txt

Define your default launching command to start the application:

CMD [ "streamlit" , "run" , "/workspace/app.py", "--server.address=0.0.0.0" ]

Give correct access rights to ovhcloud user (42420:42420):

RUN chown -R 42420:42420 /workspace
ENV HOME=/workspace

Build the Docker image from the Dockerfile

Launch the following command from the Dockerfile directory to build your application image:

docker build . -t streamlit_app:latest

The dot . argument indicates that your build context (place of the Dockerfile and other needed files) is the current directory.

The -t argument allows you to choose the identifier to give to your image. Usually image identifiers are composed of a name and a version tag <name>:<version>. For this example we chose sreamlit_app:latest.

Test it locally (optional)

Launch the following Docker command to launch your application locally on your computer:

docker run --rm -it -p 8501:8501 --user=42420:42420 streamlit_app:latest

The -p 8501:8501 argument indicates that you want to execute a port redirection from the port 8501 of your local machine into the port 8501 of the Docker container. The port 8501 is the default port used by Streamlit applications.

Don't forget the --user=42420:42420 argument if you want to simulate the exact same behaviour that will occur on AI Training jobs. It executes the Docker container as the specific OVHcloud user (user 42420:42420).

Once started, your application should be available on http://localhost:8501.

Push the image into the shared registry

The shared registry of AI Training should only be used for testing purpose. Please consider attaching your own Docker registry. More information about this can be found here.

Find the address of your shared registry by launching this command:

ovhai registry list

Log in on the shared registry with your usual OpenStack credentials:

docker login -u <user> -p <password> <shared-registry-address>

Push the compiled image into the shared registry:

docker tag streamlit_app:latest <shared-registry-address>/streamlit_app:latest
docker push <shared-registry-address>/streamlit_app:latest

Launch the job

The following command starts a new job running your Streamlit application:

ovhai job run --default-http-port 8501 \
      --cpu 1 \
      --volume <my_csv_files>@<region>/:/workspace/csv_files:RW \
      --volume <my_audio_files>@<region>/:/workspace/audio_files:RW \
      --volume <my_saved_model>@<region>/:/workspace/saved_model:RO \
      <shared-registry-address>/streamlit_app:latest

--default-http-port 8501 indicates that the port to reach on the job URL is the 8501.

--cpu 1 indicates that we request 1 CPU for that job.

To launch your Streamlit app, you need to attach 3 volumes to your job.

The first volume contains the csv files from the transformation of the sound files. By launching the job, this Object Container contains only the data.csv file created from step 4 "Data preprocessing" of the notebook.

--volume <my_csv_files>@<region>/:/workspace/csv_files:RW is the first volume attached for storing csv files. This volume is read/write (RW) because new csv files will be created and saved each time a new sound is uploaded.

The second volume contains the sound files that you will upload directly from the Streamlit app. By running the job, this Object Container is empty. It will fill up as the sound files are uploaded.

--volume <my_audio_files>@<region>/:/workspace/audio_files:RW is the second volume attached for storing audio files. This volume is read/write (RW) attached because new audio files will be saved there.

The third container contains the model that you trained before in step 8 "Save the model for future inference" of the notebook.

--volume <my_saved_model>@<region>/:/workspace/saved_model:RO is the third volume attached for using your pretrained model. This volume is read-only (RO) because you just need to use the model and not make any changes to this Object Container.

If you want your notebook to be accessible without the need to authenticate, specify it as follows.

Consider adding the --unsecure-http attribute if you want your application to be reachable without any authentication.

Feedback

Please send us your questions, feedback and suggestions to improve the service:


Did you find this guide useful?

Please feel free to give any suggestions in order to improve this documentation.

Whether your feedback is about images, content, or structure, please share it, so that we can improve it together.

Your support requests will not be processed via this form. To do this, please use the "Create a ticket" form.

Thank you. Your feedback has been received.


These guides might also interest you...

OVHcloud Community

Access your community space. Ask questions, search for information, post content, and interact with other OVHcloud Community members.

Discuss with the OVHcloud community