Spaces:
Sleeping
Sleeping
palitrajarshi
commited on
Commit
•
0f66d47
1
Parent(s):
81650e9
Upload ChatterBot.py
Browse files- pages/ChatterBot.py +63 -0
pages/ChatterBot.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from streamlit_chat import message
|
3 |
+
from langchain import OpenAI
|
4 |
+
from langchain.chains import LLMChain, ConversationChain
|
5 |
+
from langchain.chains.conversation.memory import (ConversationBufferMemory, ConversationSummaryMemory, ConversationBufferWindowMemory)
|
6 |
+
|
7 |
+
if 'conversation' not in st.session_state:
|
8 |
+
st.session_state['conversation'] =None
|
9 |
+
if 'messages' not in st.session_state:
|
10 |
+
st.session_state['messages'] =[]
|
11 |
+
if 'API_Key' not in st.session_state:
|
12 |
+
st.session_state['API_Key'] =''
|
13 |
+
|
14 |
+
# Setting page title and header
|
15 |
+
st.set_page_config(page_title="Chat GPT Clone", page_icon=":robot_face:")
|
16 |
+
st.markdown("<h1 style='text-align: center;'>How can I assist you? </h1>", unsafe_allow_html=True)
|
17 |
+
|
18 |
+
|
19 |
+
st.sidebar.title("😎")
|
20 |
+
st.session_state['API_Key']= st.sidebar.text_input("What's your API key?",type="password")
|
21 |
+
summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
|
22 |
+
if summarise_button:
|
23 |
+
summarise_placeholder = st.sidebar.write("Nice chatting with you my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
|
24 |
+
#summarise_placeholder.write("Nice chatting with you my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
|
25 |
+
|
26 |
+
|
27 |
+
#import os
|
28 |
+
#os.environ["OPENAI_API_KEY"] = "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"
|
29 |
+
|
30 |
+
|
31 |
+
def getresponse(userInput, api_key):
|
32 |
+
|
33 |
+
if st.session_state['conversation'] is None:
|
34 |
+
|
35 |
+
llm = OpenAI(temperature=0, openai_api_key=api_key, model_name='text-davinci-003')
|
36 |
+
#we can also use 'gpt-3.5-turbo'
|
37 |
+
st.session_state['conversation'] = ConversationChain(llm=llm, verbose=True, memory=ConversationBufferMemory())
|
38 |
+
|
39 |
+
response = st.session_state['conversation'].predict(input=userInput)
|
40 |
+
print(st.session_state['conversation'].memory.buffer)
|
41 |
+
|
42 |
+
return response
|
43 |
+
|
44 |
+
response_container = st.container()
|
45 |
+
# Here we will have a container for user input text box
|
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("Your question goes here:", key='input', height=100)
|
51 |
+
submit_button = st.form_submit_button(label='Send')
|
52 |
+
|
53 |
+
if submit_button:
|
54 |
+
st.session_state['messages'].append(user_input)
|
55 |
+
model_response=getresponse(user_input, st.session_state['API_Key'])
|
56 |
+
st.session_state['messages'].append(model_response)
|
57 |
+
|
58 |
+
with response_container:
|
59 |
+
for i in range(len(st.session_state['messages'])):
|
60 |
+
if (i % 2) == 0:
|
61 |
+
message(st.session_state['messages'][i], is_user=True, key=str(i) + '_user')
|
62 |
+
else:
|
63 |
+
message(st.session_state['messages'][i], key=str(i) + '_AI')
|