St.rerun() failing with RerunData(page_script_hash=

:rotating_light: Before clicking “Create Topic”, please make sure your post includes the following information (otherwise, the post will be locked). :rotating_light:

  1. Are you running your app locally or is it deployed? local
  2. If your app is deployed: n/a
  3. Share the link to your app’s public GitHub repository : code inline
  4. Share the full text of the error message (not a screenshot).
    RerunData(page_script_hash=‘7da9a8695239feb8134e3026377ef933’)
  5. Share the Streamlit and Python versions: Python 3.10.12 and Streamlit, version 1.28.1

Playing around with voice recognition enabling menu selection … to do this I need to rerun the app if a valid selection is made … when I hit the st.rerun() statement the app fails …

To get the error first say “hey streamlit” and then, when prompted, say “go to option one” … you’ll need to make sure your mic is working properly …

Here’s a snippet of the code demonstrating the error …

import os
import streamlit as st
import speech_recognition as sr
from gtts import gTTS
from thefuzz import fuzz

def initialise():

if "smart_assist_mode" not in st.session_state:
    st.session_state.smart_assist_mode = False

if "smart_assistant_selection" not in st.session_state:
    st.session_state.smart_assistant_selection = False

def remove_file(file_name):

if os.path.exists(file_name):
    os.remove(file_name)
else:
    st.write("File not found.")

def read_out_msg(the_msg):

st.write(the_msg)

if st.session_state.smart_assist_mode:
    tts = gTTS(the_msg, lang='en', tld='com.au')
    tts.save("the_msg.mp3")
    os.system("mpg321 the_msg.mp3") 
    remove_file("the_msg.mp3")

def requested_selection(selection):

match selection:
     case "option1":
         read_out_msg("first choice")
     case "option2":
         read_out_msg("second choice")
     case other:
         read_out_msg("unknown select ",selection)

def smart_assistance():

st.write("into smart_assistance")

menu_options = {
    "option1": ["option one","main choice","numero uno"],
    "option2": ["option two","second in line", "the next choice"]
}
menu_options_list = list(menu_options.keys())

recognizer = sr.Recognizer()

with sr.Microphone() as source:
    st.write("What can I help you with ...")
    audio = recognizer.listen(source)

try:
    recognized_text = recognizer.recognize_google(audio)
    best_match = None
    best_score = 0
    for option, keywords in menu_options.items():
        for keyword in keywords:
            score = fuzz.ratio(recognized_text, keyword)
            if score > best_score:
                best_match = option
                best_score = score

    st.session_state.smart_assistant_selection=False

    if best_score > 60:
         st.session_state.smart_assistant_selection=True
         st.session_state.smart_assistant_option_selected=menu_options_list.index(best_match)
         st.write("st.session_state.smart_assistant_selection= "+str(st.session_state.smart_assistant_selection))
         st.write("st.session_state.smart_assistant_option_selected= "+str(st.session_state.smart_assistant_option_selected))
         st.write("fails right about here")
         st.rerun()

    st.write("Let's take a closer look. For confirmation, you're asking ==>")
    read_out_msg(recognized_text)

except sr.UnknownValueError as e:
    st.write(f"Could not understand audio; {e}")
    smart_assistance()
except BaseException as e:
    st.write(f"unknown exception; {e}")

def launch_smart_assistance():

recognizer = sr.Recognizer()

with sr.Microphone() as source:
    try:
        while True:
            try:
                st.write("into launch_smart_assistance")
                recognizer.adjust_for_ambient_noise(source)
                audio = recognizer.listen(source)
                recognized_text = recognizer.recognize_google(audio)
                if fuzz.ratio(recognized_text, "hey streamlit") >= 70:
                    break
            except sr.WaitTimeoutError:
                pass
            except sr.UnknownValueError:
                pass
            except BaseException as e:
                st.write(f"unknown exception; {e}")
                st.stop()
          
        smart_assistance() 

    except sr.RequestError as e:
        st.write(f"Could not request results; {e}")
    except BaseException as e:
        st.write(f"Unknown exception in voice recognition; {e}")

st.write(“let’s get started”)

initialise()

if st.session_state.smart_assistant_selection:
st.write(“smart assistant selection”)
st.write(st.session_state.smart_assistant_option_selected)

launch_smart_assistance()

hi @Adam_Crosby
Instead of using st.rerun() , you can consider restructuring your app to use the st.button or st.sidebar.button components to trigger actions. Here’s a revised version of your code with an example of using a button to trigger the rerun:

import os
import streamlit as st
import speech_recognition as sr
from gtts import gTTS
from thefuzz import fuzz

def initialise():
    if "smart_assist_mode" not in st.session_state:
        st.session_state.smart_assist_mode = False

    if "smart_assistant_selection" not in st.session_state:
        st.session_state.smart_assistant_selection = False

def remove_file(file_name):
    if os.path.exists(file_name):
        os.remove(file_name)
    else:
        st.write("File not found.")

def read_out_msg(the_msg):
    st.write(the_msg)

    if st.session_state.smart_assist_mode:
        tts = gTTS(the_msg, lang='en', tld='com.au')
        tts.save("the_msg.mp3")
        os.system("mpg321 the_msg.mp3")
        remove_file("the_msg.mp3")

def requested_selection(selection):
    match selection:
        case "option1":
            read_out_msg("first choice")
        case "option2":
            read_out_msg("second choice")
        case other:
            read_out_msg("unknown select ", selection)

def smart_assistance():
    st.write("into smart_assistance")

    menu_options = {
        "option1": ["option one", "main choice", "numero uno"],
        "option2": ["option two", "second in line", "the next choice"]
    }
    menu_options_list = list(menu_options.keys())

    recognizer = sr.Recognizer()

    with sr.Microphone() as source:
        st.write("What can I help you with ...")
        audio = recognizer.listen(source)

    try:
        recognized_text = recognizer.recognize_google(audio)
        best_match = None
        best_score = 0
        for option, keywords in menu_options.items():
            for keyword in keywords:
                score = fuzz.ratio(recognized_text, keyword)
                if score > best_score:
                    best_match = option
                    best_score = score

        st.session_state.smart_assistant_selection = False

        if best_score > 60:
            st.session_state.smart_assistant_selection = True
            st.session_state.smart_assistant_option_selected = menu_options_list.index(best_match)
            st.write("st.session_state.smart_assistant_selection= " + str(st.session_state.smart_assistant_selection))
            st.write("st.session_state.smart_assistant_option_selected= " + str(
                st.session_state.smart_assistant_option_selected))
            st.write("fails right about here")
            st.experimental_rerun()

        st.write("Let's take a closer look. For confirmation, you're asking ==>")
        read_out_msg(recognized_text)

    except sr.UnknownValueError as e:
        st.write(f"Could not understand audio; {e}")
        smart_assistance()
    except BaseException as e:
        st.write(f"unknown exception; {e}")

def launch_smart_assistance():
    recognizer = sr.Recognizer()

    with sr.Microphone() as source:
        try:
            while True:
                try:
                    st.write("into launch_smart_assistance")
                    recognizer.adjust_for_ambient_noise(source)
                    audio = recognizer.listen(source)
                    recognized_text = recognizer.recognize_google(audio)
                    if fuzz.ratio(recognized_text, "hey streamlit") >= 70:
                        break
                except sr.WaitTimeoutError:
                    pass
                except sr.UnknownValueError:
                    pass
                except BaseException as e:
                    st.write(f"unknown exception; {e}")
                    st.stop()

            smart_assistance()

        except sr.RequestError as e:
            st.write(f"Could not request results; {e}")
        except BaseException as e:
            st.write(f"Unknown exception in voice recognition; {e}")

st.write("let’s get started")

initialise()

if st.button("Launch Smart Assistant"):
    st.session_state.smart_assistant_selection = True
    launch_smart_assistance()

if st.session_state.smart_assistant_selection:
    st.write("smart assistant selection")
    st.write(st.session_state.smart_assistant_option_selected)

in this st.rerun() is replaced with st.experimental_rerun() , and used a button to launch the smart assistant instead of trying to trigger a rerun within the smart_assistance() function.

Hi … thanks for the suggestion…I want the rerun to be triggered by a verbal request rather than manually, if that’s possible.

I’m wondering whether it’s the depth of functions causing the issue. Testing that now

issue seems to be related to TRY/EXCEPT … remove these and it works

1 Like

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