File uploading and reading using st.file_uploader

The issue is that model.transcribe is expecting either a file name string, or a numpy array, or a Tensor, and the UploadedFile is none of these. (see whisper/transcribe.py at main · openai/whisper · GitHub for more details)

The easiest way to solve this is to save the uploaded file to a temporary file with a known path.

from tempfile import NamedTemporaryFile

import streamlit as st
import whisper

audio = st.file_uploader("Upload an audio file", type=["mp3"])

if audio is not None:
    with NamedTemporaryFile(suffix="mp3") as temp:
        temp.write(audio.getvalue())
        temp.seek(0)
        model = whisper.load_model("base")
        result = model.transcribe(temp.name)
        st.write(result["text"])

This seems to work well. I can’t speak to the legal constraints, but if you are running this app on your local machine, then it won’t go anywhere else when you upload it. If you are running your app on a remote server, this method will certainly (at least temporarily) put the file on the server’s disk.

4 Likes