Portaudio error

Hi, I have a voice assistant in my project, It works fine in my local system while deploying it throws an error, I am new to this, If any help is appreciated.

Pyaudio error
fatal error: portaudio

screenshot of my error

Hello @Jaiharish-passion07

I think we had this issue before here: PortAudio Library Not Found

Depending on where you are deploying there should be an intermediate step to instapp apt packages, this is where you’d want to install PortAudio. Hope that clears some things up!

EDIT: ah wait this is on Streamlit Share, let me check real quick.

EDIT2: so I “think” I got this working using this. Did not fully test but it doesn’t err anymore at the install.

Add a packages.txt file at the root of your project with the following contents:

build-essential
libasound-dev
portaudio19-dev
python3-pyaudio

You can check this repo for a working example.

Have a nice day,
Fanilo

Thankyou @andfanilo it installed portaudio,but other pbm is I using microphone for my app,it does not configure,it shows this error


Is there possible can I use my microphone with streamlit app?
If Iam wrong let me know it,waiting for your reply…

Hey @Jaiharish-passion07,

In Streamlit, any Python code is going to be run server-side, which means when you are using device_index = pa.get_default_input_device(), it is going to try and get the audio device from the service in the Cloud instead of the device from your browser.

You need to implement code to read audio from the client-side and send it to the Python part for your purpose, which translates in writing JS code…One would normally build a dedicated Streamlit Component for this, integrating for example React-Mic. You may also be able to workaround this using the streamlit-bokeh-events package to pass JS code and interact with the MediaRecorder API as in this example:


All things aside, a Streamlit Audio recorder is a popular demand so if you or anyone can come to a first solution it would be awesomlit’

Fanilo

1 Like

Hi @andfanilo, :blush:I really thanks for your help and it works fine well in Google Chrome not other browsers.
Is there a way to use python speech recognition library for listening voice input,while running in streamlit server using python alone.(sr.withMicrophone() as source:
source.listen())
If there is a way let me know it.
And once again thank you for your help :clap:

Any update on the solution for this problem?
I am also facing the same issue

I was able to use streamlit-webrtc to record and play back audio on the client side.

Hi @michen00,
Can you please share how to only record audio using this library?

Here’s something you could try:

import streamlit as st
from streamlit_webrtc import webrtc_streamer, WebRtcMode
from pydub import AudioSegment

with st.container():
    sample = st.session_state.audio_buffer
    audio_available = sample != AudioSegment.empty()
    if audio_available:
        st.audio(
            sample.export(format="wav", codec="pcm_s16le", bitrate="128k").read()
        )
    else:
        with (record_section := st.container()):
            webrtc_ctx = webrtc_streamer(
                key="sendonly-audio",
                mode=WebRtcMode.SENDONLY,
                audio_receiver_size=1024,
                rtc_configuration={
                    "iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
                },
                media_stream_constraints={"audio": True, "video": False},
            )

            with st.spinner(text="recording..."):
                while True:
                    if webrtc_ctx.audio_receiver:
                        try:
                            audio_frames = webrtc_ctx.audio_receiver.get_frames(
                                timeout=3
                            )
                        except queue.Empty:
                            record_section.write("no audio received...")
                        sound_chunk = AudioSegment.empty()
                        try:
                            for audio_frame in audio_frames:
                                sound = AudioSegment(
                                    data=audio_frame.to_ndarray().tobytes(),
                                    sample_width=audio_frame.format.bytes,
                                    frame_rate=audio_frame.sample_rate,
                                    channels=len(audio_frame.layout.channels),
                                )
                                sound_chunk += sound
                            if len(sound_chunk) > 0:
                                session_state.audio_buffer += sound_chunk
                        except UnboundLocalError:
                            # UnboundLocalError when audio_frames is not set
                            record_section.write("no audio detected...")
                    else:
                        break

This code makes use of st.session_state to track the recording and st.audio and pydub’s AudioSegment for playback. I adapted it from my app (repo), but I’m sure it was directly inspired by something @whitphx wrote, possibly in the discussions section of the streamlit-webrtc repo.

[We are getting off topic here as none of this is directly related to portaudio, but maybe a moderator will help us reorganize.]

Thank you @michen00 for your help!

1 Like

Hi, I was facing a simiar problem while converting text-to-speech on Client side. I tried your code but it throws the error:

AttributeError: st.session_state has no attribute “audio_buffer”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs

Any help on the same would really be great!

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