Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import speech_recognition as sr
|
| 3 |
+
from pocketsphinx import pocketsphinx, Jsgf, FsgModel
|
| 4 |
+
import requests
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
st.title("Speech to text recognition")
|
| 9 |
+
|
| 10 |
+
# st.markdown("## Here we use pocketsphinx model for automatic speech recognition")
|
| 11 |
+
|
| 12 |
+
audio = st.file_uploader(label = "Upload your audio file here in .wav format")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# audio_file = '/Users/kapilgupta/Downloads/audio/videoplayback.wav'
|
| 16 |
+
text_filename = "./subfolder/text_file"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
language_model = './language-model.lm.bin'
|
| 20 |
+
acoustic_model = './acoustic-model'
|
| 21 |
+
pronunciation_dict = './pronounciation-dictionary.dict'
|
| 22 |
+
|
| 23 |
+
@st.cache
|
| 24 |
+
def model(audio, text_filename):
|
| 25 |
+
framerate = 100
|
| 26 |
+
config = pocketsphinx.Config()
|
| 27 |
+
config.set_string('-hmm', acoustic_model)
|
| 28 |
+
config.set_string('-lm', language_model)
|
| 29 |
+
config.set_string('-dict', pronunciation_dict)
|
| 30 |
+
decoder = pocketsphinx.Decoder(config)
|
| 31 |
+
|
| 32 |
+
def recognize_sphinx(audio, show_all=True):
|
| 33 |
+
decoder.start_utt()
|
| 34 |
+
decoder.process_raw(audio.get_raw_data(), False, True)
|
| 35 |
+
decoder.end_utt()
|
| 36 |
+
hypothesis = decoder.hyp()
|
| 37 |
+
return decoder, hypothesis.hypstr
|
| 38 |
+
|
| 39 |
+
# Create a Recognizer instance
|
| 40 |
+
r = sr.Recognizer()
|
| 41 |
+
|
| 42 |
+
# Set the recognize_sphinx() function as the speech recognition method
|
| 43 |
+
r.recognize_sphinx = recognize_sphinx
|
| 44 |
+
|
| 45 |
+
with sr.AudioFile(audio) as source:
|
| 46 |
+
audio = r.record(source)
|
| 47 |
+
sample_rate = audio.sample_rate
|
| 48 |
+
decoder, recognized_text = r.recognize_sphinx(audio, show_all=True)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
with open(text_filename, 'w') as text_file:
|
| 52 |
+
for seg in decoder.seg():
|
| 53 |
+
segment_info = (seg.word, seg.start_frame/sample_rate, seg.end_frame/sample_rate)
|
| 54 |
+
text_file.write(str(segment_info) + "\n")
|
| 55 |
+
return recognized_text
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
if audio is not None:
|
| 59 |
+
with st.spinner("code is at Working! "):
|
| 60 |
+
segment_info = model(audio, text_filename)
|
| 61 |
+
st.write(segment_info)
|
| 62 |
+
st.balloons()
|
| 63 |
+
else:
|
| 64 |
+
st.write("Upload an audio")
|