speech-to-text / app.py
pratikshahp's picture
Update app.py
a1c0ebf verified
raw
history blame
974 Bytes
import torch
from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
import streamlit as st
from audio_recorder_streamlit import audio_recorder
audio_bytes = audio_recorder(pause_threshold=3.0, sample_rate=16_000)
if audio_bytes:
st.audio(audio_bytes, format="audio/wav")
# Load pre-trained model and tokenizer
tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h")
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
# Tokenize the audio input
input_values = tokenizer(audio_bytes, return_tensors='pt').input_values
# Perform inference
logits = model(input_values).logits
predicted_ids = torch.argmax(logits, dim=-1)
# Decode the audio to generate text
transcriptions = tokenizer.decode(predicted_ids[0])
if transcriptions is not None:
st.write(transcriptions)
else:
st.write("Error: Failed to decode audio.")
else:
st.write("No audio recorded.")