Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,57 @@
|
|
1 |
import streamlit as st
|
2 |
from streamlit_chat import message
|
3 |
-
from langchain.llms import
|
4 |
from langchain.chains import ConversationChain
|
5 |
-
from langchain.chains.conversation.memory import
|
|
|
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
|
14 |
st.set_page_config(page_title="ChatGPT Clone", page_icon=":robot_face:")
|
15 |
-
st.markdown("<h1 style='text-align: center;'>
|
16 |
|
17 |
-
# Sidebar
|
18 |
-
st.sidebar.title("
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
-
#
|
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
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
)
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
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("
|
51 |
submit_button = st.form_submit_button(label='Send')
|
52 |
-
|
53 |
-
|
54 |
-
model_response = get_response(user_input,
|
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 |
-
|
61 |
-
|
62 |
-
message(
|
|
|
|
|
|
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')
|