Spaces:
Build error
Build error
Commit
·
219ebb0
1
Parent(s):
d537a69
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import streamlit.components.v1 as stc
|
| 3 |
+
import noisereduce as nr
|
| 4 |
+
import librosa
|
| 5 |
+
import soundfile as sf
|
| 6 |
+
import numpy as np
|
| 7 |
+
import plotly.graph_objects as go
|
| 8 |
+
import pickle
|
| 9 |
+
|
| 10 |
+
from pyannote.audio.utils.signal import Binarize
|
| 11 |
+
import torch
|
| 12 |
+
|
| 13 |
+
@st.cache
|
| 14 |
+
def speech_activity_detection_model():
|
| 15 |
+
# sad = torch.hub.load('pyannote-audio', 'sad_ami', source='local', device='cpu', batch_size=128)
|
| 16 |
+
with open('speech_activity_detection_model.pkl', 'rb') as f:
|
| 17 |
+
sad = pickle.load(f)
|
| 18 |
+
return sad
|
| 19 |
+
|
| 20 |
+
@st.cache
|
| 21 |
+
def trim_noise_part_from_speech(sad, fname, speech_wav, sr):
|
| 22 |
+
file_obj = {"uri": "filename", "audio": fname}
|
| 23 |
+
sad_scores = sad(file_obj)
|
| 24 |
+
binarize = Binarize(offset=0.52, onset=0.52, log_scale=True, min_duration_off=0.1, min_duration_on=0.1)
|
| 25 |
+
speech = binarize.apply(sad_scores, dimension=1)
|
| 26 |
+
|
| 27 |
+
noise_wav = np.zeros((speech_wav.shape[0], 0))
|
| 28 |
+
append_axis = 1 if speech_wav.ndim == 2 else 0
|
| 29 |
+
noise_ranges = []
|
| 30 |
+
noise_start = 0
|
| 31 |
+
for segmentation in speech.segmentation():
|
| 32 |
+
noise_end, next_noise_start = int(segmentation.start*sr), int(segmentation.end*sr)
|
| 33 |
+
noise_wav = np.append(noise_wav, speech_wav[:, noise_start:noise_end], axis=append_axis)
|
| 34 |
+
noise_ranges.append((noise_start/sr, noise_end/sr))
|
| 35 |
+
noise_start = next_noise_start
|
| 36 |
+
return noise_wav.T, noise_ranges
|
| 37 |
+
|
| 38 |
+
@st.cache
|
| 39 |
+
def trim_audio(data, rate, start_sec=None, end_sec=None):
|
| 40 |
+
start, end = int(start_sec * rate), int(end_sec * rate)
|
| 41 |
+
if data.ndim == 1: # mono
|
| 42 |
+
return data[start:end]
|
| 43 |
+
elif data.ndim == 2: # stereo
|
| 44 |
+
return data[:, start:end]
|
| 45 |
+
|
| 46 |
+
title = 'Audio noise reduction'
|
| 47 |
+
st.set_page_config(page_title=title, page_icon=":sound:")
|
| 48 |
+
st.title(title)
|
| 49 |
+
|
| 50 |
+
uploaded_file = st.file_uploader("Upload your audio file (.wav)")
|
| 51 |
+
|
| 52 |
+
is_file_uploaded = uploaded_file is not None
|
| 53 |
+
if not is_file_uploaded:
|
| 54 |
+
uploaded_file = 'sample.wav'
|
| 55 |
+
|
| 56 |
+
wav, sr = librosa.load(uploaded_file, sr=None)
|
| 57 |
+
wav_seconds = int(len(wav)/sr)
|
| 58 |
+
|
| 59 |
+
st.subheader('Original audio')
|
| 60 |
+
st.audio(uploaded_file)
|
| 61 |
+
|
| 62 |
+
st.subheader('Noise part')
|
| 63 |
+
noise_part_detection_method = st.radio('Noise source detection', ['Manually', 'Automatically (using speech activity detections)'])
|
| 64 |
+
if noise_part_detection_method == "Manually": # ノイズ区間は1箇所
|
| 65 |
+
default_ranges = (0.0, float(wav_seconds)) if is_file_uploaded else (73.0, float(wav_seconds))
|
| 66 |
+
noise_part_ranges = [st.slider("Select a part of the noise (sec)", 0.0, float(wav_seconds), default_ranges, step=0.1)]
|
| 67 |
+
noise_wav = trim_audio(wav, sr, noise_part_ranges[0][0], noise_part_ranges[0][1])
|
| 68 |
+
|
| 69 |
+
elif noise_part_detection_method == "Automatically (using speech activity detections)": # ノイズ区間が複数
|
| 70 |
+
with st.spinner('Please wait for Detecting the speech activities'):
|
| 71 |
+
sad = speech_activity_detection_model()
|
| 72 |
+
noise_wav, noise_part_ranges = trim_noise_part_from_speech(sad, uploaded_file, wav, sr)
|
| 73 |
+
|
| 74 |
+
fig = go.Figure()
|
| 75 |
+
x_wav = np.arange(len(wav)) / sr
|
| 76 |
+
fig.add_trace(go.Scatter(y=wav[::1000]))
|
| 77 |
+
for noise_part_range in noise_part_ranges:
|
| 78 |
+
fig.add_vrect(x0=int(noise_part_range[0]*sr/1000), x1=int(noise_part_range[1]*sr/1000), fillcolor="Red", opacity=0.2)
|
| 79 |
+
fig.update_layout(width=700, margin=dict(l=0, r=0, t=0, b=0, pad=0))
|
| 80 |
+
fig.update_yaxes(visible=False, ticklabelposition='inside', tickwidth=0)
|
| 81 |
+
st.plotly_chart(fig, use_container_with=True)
|
| 82 |
+
|
| 83 |
+
st.text('Noise audio')
|
| 84 |
+
sf.write('noise_clip.wav', noise_wav, sr)
|
| 85 |
+
noise_wav, sr = librosa.load('noise_clip.wav', sr=None)
|
| 86 |
+
st.audio('noise_clip.wav')
|
| 87 |
+
|
| 88 |
+
if st.button('Denoise the audio!'):
|
| 89 |
+
with st.spinner('Please wait for completion'):
|
| 90 |
+
nr_wav = nr.reduce_noise(audio_clip=wav, noise_clip=noise_wav, prop_decrease=1.0)
|
| 91 |
+
|
| 92 |
+
st.subheader('Denoised audio')
|
| 93 |
+
sf.write('nr_clip.wav', nr_wav, sr)
|
| 94 |
+
st.success('Done!')
|
| 95 |
+
st.text('Denoised audio')
|
| 96 |
+
st.audio('nr_clip.wav')
|