How to create a download link for an audio file

I have a python code that generates an audio file from text. I can play the audio file successfully by using‘audio.mp3’), but I also want to create a download button with st.download_button() so that one can download the audio file.
I am trying to do it with
st.download_button('Download story as mp3 file', 'audio.mp3', 'audio.mp3')
but the downloaded file is corrupted.

The description of the data argument to st.download_buton says (emphasis added):

The contents of the file to be downloaded. See example below for caching techniques to avoid recomputing this data unnecessarily.

You are passing a file name, not the contents of the file.

How can I pass the contents of the audio file?

By reading them from the file

with open("audio.mp3", "rb") as f:
    data =
1 Like

Thanks this works!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.