Unable to load librosa while deployment

I’ve made an app that classifies cough sounds, it worked perfectly on my system. But while deploying the app I faced certain issues. How can I correctly install Librosa? Any guidance will be hugely appreciated.

Traceback (most recent call last):
File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/script_runner.py", line 350, in _run_script
 exec(code, module.__dict__)
 File "/app/cov_project/app.py", line 9, in <module>
import librosa
File "/home/appuser/venv/lib/python3.7/site-packages/librosa/__init__.py", line 211, in <module>
from . import core
File "/home/appuser/venv/lib/python3.7/site-packages/librosa/core/__init__.py", line 6, in <module>
from .audio import *  # pylint: disable=wildcard-import
File "/home/appuser/venv/lib/python3.7/site-packages/librosa/core/audio.py", line 8, in <module>
import soundfile as sf
File "/home/appuser/venv/lib/python3.7/site-packages/soundfile.py", line 142, in <module>
raise OSError('sndfile library not found')

OSError: sndfile library not found

Hey @HimanshuKGP007

For Librosa to work correctly on Streamlit Sharing, you’ll need to install additional OS libraries by adding a packages.txt file at the root of the project.

You can refer to OSError: sndfile library not found - #6 by snehankekre for the content of this file :slight_smile:

Happy Streamlitin’
Fanilo :balloon:

@andfanilo, thanks for the reply. The libraries got installed perfectly. I had another issue with the app; I want to record audio from the client-side, which I believe is not possible to do with python libraries alone. I still gave it a try and got this error:

Traceback (most recent call last):
  File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/script_runner.py", line 337, in _run_script
    exec(code, module.__dict__)
  File "/app/speech-emotion-webapp/app.py", line 42, in <module>
    myrecording = record(duration, fs)
  File "/app/speech-emotion-webapp/helper.py", line 137, in record
    myrecording = sd.rec(int(duration * fs))
  File "/home/appuser/venv/lib/python3.7/site-packages/sounddevice.py", line 275, in rec
    ctx.input_dtype, callback, blocking, **kwargs)
  File "/home/appuser/venv/lib/python3.7/site-packages/sounddevice.py", line 2577, in start_stream
    **kwargs)
  File "/home/appuser/venv/lib/python3.7/site-packages/sounddevice.py", line 1416, in __init__
    **_remove_self(locals()))
File "/home/appuser/venv/lib/python3.7/site-packages/sounddevice.py", line 812, in __init__
    extra_settings, samplerate)
  File "/home/appuser/venv/lib/python3.7/site-packages/sounddevice.py", line 2650, in _get_stream_parameters
    info = query_devices(device)
  File "/home/appuser/venv/lib/python3.7/site-packages/sounddevice.py", line 564, in query_devices
    raise PortAudioError('Error querying device {}'.format(device))
sounddevice.PortAudioError: Error querying device -1

Here’s the repo of the code. How do I solve this issue?

Your guess is correct :confused: until an audio recorder Streamlit native widget is implemented, which I believe is somewhere on the roadmap, your best bet would be using streamlit-webrtc which uses JS code on the frontend side to pass audio to the Python backend server. There’s also a JS hack through streamlit-bokeh-events.

2 Likes