Separating form submit buttons in different columns

Summary

Hello community!

I have a Streamlit application where I use two forms, one in each column. The first form transcribes an audio file using Wav2Vec2, and the second one asks questions about the transcription using ChatGPT. The problem I’m facing is that when I click the “chatgpt_submit_button”, it also triggers the “transcribe_submit_button”. I need them to be separated, so that each column operates independently.

Here’s a simplified version of my code:

Code snippet:

import streamlit as st

st.set_page_config(page_title="Speech-to-Text Transcription & ChatGPT", layout="wide", page_icon="🎤")

c1, c2 = st.columns([1, 1], gap="medium")

with c1:
    st.header("Transcribe your audio")
    with st.form("transcribe_form"):
        f = st.file_uploader("", type=[".wav"])
        transcribe_submit_button = st.form_submit_button("Transcribe")

    if f and transcribe_submit_button is not None:
        # Transcription code here
        pass

with c2:
    st.header("Ask ChatGPT about the transcription")
    with st.form("chatgpt_form"):
        prompt = st.text_area("Enter your query:", "", height=200)
        chatgpt_submit_button = st.form_submit_button("Generate response")

    if chatgpt_submit_button:
        # ChatGPT code here
        pass

I want the “transcribe_submit_button” and “chatgpt_submit_button” to work independently of each other. How can I achieve this in Streamlit? Any help would be greatly appreciated.

Thank you!

@othmanelhoufi

It is your logical conditions that are not correct as the st.form_submit_button returns boolean and is True or False (see st.form_submit_button - Streamlit Docs):
Below an example of what you could change:

# your old code
 if f  and transcribe_submit_button is not None:

should be:

# if no file is uploaded and the form submit button is not pressed
if f is not None and transcribe_submit_button != True:
  # do not run code 
  pass
else:
  # run code

Let me know if that solved your issue.

@tonyhollaar
I don’t know how I missed that! haha
My coded needed some refinements, but yes, condition logic was the issue, thank you so much :smiling_face_with_three_hearts:

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