英文:
**Error with st.session_state attribute**
问题
Here is the translated code portion:
if "generated" not in st.session_state:
st.session_state["generated"] = []
if "past" not in st.session_state:
st.session_state["past"] = []
if "stored_session" not in st.session_state:
st.session_state["stored_session"] = []
def get_text():
input_text = st.text_input("You: ", st.session_state.get("input", "Hello, how are you?"), key="input",
placeholder="Your Notion bot, ask me anything")
return input_text
def new_chat():
save = []
for i in range(len(st.session_state['generated'])-1, -1, -1):
save.append("User:" + st.session_state["past"][i])
save.append("Bot:" + st.session_state["generated"][i])
st.session_state["stored_session"].append(save)
st.session_state["generated"] = []
st.session_state["past"] = []
st.session_state["input"] = ""
st.sidebar.button("New Chat", on_click=new_chat)
user_input = get_text()
if user_input:
result = chain({"question": user_input})
output = f"Answer: {result['answer']}"
st.session_state["past"].append(user_input)
st.session_state["generated"].append(output)
if st.session_state["generated"]:
for i in range(len(st.session_state["generated"]) - 1, -1, -1):
message(st.session_state["past"][i], is_user=True, key=str(i) + "_user")
message(st.session_state["generated"][i], key=str(i))
Regarding the error message you're encountering, it seems like the issue is related to the use of st.session_state.past.append(user_input)
and st.session_state.generated.append(output)
in your code. You should access the session_state dictionary using square brackets, like st.session_state["past"]
and st.session_state["generated"]
, as shown in the translated code above. Make sure to update your code accordingly to fix this issue.
英文:
I am new to python and streamlit and I am creating a bot to answer based on a vectorstore and i am using Langchain too. This is my part for the streamlit UI code and although I initialize the session state, I'm having an error related to it.
if "generated" not in st.session_state:
st.session_state["generated"] = []
if "past" not in st.session_state:
st.session_state["past"] = []
if "stored_session" not in st.session_state:
st.session_state["stored_session"] = []
def get_text():
input_text = st.text_input("You: ", st.session_state.get("input", "Hello, how are you?"), key="input",
placeholder="Your Notion bot, ask me anything")
return input_text
def new_chat()
save = []
for i in range(len(st.session_state['generated'])-1, -1, -1):
save.append("User:" + st.session_state["past"][i])
save.append("Bot:" + st.session_state["generated"][i])
st.session_state["stored_session"].append(save)
st.session_state["generated"] = []
st.session_state["past"] = []
st.session_state["input"] = ""
st.sidebar.button("New Chat", on_click=new_chat)
user_input = get_text()
if user_input:
result = chain({"question": user_input})
output = f"Answer: {result['answer']}"
#output = f"Answer: {result['answer']}\nSources: {result['sources']}"
st.session_state.past.append(user_input)
st.session_state.generated.append(output)
if st.session_state["generated"]:
for i in range(len(st.session_state["generated"]) - 1, -1, -1):
message(st.session_state["past"][i], is_user=True, key=str(i) + "_user")
message(st.session_state["generated"][i], key=str(i))
The error i get is :
AttributeError: st.session_state has no attribute "past". Did you forget to initialize it?
I am running the script in the terminal using streamlit run filename.py
What could be the problem?
答案1
得分: 1
"Just run your python code by
streamlit run xxx.py
```"
<details>
<summary>英文:</summary>
just run your python code by
streamlit run xxx.py
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论