Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,47 +16,31 @@ prompt=ChatPromptTemplate.from_messages([
|
|
| 16 |
MessagesPlaceholder(variable_name="history"),
|
| 17 |
("human","{input}")])
|
| 18 |
|
| 19 |
-
|
| 20 |
-
memory
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
response_content = res.content
|
| 48 |
-
st.write("Generated Response:")
|
| 49 |
-
st.write(response_content)
|
| 50 |
-
|
| 51 |
-
# Save the context in memory and session state
|
| 52 |
-
memory.save_context({"input": user_input}, {"output": response_content})
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# Display the updated conversation history
|
| 56 |
-
#st.write("Conversation History:")
|
| 57 |
-
#for msg in st.session_state.conversation_history:
|
| 58 |
-
# st.write(f"{msg['role']}: {msg['content']}")
|
| 59 |
-
|
| 60 |
-
# End conversation button
|
| 61 |
-
if st.button("End Conversation"):
|
| 62 |
-
end_conv()
|
|
|
|
| 16 |
MessagesPlaceholder(variable_name="history"),
|
| 17 |
("human","{input}")])
|
| 18 |
|
| 19 |
+
# Initialize memory in session state
|
| 20 |
+
if 'memory' not in st.session_state:
|
| 21 |
+
st.session_state.memory = ConversationBufferWindowMemory(k=10, return_messages=True)
|
| 22 |
+
|
| 23 |
+
# Define the chain
|
| 24 |
+
chain = (RunnablePassthrough.assign(history=RunnableLambda(st.session_state.memory.load_memory_variables) | itemgetter("history")) |
|
| 25 |
+
prompt | model)
|
| 26 |
+
|
| 27 |
+
# Streamlit app
|
| 28 |
+
st.title("Interactive Chatbot")
|
| 29 |
+
|
| 30 |
+
# Initialize session state for user input
|
| 31 |
+
if 'user_input' not in st.session_state:
|
| 32 |
+
st.session_state.user_input = ""
|
| 33 |
+
|
| 34 |
+
# Input from user
|
| 35 |
+
user_input = st.text_area("User: ", st.session_state.user_input, height=100)
|
| 36 |
+
|
| 37 |
+
if st.button("Submit"):
|
| 38 |
+
response = chain.invoke({"input": user_input})
|
| 39 |
+
st.write(f"Assistant: {response.content}")
|
| 40 |
+
st.session_state.memory.save_context({"input": user_input}, {"output": response.content})
|
| 41 |
+
st.session_state.user_input = "" # Clear the input box
|
| 42 |
+
|
| 43 |
+
# Display chat history
|
| 44 |
+
if st.checkbox("Show Chat History"):
|
| 45 |
+
chat_history = st.session_state.memory.load_memory_variables({})
|
| 46 |
+
st.write(chat_history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|