|
import streamlit as st |
|
import logging |
|
from transformers import pipeline |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
|
|
|
|
asr = pipeline(task="automatic-speech-recognition", |
|
model="facebook/wav2vec2-base-960h") |
|
|
|
|
|
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)}" |
|
|
|
|
|
def main(): |
|
st.title("Simple Speech Recognition App") |
|
st.write("### This app allows you to record or upload audio and see its transcription.") |
|
|
|
|
|
audio_file = st.file_uploader("Upload Audio File", type=["wav", "mp3"]) |
|
if audio_file: |
|
st.audio(audio_file, format='audio/wav') |
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
|
|
|
|