Joanna30 commited on
Commit
71e030e
·
verified ·
1 Parent(s): 38ef369

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -16
app.py CHANGED
@@ -4,41 +4,48 @@ from langchain_google_genai import ChatGoogleGenerativeAI
4
  from langchain.chains import ConversationChain
5
  from langchain.chains.conversation.memory import ConversationBufferMemory, ConversationSummaryMemory
6
 
7
- # Step 2: Initialize session state variables
 
 
 
 
8
  if 'conversation' not in st.session_state:
9
  st.session_state['conversation'] = None
10
  if 'messages' not in st.session_state:
11
  st.session_state['messages'] = []
12
  if 'API_Key' not in st.session_state:
13
- st.session_state['API_Key'] = ''
14
 
15
  # Step 3: Build the Streamlit UI
16
  st.set_page_config(page_title="Chat GPT Clone", page_icon=":robot_face:")
17
  st.markdown("<h1 style='text-align: center;'>How can I assist you? </h1>", unsafe_allow_html=True)
18
 
19
- # Sidebar input for the API key
20
  st.sidebar.title(":D")
21
- st.session_state['API_Key'] = st.sidebar.text_input("Enter your Google API key:", type="password")
22
 
23
  # Summarization button
24
  summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
25
- if summarise_button and st.session_state['conversation'] is not None:
26
- summarise_placeholder = st.sidebar.write("Conversation Summary:\n\n" + st.session_state['conversation'].memory.buffer)
27
 
28
- # Step 5: Define the getresponse function
29
  def getresponse(userInput, api_key):
30
  if st.session_state['conversation'] is None:
31
- # Initialize the Google Generative AI model
32
- chat_model = ChatGoogleGenerativeAI(api_key=api_key, model="google/gemini-flash-1.5")
 
 
 
33
  st.session_state['conversation'] = ConversationChain(
34
- llm=chat_model,
35
  verbose=True,
36
- memory=ConversationSummaryMemory(llm=chat_model)
37
  )
38
  response = st.session_state['conversation'].predict(input=userInput)
39
  return response
40
 
41
- # Step 6: Create the Chat UI
42
  response_container = st.container()
43
  container = st.container()
44
 
@@ -46,7 +53,6 @@ with container:
46
  with st.form(key='my_form', clear_on_submit=True):
47
  user_input = st.text_area("Your question goes here:", key='input', height=100)
48
  submit_button = st.form_submit_button(label='Send')
49
-
50
  if submit_button:
51
  st.session_state['messages'].append(user_input)
52
  model_response = getresponse(user_input, st.session_state['API_Key'])
@@ -59,6 +65,9 @@ with response_container:
59
  else:
60
  message(st.session_state['messages'][i], key=str(i) + '_AI')
61
 
62
- # Step 7: Handle API Key and State
63
- if 'conversation' not in st.session_state:
64
- st.session_state['conversation'] = None
 
 
 
 
4
  from langchain.chains import ConversationChain
5
  from langchain.chains.conversation.memory import ConversationBufferMemory, ConversationSummaryMemory
6
 
7
+ # Step 1: Set up Google API key
8
+ # Retrieve API key from secrets (ensure you set this up in Streamlit secrets)
9
+ google_api_key = st.secrets["google_api_key"]
10
+
11
+ # Step 2: Initialize Session State Variables
12
  if 'conversation' not in st.session_state:
13
  st.session_state['conversation'] = None
14
  if 'messages' not in st.session_state:
15
  st.session_state['messages'] = []
16
  if 'API_Key' not in st.session_state:
17
+ st.session_state['API_Key'] = google_api_key # Use the Google API key from secrets
18
 
19
  # Step 3: Build the Streamlit UI
20
  st.set_page_config(page_title="Chat GPT Clone", page_icon=":robot_face:")
21
  st.markdown("<h1 style='text-align: center;'>How can I assist you? </h1>", unsafe_allow_html=True)
22
 
23
+ # Sidebar for API key input
24
  st.sidebar.title(":D")
25
+ st.session_state['API_Key'] = st.sidebar.text_input("Enter your Google API key", type="password")
26
 
27
  # Summarization button
28
  summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
29
+ if summarise_button:
30
+ summarise_placeholder = st.sidebar.write("Nice chatting with you my friend <3️:\n\n" + str(st.session_state['conversation'].memory.buffer))
31
 
32
+ # Step 4: Define the getresponse function using Google's Gemini
33
  def getresponse(userInput, api_key):
34
  if st.session_state['conversation'] is None:
35
+ # Initialize the Google generative model
36
+ chat = ChatGoogleGenerativeAI(
37
+ model="gemini-1.5-flash",
38
+ google_api_key=api_key
39
+ )
40
  st.session_state['conversation'] = ConversationChain(
41
+ llm=chat,
42
  verbose=True,
43
+ memory=ConversationSummaryMemory(llm=chat)
44
  )
45
  response = st.session_state['conversation'].predict(input=userInput)
46
  return response
47
 
48
+ # Step 5: Creating the Chat UI
49
  response_container = st.container()
50
  container = st.container()
51
 
 
53
  with st.form(key='my_form', clear_on_submit=True):
54
  user_input = st.text_area("Your question goes here:", key='input', height=100)
55
  submit_button = st.form_submit_button(label='Send')
 
56
  if submit_button:
57
  st.session_state['messages'].append(user_input)
58
  model_response = getresponse(user_input, st.session_state['API_Key'])
 
65
  else:
66
  message(st.session_state['messages'][i], key=str(i) + '_AI')
67
 
68
+ # Step 6: Handling State and API Key
69
+ st.session_state['API_Key'] = st.sidebar.text_input("Enter your Google API key", type="password")
70
+
71
+ # Step 7: Running the App
72
+ if __name__ == "__main__":
73
+ st.run()