Handling multiple Inputs leads to Error: streamlit.errors.DuplicateWidgetID: There are multiple widgets with the same `key=

Hey y’all,

I want to create a streamlit app as UI for valueinputs to then run the program with a button. This app is a podcast_generator, which takes user Input to transform it into an agent-debate, which then is transformed to a .mp3.
Im running this app locally.

The problem is, no matter what I tried, any kind of this error occurs:

Error Message

`streamlit.errors.DuplicateWidgetID: There are multiple widgets with the same `key='1'`.
To fix this, please make sure that the `key` argument is unique for each
widget you create.`

Versions:
Streamlit, version 1.37.0
Python 3.10.14

I understand the need for unique keys, but there must be something missing. I saw the session_state mentioned alot in this case, but as I understood this should not be the case for simple inputs.

In the following I pasted my streamlit code. When I hardcoded the values, the skript ran fine, so it must be something, that I missed.

import streamlit as st
import os
from functions.skriptgen import log2skript
from functions.text2speech import elevenlabs_tts
from prompts import generate_message1, generate_message2, generate_modmessage, generate_outline, generate_managermessage

data = [
    {"name": "Adam", "voice_id": "pNInz6obpgDQGcFmaJgB"},
    {"name": "Charlie", "voice_id": "IKne3meq5aSn9XLyUdCD"},
    {"name": "Clyde", "voice_id": "2EiwWnXFnvU5JabPnv8n"},
    {"name": "Dorothy", "voice_id": "ThT5KcBeYPX3keUQqHPh"},
    {"name": "Freya", "voice_id": "jsCqWAovK2LkecY7zXl4"},
    {"name": "Gigi", "voice_id": "jBpfuIE2acCO8z3wKNLl"},
    {"name": "Harry", "voice_id": "SOYHLrjzK2X1ezoPC6cr"},
    {"name": "James", "voice_id": "ZQe5CZNOzWyzPSCn5a3c"},
    {"name": "Lily", "voice_id": "pFZP5JQG7iQjIQuC4Bku"},
    {"name": "Rachel", "voice_id": "21m00Tcm4TlvDq8ikWAM"}
]
voice_dict = {item["name"]: item["voice_id"] for item in data}

def get_voice_id(name):
    return voice_dict.get(name)

OPENAI_API_KEY = st.text_input("OpenAI-Key", "ExampleKey", key="1")
model = "gpt-4o-mini"
XI_API_KEY = st.text_input("Elevenlabs-Key", "ExampleKey", key="2")
SERPER_API_KEY = st.text_input("Serper-Key", "ExampleKey", key="3")

topic = st.text_input("Topic", "AI in Education", key="4")

name1 = st.text_input("name1", "Hannah", key="5")
gender1 = st.text_input("Pronouns", "She/Her", key="6")
expertise1 = st.text_input("Expertise in", "AI", key="7")
role1 = "Affirmative"

name2 = st.text_input("name2", "Bob", key="8")
gender2 = st.text_input("gender2", "He/Him", key="9")
expertise2 = st.text_input("Expertise in", "Education", key="10")
role2 = "Negative"

moderator_voice = st.text_input("Moderator Voice", "Adam", key="11")
expert1_voice = st.text_input("Moderator Voice", "Charlie", key="12")
expert2_voice = st.text_input("Moderator Voice", "Dorothy", key="13")

if st.button("🚀 Generate Podcast", key="14"):
    os.environ[OPENAI_API_KEY] = OPENAI_API_KEY
    print(os.environ[OPENAI_API_KEY])
    os.environ[XI_API_KEY] = XI_API_KEY
    print(os.environ[XI_API_KEY])
    os.environ[SERPER_API_KEY] = SERPER_API_KEY
    print(os.environ[SERPER_API_KEY])
    moderator_voice_id = get_voice_id(moderator_voice)
    print(moderator_voice, moderator_voice_id)
    expert1_voice_id = get_voice_id(expert1_voice)
    print(expert1_voice, expert1_voice_id)
    expert2_voice_id = get_voice_id(expert2_voice)
    print(expert2_voice, expert2_voice_id)
    message1 = generate_message1(name1, gender2, expertise1, role1, name2, topic)
    message2 = generate_message2(name2, gender1, expertise2, role2, name1, topic)
    modmessage = generate_modmessage(name1, expertise1, name2, expertise2, topic)
    outline = generate_outline(name1, role1, name2, role2)
    
    managermessage = generate_managermessage(outline, name1, expertise1, name2, expertise2, topic)
    from debate2podcast0_8 import run_debate
    project_directory = run_debate(
        name1=name1,
        message1=message1,
        name2=name2,
        message2=message2,
        modmessage=modmessage,
        managermessage=managermessage,
        expertise1=expertise1,
        expertise2=expertise2,
        model=model,
        role1= role1,
        role2=role2,
        topic=topic
    )
    log2skript(project_directory=project_directory)

    elevenlabs_tts(moderator_voice_id=moderator_voice_id, 
                   expert1_voice_id=expert1_voice_id, 
                   expert2_voice_id=expert2_voice_id, 
                   name1=name1, 
                   name2=name2,
                   project_directory=project_directory)

I assume somehow the app runs twice, therefore the key-value is already taken, but I couldn’t figure out why and how to avoid it.

I am really thankful for any pointing in the right direction!

Many Thanks and have a nice Weekend!
Johannes

Hey,
it’s not suppose to do this, and i confirm, no need to session_state here.
Have you try without the OPENAI_API_KEY = st.text_input("OpenAI-Key", "ExampleKey", key="1"), just to check if the other widget are in duplicate too ? / see if only the text input working correctly. Check if it’s working (the text input) by commenting the bottom. Check the function “from debate2podcast0_8 import run_debate”
You could try to imbricate all the st.text_input in a st.form st.form_submit_button - Streamlit Docs

1 Like

Hey, thank you for your advice, I slept over it and figured out, that I called the streamlit app in another module, this caused a rerun of the app.

A little facepalm from my side!
Thank you for answerig me :slight_smile:

This thread will be closed
cheers

1 Like

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