Spaces:
Sleeping
Sleeping
Update Demo.py
Browse files
Demo.py
CHANGED
|
@@ -1,146 +1,147 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import sparknlp
|
| 3 |
-
import os
|
| 4 |
-
import pandas as pd
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
from sparknlp.
|
| 8 |
-
from
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
.
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
.
|
| 53 |
-
.
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
data
|
| 64 |
-
data =
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
StructField("
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
"
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
"
|
| 93 |
-
"
|
| 94 |
-
"
|
| 95 |
-
"
|
| 96 |
-
"
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
st.markdown('<div class="
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
st.sidebar.markdown(
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import sparknlp
|
| 3 |
+
import os
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import librosa
|
| 6 |
+
|
| 7 |
+
from sparknlp.base import *
|
| 8 |
+
from sparknlp.annotator import *
|
| 9 |
+
from pyspark.ml import Pipeline
|
| 10 |
+
from sparknlp.pretrained import PretrainedPipeline
|
| 11 |
+
|
| 12 |
+
# Page configuration
|
| 13 |
+
st.set_page_config(
|
| 14 |
+
layout="wide",
|
| 15 |
+
initial_sidebar_state="auto"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Custom CSS for styling
|
| 19 |
+
st.markdown("""
|
| 20 |
+
<style>
|
| 21 |
+
.main-title {
|
| 22 |
+
font-size: 36px;
|
| 23 |
+
color: #4A90E2;
|
| 24 |
+
font-weight: bold;
|
| 25 |
+
text-align: center;
|
| 26 |
+
}
|
| 27 |
+
.section {
|
| 28 |
+
background-color: #f9f9f9;
|
| 29 |
+
padding: 10px;
|
| 30 |
+
border-radius: 10px;
|
| 31 |
+
margin-top: 10px;
|
| 32 |
+
}
|
| 33 |
+
.section p, .section ul {
|
| 34 |
+
color: #666666;
|
| 35 |
+
}
|
| 36 |
+
</style>
|
| 37 |
+
""", unsafe_allow_html=True)
|
| 38 |
+
|
| 39 |
+
@st.cache_resource
|
| 40 |
+
def init_spark():
|
| 41 |
+
"""Initialize Spark NLP."""
|
| 42 |
+
return sparknlp.start()
|
| 43 |
+
|
| 44 |
+
@st.cache_resource
|
| 45 |
+
def create_pipeline(model):
|
| 46 |
+
"""Create a Spark NLP pipeline for audio processing."""
|
| 47 |
+
audio_assembler = AudioAssembler() \
|
| 48 |
+
.setInputCol("audio_content") \
|
| 49 |
+
.setOutputCol("audio_assembler")
|
| 50 |
+
|
| 51 |
+
speech_to_text = Wav2Vec2ForCTC \
|
| 52 |
+
.pretrained(model)\
|
| 53 |
+
.setInputCols("audio_assembler") \
|
| 54 |
+
.setOutputCol("text")
|
| 55 |
+
|
| 56 |
+
pipeline = Pipeline(stages=[
|
| 57 |
+
audio_assembler,
|
| 58 |
+
speech_to_text
|
| 59 |
+
])
|
| 60 |
+
return pipeline
|
| 61 |
+
|
| 62 |
+
def fit_data(pipeline, fed_data):
|
| 63 |
+
"""Fit the data into the pipeline and return the transcription."""
|
| 64 |
+
data, sampling_rate = librosa.load(fed_data, sr=16000)
|
| 65 |
+
data = [float(x) for x in data]
|
| 66 |
+
|
| 67 |
+
schema = StructType([
|
| 68 |
+
StructField("audio_content", ArrayType(FloatType())),
|
| 69 |
+
StructField("sampling_rate", LongType())
|
| 70 |
+
])
|
| 71 |
+
|
| 72 |
+
df = pd.DataFrame({
|
| 73 |
+
"audio_content": [data],
|
| 74 |
+
"sampling_rate": [sampling_rate]
|
| 75 |
+
})
|
| 76 |
+
|
| 77 |
+
spark_df = spark.createDataFrame(df, schema)
|
| 78 |
+
pipeline_df = pipeline.fit(spark_df).transform(spark_df)
|
| 79 |
+
return pipeline_df.select("text.result")
|
| 80 |
+
|
| 81 |
+
def save_uploadedfile(uploadedfile, path):
|
| 82 |
+
"""Save the uploaded file to the specified path."""
|
| 83 |
+
filepath = os.path.join(path, uploadedfile.name)
|
| 84 |
+
with open(filepath, "wb") as f:
|
| 85 |
+
if hasattr(uploadedfile, 'getbuffer'):
|
| 86 |
+
f.write(uploadedfile.getbuffer())
|
| 87 |
+
else:
|
| 88 |
+
f.write(uploadedfile.read())
|
| 89 |
+
|
| 90 |
+
# Sidebar content
|
| 91 |
+
model_list = [
|
| 92 |
+
"asr_wav2vec2_large_xlsr_53_english_by_jonatasgrosman",
|
| 93 |
+
"asr_wav2vec2_base_100h_13K_steps",
|
| 94 |
+
"asr_wav2vec2_base_100h_ngram",
|
| 95 |
+
"asr_wav2vec2_base_100h_by_facebook",
|
| 96 |
+
"asr_wav2vec2_base_100h_test",
|
| 97 |
+
"asr_wav2vec2_base_960h"
|
| 98 |
+
]
|
| 99 |
+
|
| 100 |
+
model = st.sidebar.selectbox(
|
| 101 |
+
"Choose the pretrained model",
|
| 102 |
+
model_list,
|
| 103 |
+
help="For more info about the models visit: https://sparknlp.org/models"
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Main content
|
| 107 |
+
st.markdown('<div class="main-title">Speech Recognition With Wav2Vec2ForCTC</div>', unsafe_allow_html=True)
|
| 108 |
+
st.markdown('<div class="section"><p>This demo transcribes audio files into texts using the <code>Wav2Vec2ForCTC</code> Annotator and advanced speech recognition models.</p></div>', unsafe_allow_html=True)
|
| 109 |
+
|
| 110 |
+
# Reference notebook link in sidebar
|
| 111 |
+
st.sidebar.markdown('Reference notebook:')
|
| 112 |
+
st.sidebar.markdown("""
|
| 113 |
+
<a href="https://githubtocolab.com/JohnSnowLabs/spark-nlp-workshop/blob/master/open-source-nlp/17.0.Automatic_Speech_Recognition_Wav2Vec2.ipynb">
|
| 114 |
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
|
| 115 |
+
</a>
|
| 116 |
+
""", unsafe_allow_html=True)
|
| 117 |
+
|
| 118 |
+
# Load examples
|
| 119 |
+
AUDIO_FILE_PATH = "inputs"
|
| 120 |
+
audio_files = sorted(os.listdir(AUDIO_FILE_PATH))
|
| 121 |
+
|
| 122 |
+
selected_audio = st.selectbox("Select an audio", audio_files)
|
| 123 |
+
|
| 124 |
+
# Creating a simplified Python list of audio file types
|
| 125 |
+
audio_file_types = ["mp3", "flac", "wav", "aac", "ogg", "aiff", "wma", "m4a", "ape", "dsf", "dff", "midi", "mid", "opus", "amr"]
|
| 126 |
+
uploadedfile = st.file_uploader("Try it for yourself!", type=audio_file_types)
|
| 127 |
+
|
| 128 |
+
if uploadedfile:
|
| 129 |
+
selected_audio = f"{AUDIO_FILE_PATH}/{uploadedfile.name}"
|
| 130 |
+
save_uploadedfile(uploadedfile, AUDIO_FILE_PATH)
|
| 131 |
+
elif selected_audio:
|
| 132 |
+
selected_audio = f"{AUDIO_FILE_PATH}/{selected_audio}"
|
| 133 |
+
|
| 134 |
+
# Audio playback and transcription
|
| 135 |
+
st.subheader("Play Audio")
|
| 136 |
+
|
| 137 |
+
with open(selected_audio, 'rb') as audio_file:
|
| 138 |
+
audio_bytes = audio_file.read()
|
| 139 |
+
st.audio(audio_bytes)
|
| 140 |
+
|
| 141 |
+
st.subheader(f"Transcription for {selected_audio}:")
|
| 142 |
+
|
| 143 |
+
spark = init_spark()
|
| 144 |
+
pipeline = create_pipeline(model)
|
| 145 |
+
output = fit_data(pipeline, selected_audio)
|
| 146 |
+
|
| 147 |
+
st.text(output.first().result[0].strip())
|