venkatcharan commited on
Commit
8491e8c
·
verified ·
1 Parent(s): e62af2a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -44
app.py CHANGED
@@ -16,47 +16,31 @@ prompt=ChatPromptTemplate.from_messages([
16
  MessagesPlaceholder(variable_name="history"),
17
  ("human","{input}")])
18
 
19
-
20
- memory=ConversationBufferWindowMemory(k=3,return_messages=True)
21
-
22
- chain=(RunnablePassthrough.assign(history=RunnableLambda(memory.load_memory_variables)|
23
- itemgetter("history"))|prompt|model)
24
-
25
- # Streamlit interface
26
- st.title("chat bot")
27
- st.write("Enter your input text:")
28
-
29
-
30
- def end_conv():
31
- st.write("Conversation ended.")
32
-
33
-
34
- # Initialize session state for conversation history if not already done
35
-
36
-
37
- # User input
38
- user_input = st.text_area("Input Text")
39
-
40
- # Generate and display the response
41
- if st.button("Generate Response"):
42
- # Load current conversation history
43
- history = memory.load_memory_variables({})['history']
44
-
45
- # Invoke the chain to get the response
46
- res = chain.invoke({"input": user_input})
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)