phuntshowangdi's picture
Update app.py
ca805e0 verified
raw
history blame
No virus
1.52 kB
import streamlit as st
import logging
from transformers import pipeline
# Setup logging
logging.basicConfig(level=logging.INFO)
# Load speech recognition pipeline
asr = pipeline(task="automatic-speech-recognition",
model="facebook/wav2vec2-base-960h")
# Function for transcribing speech
def transcribe_speech(audio_file):
if not audio_file:
logging.error("No audio file provided.")
return "No audio found, please retry."
try:
logging.info(f"Processing file: {audio_file.name}")
audio_input = audio_file.read()
output = asr(audio_input)
return output[0]['transcription']
except Exception as e:
logging.error(f"Error during transcription: {str(e)}")
return f"Error processing the audio file: {str(e)}"
# Streamlit app UI
def main():
st.title("Simple Speech Recognition App")
st.write("### This app allows you to record or upload audio and see its transcription.")
# Upload audio file or record from microphone
audio_file = st.file_uploader("Upload Audio File", type=["wav", "mp3"])
if audio_file:
st.audio(audio_file, format='audio/wav')
# Button to transcribe audio
if st.button("Transcribe Audio"):
if audio_file:
transcription = transcribe_speech(audio_file)
st.write("### Transcription:")
st.write(transcription)
else:
st.warning("Please upload an audio file first.")
if __name__ == "__main__":
main()