AjayKr09 commited on
Commit
d8bf21a
·
verified ·
1 Parent(s): c8cca73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -13
app.py CHANGED
@@ -1,19 +1,84 @@
1
  import streamlit as st
 
 
 
 
 
 
 
2
 
3
- st.title("AI Chat Bot Dashboard")
 
4
 
5
- st.header('Interactive Chatbot')
 
 
 
 
 
 
 
6
 
7
- st.write('''An interactive chatbot is designed to engage in dynamic, back-and-forth conversations with users.
8
- These chatbots can understand and retain context from previous interactions, making their responses more
9
- relevant and coherent as the conversation progresses. Interactive chatbots often use advanced natural language
10
- processing (NLP) techniques and memory management to provide a more human-like experience. They are commonly used
11
- in applications where ongoing interaction and context awareness are crucial, such as customer support, virtual
12
- assistants, and personalized recommendations.''')
13
 
14
- st.header('Non-Interactive Chatbot')
 
 
15
 
16
- st.write('''A non-interactive chatbot, on the other hand, is designed for more straightforward, single-turn interactions.
17
- These chatbots do not retain context from previous interactions, meaning each user query is treated independently.
18
- Non-interactive chatbots are typically used for simple, transactional tasks where context is not required. They are
19
- easier to develop and deploy and are suitable for scenarios where the interaction is brief and to the point.''')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import os
3
+ from langchain_core.prompts import ChatPromptTemplate
4
+ from langchain_google_genai import ChatGoogleGenerativeAI
5
+ from langchain_core.prompts import MessagesPlaceholder
6
+ from langchain.memory import ConversationBufferWindowMemory
7
+ from operator import itemgetter
8
+ from langchain_core.runnables import RunnableLambda, RunnablePassthrough
9
 
10
+ # Set the API key for Google Generative AI
11
+ os.environ['GOOGLE_API_KEY'] = 'AIzaSyBHPIIk4-BOgXvnQ2_o6c2wTGpY2ByRIDs'
12
 
13
+ # Define the prompt
14
+ prompt = ChatPromptTemplate.from_messages(
15
+ [
16
+ ('system', 'you are a good assistant.'),
17
+ MessagesPlaceholder(variable_name='history'),
18
+ ("human", "{input}")
19
+ ]
20
+ )
21
 
22
+ # Initialize memory in session state
23
+ if 'memory' not in st.session_state:
24
+ st.session_state.memory = ConversationBufferWindowMemory(k=10, return_messages=True)
 
 
 
25
 
26
+ # Define the chain
27
+ chain = (RunnablePassthrough.assign(history=RunnableLambda(st.session_state.memory.load_memory_variables) | itemgetter("history")) |
28
+ prompt | ChatGoogleGenerativeAI(model='gemini-pro', temperature=0, max_output_tokens=500, convert_system_message_to_human=True))
29
 
30
+ # Define the pages
31
+ def home():
32
+ st.title("AI Chat Bot Dashboard")
33
+ st.header('Interactive Chatbot')
34
+ st.write('''An interactive chatbot is designed to engage in dynamic, back-and-forth conversations with users.
35
+ These chatbots can understand and retain context from previous interactions, making their responses more
36
+ relevant and coherent as the conversation progresses. Interactive chatbots often use advanced natural language
37
+ processing (NLP) techniques and memory management to provide a more human-like experience. They are commonly used
38
+ in applications where ongoing interaction and context awareness are crucial, such as customer support, virtual
39
+ assistants, and personalized recommendations.''')
40
+ st.header('Non-Interactive Chatbot')
41
+ st.write('''A non-interactive chatbot, on the other hand, is designed for more straightforward, single-turn interactions.
42
+ These chatbots do not retain context from previous interactions, meaning each user query is treated independently.
43
+ Non-interactive chatbots are typically used for simple, transactional tasks where context is not required. They are
44
+ easier to develop and deploy and are suitable for scenarios where the interaction is brief and to the point.''')
45
+
46
+ def page1():
47
+ st.title("Interactive Chatbot")
48
+ if 'user_input' not in st.session_state:
49
+ st.session_state.user_input = ""
50
+ user_input = st.text_area("User: ", st.session_state.user_input, height=100)
51
+ if st.button("Submit"):
52
+ response = chain.invoke({"input": user_input})
53
+ st.write(f"Assistant: {response.content}")
54
+ st.session_state.memory.save_context({"input": user_input}, {"output": response.content})
55
+ st.session_state.user_input = "" # Clear the input box
56
+ if st.checkbox("Show Chat History"):
57
+ chat_history = st.session_state.memory.load_memory_variables({})
58
+ st.write(chat_history)
59
+
60
+ def page2():
61
+ st.title("Interactive Chatbot")
62
+ if 'user_input' not in st.session_state:
63
+ st.session_state.user_input = ""
64
+ user_input = st.text_area("User: ", st.session_state.user_input, height=100)
65
+ if st.button("Submit"):
66
+ response = chain.invoke({"input": user_input})
67
+ st.write(f"Assistant: {response.content}")
68
+ st.session_state.memory.save_context({"input": user_input}, {"output": response.content})
69
+ st.session_state.user_input = "" # Clear the input box
70
+ if st.checkbox("Show Chat History"):
71
+ chat_history = st.session_state.memory.load_memory_variables({})
72
+ st.write(chat_history)
73
+
74
+ # Sidebar navigation
75
+ st.sidebar.title("Navigation")
76
+ page = st.sidebar.radio("Go to", ("Home", "Page 1", "Page 2"))
77
+
78
+ # Page rendering
79
+ if page == "Home":
80
+ home()
81
+ elif page == "Page 1":
82
+ page1()
83
+ elif page == "Page 2":
84
+ page2()