Update app.py
Browse files
app.py
CHANGED
@@ -1,83 +1,84 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from streamlit_chat import message
|
3 |
-
# from langchain.llms import OpenAI #This import has been replaced by the below import please
|
4 |
-
from langchain_openai import OpenAI
|
5 |
-
from langchain.chains import ConversationChain
|
6 |
-
from langchain.chains.conversation.memory import (ConversationBufferMemory,
|
7 |
-
ConversationSummaryMemory,
|
8 |
-
ConversationBufferWindowMemory
|
9 |
-
|
10 |
-
)
|
11 |
-
|
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'] =''
|
18 |
-
|
19 |
-
# Setting page title and header
|
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 |
-
|
24 |
-
st.sidebar.title("😎")
|
25 |
-
|
26 |
-
summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
|
27 |
-
if summarise_button:
|
28 |
-
summarise_placeholder = st.sidebar.write("Nice chatting with you my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
|
29 |
-
#summarise_placeholder.write("Nice chatting with you my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
)
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
container
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
st.session_state['
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from streamlit_chat import message
|
3 |
+
# from langchain.llms import OpenAI #This import has been replaced by the below import please
|
4 |
+
from langchain_openai import OpenAI
|
5 |
+
from langchain.chains import ConversationChain
|
6 |
+
from langchain.chains.conversation.memory import (ConversationBufferMemory,
|
7 |
+
ConversationSummaryMemory,
|
8 |
+
ConversationBufferWindowMemory
|
9 |
+
|
10 |
+
)
|
11 |
+
|
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'] =''
|
18 |
+
|
19 |
+
# Setting page title and header
|
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 |
+
|
24 |
+
st.sidebar.title("😎")
|
25 |
+
groq_api_key=st.sidebar.text_input(label="Groq API Key",type="password")
|
26 |
+
summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
|
27 |
+
if summarise_button:
|
28 |
+
summarise_placeholder = st.sidebar.write("Nice chatting with you my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
|
29 |
+
#summarise_placeholder.write("Nice chatting with you my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
|
30 |
+
|
31 |
+
|
32 |
+
def getresponse(userInput, api_key):
|
33 |
+
|
34 |
+
if st.session_state['conversation'] is None:
|
35 |
+
|
36 |
+
'''llm = OpenAI(
|
37 |
+
temperature=0,
|
38 |
+
openai_api_key=api_key,
|
39 |
+
model_name='gpt-3.5-turbo-instruct' # 'text-davinci-003' model is depreciated now, so we are using the openai's recommended model
|
40 |
+
)'''
|
41 |
+
|
42 |
+
llm=ChatGroq(model="Gemma2-9b-It",groq_api_key=groq_api_key)
|
43 |
+
|
44 |
+
|
45 |
+
st.session_state['conversation'] = ConversationChain(
|
46 |
+
llm=llm,
|
47 |
+
verbose=True,
|
48 |
+
memory=ConversationSummaryMemory(llm=llm)
|
49 |
+
)
|
50 |
+
|
51 |
+
response=st.session_state['conversation'].predict(input=userInput)
|
52 |
+
print(st.session_state['conversation'].memory.buffer)
|
53 |
+
|
54 |
+
|
55 |
+
return response
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
response_container = st.container()
|
60 |
+
# Here we will have a container for user input text box
|
61 |
+
container = st.container()
|
62 |
+
|
63 |
+
|
64 |
+
with container:
|
65 |
+
with st.form(key='my_form', clear_on_submit=True):
|
66 |
+
user_input = st.text_area("Your question goes here:", key='input', height=100)
|
67 |
+
submit_button = st.form_submit_button(label='Send')
|
68 |
+
|
69 |
+
if submit_button:
|
70 |
+
st.session_state['messages'].append(user_input)
|
71 |
+
model_response=getresponse(user_input,st.session_state['API_Key'])
|
72 |
+
st.session_state['messages'].append(model_response)
|
73 |
+
|
74 |
+
|
75 |
+
with response_container:
|
76 |
+
for i in range(len(st.session_state['messages'])):
|
77 |
+
if (i % 2) == 0:
|
78 |
+
message(st.session_state['messages'][i], is_user=True, key=str(i) + '_user')
|
79 |
+
else:
|
80 |
+
message(st.session_state['messages'][i], key=str(i) + '_AI')
|
81 |
+
|
82 |
+
|
83 |
+
|
84 |
+
|