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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -75
app.py CHANGED
@@ -1,84 +1,31 @@
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()
 
1
  import streamlit as st
 
 
 
 
 
 
 
2
 
3
+ def page1():
4
+ st.write("This is Page 1!")
5
 
6
+ def page2():
7
+ st.write("This is Page 2!")
 
 
 
 
 
 
8
 
9
+ # Create a Streamlit app
10
+ app = st.StreamlitApp()
 
11
 
12
+ # Define the main app logic
13
+ def main():
14
+ st.title("Welcome to My App")
15
 
16
+ # Create two buttons
17
+ button1 = st.button("Go to Page 1")
18
+ button2 = st.button("Go to Page 2")
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ # Use Streamlit's routing feature to navigate between pages
21
+ if button1:
22
+ app.page("page1")
23
+ elif button2:
24
+ app.page("page2")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ # Register the pages with Streamlit
27
+ app.page("page1", page1)
28
+ app.page("page2", page2)
29
 
30
+ # Run the main app logic
31
+ main()