Joanna30 commited on
Commit
b4152d6
·
verified ·
1 Parent(s): d34504c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -42
app.py CHANGED
@@ -1,62 +1,57 @@
1
  import streamlit as st
2
  from streamlit_chat import message
3
- from langchain.llms import Gemini
4
  from langchain.chains import ConversationChain
5
- from langchain.chains.conversation.memory import ConversationBufferMemory
 
6
 
7
- # Initialize session state
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
 
13
- # UI setup
14
  st.set_page_config(page_title="ChatGPT Clone", page_icon=":robot_face:")
15
- st.markdown("<h1 style='text-align: center;'>Your AI Assistant</h1>", unsafe_allow_html=True)
16
 
17
- # Sidebar for API key (This might need adjustments for Gemini)
18
- st.sidebar.title("API Key")
19
- api_key = st.sidebar.text_input("Enter your Gemini API key:", type="password")
 
 
 
20
 
21
- # Summarization button
22
- summarise_button = st.sidebar.button("Summarize the conversation", key="summarise")
23
- if summarise_button and st.session_state['conversation'] is not None:
24
- st.sidebar.write("Conversation Summary:\n\n" + st.session_state['conversation'].memory.buffer)
25
-
26
- # Get response function (Modified for Gemini)
27
  def get_response(user_input, api_key):
28
- if api_key:
29
- if st.session_state['conversation'] is None:
30
- llm = Gemini(
31
- model="gemini-flash", # Specify the Gemini model
32
- temperature=0,
33
- openai_api_key=api_key # Adapt this if Gemini uses a different key
34
- )
35
- st.session_state['conversation'] = ConversationChain(llm=llm, verbose=True, memory=ConversationBufferMemory())
36
- try:
37
- response = st.session_state['conversation'].predict(input=user_input)
38
- return response
39
- except Exception as e:
40
- return f"Error: {e}"
41
- else:
42
- return "Enter your API key in the sidebar."
43
-
44
- # Chat interface (unchanged)
45
  response_container = st.container()
46
  container = st.container()
47
-
48
  with container:
49
  with st.form(key='my_form', clear_on_submit=True):
50
- user_input = st.text_area("You:", key='input', height=100)
51
  submit_button = st.form_submit_button(label='Send')
52
-
53
- if submit_button and user_input:
54
- model_response = get_response(user_input, api_key)
55
-
56
- st.session_state['messages'].append({"message": user_input, "is_user": True})
57
- st.session_state['messages'].append({"message": model_response, "is_user": False})
58
 
59
  with response_container:
60
- if st.session_state['messages']:
61
- for msg in st.session_state['messages']:
62
- message(msg["message"], is_user=msg["is_user"])
 
 
 
1
  import streamlit as st
2
  from streamlit_chat import message
3
+ from langchain.llms import GooglePalm
4
  from langchain.chains import ConversationChain
5
+ from langchain.chains.conversation.memory import ConversationSummaryMemory
6
+ import os # Import the os module
7
 
8
+ # Initialize session state variables
9
  if 'conversation' not in st.session_state:
10
  st.session_state['conversation'] = None
11
  if 'messages' not in st.session_state:
12
  st.session_state['messages'] = []
13
+ if 'API_Key' not in st.session_state:
14
+ st.session_state['API_Key'] = ''
15
 
16
+ # Streamlit UI
17
  st.set_page_config(page_title="ChatGPT Clone", page_icon=":robot_face:")
18
+ st.markdown("<h1 style='text-align: center;'>How can I assist you?</h1>", unsafe_allow_html=True)
19
 
20
+ # Sidebar
21
+ st.sidebar.title(":D")
22
+ st.session_state['API_Key'] = st.sidebar.text_input("What's your API key?", type="password") # Accepts any API key here
23
+ summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
24
+ if summarise_button:
25
+ summarise_placeholder = st.sidebar.write("Nice chatting with you my friend <3️:\n\n" + st.session_state['conversation'].memory.buffer)
26
 
27
+ # Function to get response from Google Palm
 
 
 
 
 
28
  def get_response(user_input, api_key):
29
+ if st.session_state['conversation'] is None:
30
+ google_api_key = os.environ.get('GOOGLE_API_KEY') # Get API key from environment variable
31
+ llm = GooglePalm(google_api_key=google_api_key, temperature=0)
32
+ st.session_state['conversation'] = ConversationChain(
33
+ llm=llm,
34
+ verbose=True,
35
+ memory=ConversationSummaryMemory(llm=llm)
36
+ )
37
+ response = st.session_state['conversation'].predict(input=user_input)
38
+ return response
39
+
40
+ # Chat UI
 
 
 
 
 
41
  response_container = st.container()
42
  container = st.container()
 
43
  with container:
44
  with st.form(key='my_form', clear_on_submit=True):
45
+ user_input = st.text_area("Your question goes here:", key='input', height=100)
46
  submit_button = st.form_submit_button(label='Send')
47
+ if submit_button:
48
+ st.session_state['messages'].append(user_input)
49
+ model_response = get_response(user_input, st.session_state['API_Key'])
50
+ st.session_state['messages'].append(model_response)
 
 
51
 
52
  with response_container:
53
+ for i in range(len(st.session_state['messages'])):
54
+ if (i % 2) == 0:
55
+ message(st.session_state['messages'][i], is_user=True, key=str(i) + '_user')
56
+ else:
57
+ message(st.session_state['messages'][i], key=str(i) + '_AI')