Spaces:
Sleeping
Sleeping
venkat charan
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -28,31 +28,36 @@ st.write("Enter your input text:")
|
|
28 |
|
29 |
|
30 |
def end_conv():
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
user_input = st.text_area("Input Text")
|
34 |
-
|
35 |
-
# Generate and display the response
|
36 |
if st.button("Generate Response"):
|
37 |
-
# Load current conversation history
|
38 |
history = memory.load_memory_variables({})['history']
|
39 |
|
40 |
-
|
41 |
res = chain.invoke({"input": user_input})
|
42 |
-
response_content = res.content
|
43 |
st.write("Generated Response:")
|
44 |
st.write(response_content)
|
45 |
|
46 |
-
# Save the context in memory
|
47 |
memory.save_context({"input": user_input}, {"output": response_content})
|
|
|
48 |
|
49 |
-
# Display the conversation history
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
|
54 |
-
#
|
55 |
if st.button("End Conversation"):
|
56 |
-
end_conv()
|
57 |
-
|
58 |
-
|
|
|
28 |
|
29 |
|
30 |
def end_conv():
|
31 |
+
st.write("Conversation ended.")
|
32 |
+
st.session_state.conversation_history = []
|
33 |
+
|
34 |
+
# Initialize session state for conversation history if not already done
|
35 |
+
if 'conversation_history' not in st.session_state:
|
36 |
+
st.session_state.conversation_history = []
|
37 |
+
|
38 |
+
# User input
|
39 |
user_input = st.text_area("Input Text")
|
40 |
+
|
41 |
+
# Generate and display the response
|
42 |
if st.button("Generate Response"):
|
43 |
+
# Load current conversation history
|
44 |
history = memory.load_memory_variables({})['history']
|
45 |
|
46 |
+
# Invoke the chain to get the response
|
47 |
res = chain.invoke({"input": user_input})
|
48 |
+
response_content = res.get("content", "No content generated.")
|
49 |
st.write("Generated Response:")
|
50 |
st.write(response_content)
|
51 |
|
52 |
+
# Save the context in memory and session state
|
53 |
memory.save_context({"input": user_input}, {"output": response_content})
|
54 |
+
st.session_state.conversation_history.extend([{"role": "human", "content": user_input}, {"role": "assistant", "content": response_content}])
|
55 |
|
56 |
+
# Display the updated conversation history
|
57 |
+
#st.write("Conversation History:")
|
58 |
+
#for msg in st.session_state.conversation_history:
|
59 |
+
# st.write(f"{msg['role']}: {msg['content']}")
|
60 |
|
61 |
+
# End conversation button
|
62 |
if st.button("End Conversation"):
|
63 |
+
end_conv()
|
|
|
|