Joanna30 commited on
Commit
38ef369
·
verified ·
1 Parent(s): 411db86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -48
app.py CHANGED
@@ -1,77 +1,64 @@
1
- import os
2
  import streamlit as st
3
  from streamlit_chat import message
4
  from langchain_google_genai import ChatGoogleGenerativeAI
5
  from langchain.chains import ConversationChain
6
- from langchain.chains.conversation.memory import ConversationSummaryMemory
7
 
8
- # Securely fetch API key from environment variables
9
- API_KEY = os.getenv("GOOGLE_API_KEY")
10
- if not API_KEY:
11
- st.error("API Key not found. Please set the GOOGLE_API_KEY environment variable.")
12
- st.stop()
13
-
14
- # Initialize session state variables
15
  if 'conversation' not in st.session_state:
16
  st.session_state['conversation'] = None
17
  if 'messages' not in st.session_state:
18
  st.session_state['messages'] = []
 
 
 
 
 
 
19
 
20
- # Streamlit UI
21
- st.set_page_config(page_title="ChatGPT Clone with Gemini", page_icon=":robot_face:")
22
- st.markdown("<h1 style='text-align: center;'>ChatGPT Clone with Google Gemini Flash 1.5</h1>", unsafe_allow_html=True)
23
 
24
- # Sidebar API Key input for manual override (if needed)
25
- st.sidebar.title("Settings")
26
- st.sidebar.write("Ensure GOOGLE_API_KEY is set.")
27
- summarise_button = st.sidebar.button("Summarize Conversation")
28
 
29
- # Function to get response from the model
30
- def getresponse(user_input):
31
  if st.session_state['conversation'] is None:
32
- llm = ChatGoogleGenerativeAI(
33
- model="gemini-flash-1.5",
34
- temperature=0,
35
- api_key=API_KEY
36
- )
37
  st.session_state['conversation'] = ConversationChain(
38
- llm=llm,
39
  verbose=True,
40
- memory=ConversationSummaryMemory(llm=llm)
41
  )
42
-
43
- response = st.session_state['conversation'].predict(input=user_input)
44
  return response
45
 
46
- # Display chat messages from history
47
  response_container = st.container()
48
  container = st.container()
 
49
  with container:
50
- with st.form(key='input_form', clear_on_submit=True):
51
- user_input = st.text_area("Your question:", key='input', height=100)
52
  submit_button = st.form_submit_button(label='Send')
53
 
54
- if submit_button and user_input:
55
- # Append user input to messages
56
  st.session_state['messages'].append(user_input)
57
-
58
- # Get model response
59
- model_response = getresponse(user_input)
60
  st.session_state['messages'].append(model_response)
61
 
62
- # Display the chat messages
63
  with response_container:
64
- for i, msg in enumerate(st.session_state['messages']):
65
- if i % 2 == 0:
66
- message(msg, is_user=True, key=f"user_{i}")
67
  else:
68
- message(msg, key=f"bot_{i}")
69
 
70
- # Handle summarization
71
- if summarise_button:
72
- if st.session_state['conversation']:
73
- summary = st.session_state['conversation'].memory.buffer
74
- st.sidebar.write("**Conversation Summary:**")
75
- st.sidebar.write(summary)
76
- else:
77
- st.sidebar.error("No conversation history available for summarization.")
 
 
1
  import streamlit as st
2
  from streamlit_chat import message
3
  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
+
45
  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'])
 
 
53
  st.session_state['messages'].append(model_response)
54
 
 
55
  with response_container:
56
+ for i in range(len(st.session_state['messages'])):
57
+ if (i % 2) == 0:
58
+ message(st.session_state['messages'][i], is_user=True, key=str(i) + '_user')
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