How to pass event data to form input value and to session_state

I am new to Streamlit. Am using the code below to get data from snowflake replicate API and everything is working fine


import streamlit as st
import numpy as np
import pandas as pd
import replicate
import os



st.write("# Welcome to Streamlit! ??")


#Set the REPLICATE_API_TOKEN environment variable
os.environ["REPLICATE_API_TOKEN"] = "my token goes here"

for event in replicate.stream(
    "snowflake/snowflake-arctic-instruct",
    input={
        "top_k": 50,
        "top_p": 0.9,
        "prompt": "Define food",
        "temperature": 0.2,
        "max_new_tokens": 512,
        "min_new_tokens": 0,
        "stop_sequences": "<|im_end|>",
        "prompt_template": "<|im_start|>system\nYou're a helpful assistant<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n\n<|im_start|>assistant\n",
        "presence_penalty": 1.15,
        "frequency_penalty": 0.2
    },
):
    
    eventx = event
    st.write(event)
    print(str(event), end="")

Here is my issue. I need to get event result data to be passed to form input value and to session_state. I have tried this code below


 if event:
        st.text_input("Your namex", key="namex", value=eventx)
    
# You can access the data at any point with:
st.session_state.eventx

But it throw error

**DuplicateWidgetID**: There are multiple widgets with the same `key='namex'`.

To fix this, please make sure that the `key` argument is unique for each widget you create.

Traceback:

File “C:\python_app2\pages\page3.py”, line 39, in
st.text_input(“Your namex”, key=“namex”, value=event)

Here is the full code


import streamlit as st
import numpy as np
import pandas as pd
import replicate
import os



st.write("# Welcome to Streamlit! ??")


#Set the REPLICATE_API_TOKEN environment variable
os.environ["REPLICATE_API_TOKEN"] = "my token goes here"

for event in replicate.stream(
    "snowflake/snowflake-arctic-instruct",
    input={
        "top_k": 50,
        "top_p": 0.9,
        "prompt": "Define food",
        "temperature": 0.2,
        "max_new_tokens": 512,
        "min_new_tokens": 0,
        "stop_sequences": "<|im_end|>",
        "prompt_template": "<|im_start|>system\nYou're a helpful assistant<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n\n<|im_start|>assistant\n",
        "presence_penalty": 1.15,
        "frequency_penalty": 0.2
    },
):
    
    eventx = event
    st.write(event)
    print(str(event), end="")

    if event:
        st.text_input("Your namex", key="namex", value=eventx)
    

# You can access the data at any point with:
st.session_state.eventx

Please I need help…

Welcome to the community, @freeboy! We’re excited to have you join us! :tada:

So I believe the error DuplicateWidgetID occurs because the st.text_input widget is being created multiple times within the for loop, and Streamlit expects each widget to have a unique key.

To fix this, try moving the st.text_input outside the loop and update its value using the st.session_state object. Try:

import streamlit as st
import numpy as np
import pandas as pd
import replicate
import os

st.write("# Welcome to Streamlit! 👋")

os.environ["REPLICATE_API_TOKEN"] = "my token goes here"

if "event_result" not in st.session_state:
    st.session_state.event_result = ""

event_input = st.text_input("Your namex", key="namex", value=st.session_state.event_result)

for event in replicate.stream(
    "snowflake/snowflake-arctic-instruct",
    input={
        "top_k": 50,
        "top_p": 0.9,
        "prompt": "Define food",
        "temperature": 0.2,
        "max_new_tokens": 512,
        "min_new_tokens": 0,
        "stop_sequences": "<|im_end|>",
        "prompt_template": "<|im_start|>system\nYou're a helpful assistant<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n\n<|im_start|>assistant\n",
        "presence_penalty": 1.15,
        "frequency_penalty": 0.2
    },
):
    st.write(event)
    print(str(event), end="")

    # Update the session_state with the event result
    st.session_state.event_result = str(event)

# You can access the data at any point with:
# st.session_state.event_result

Let me know if that solves the issue.

Best,
Charly

@Charly_Wargnier Thanks for getting back to me. When I run your code, there was no error but the session and form input values was empty.

The code Is shared was meant to address the DuplicateWidgetID issue.

If that’s resolved but the form inputs are still coming up empty, it appears we might have another issue to look into! :smile:

Is the app running smoothly on your local setup?

Thanks,
Charly

Your solutions works. Thanks

Glad to hear!

Happy Streamlit-ing! :balloon:

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