Spaces:
Sleeping
Sleeping
Commit
·
f2a32d8
1
Parent(s):
da96833
Update project files
Browse files- app.py +62 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from streamlit_chat import message
|
| 3 |
+
from langchain.llms import OpenAI
|
| 4 |
+
from langchain.chains import ConversationChain
|
| 5 |
+
from langchain.chains.conversation.memory import (
|
| 6 |
+
ConversationBufferMemory,
|
| 7 |
+
ConversationSummaryMemory,
|
| 8 |
+
ConversationBufferWindowMemory
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
if 'conversation' not in st.session_state:
|
| 12 |
+
st.session_state['conversation'] = None
|
| 13 |
+
if 'message' not in st.session_state:
|
| 14 |
+
st.session_state['message'] = []
|
| 15 |
+
if 'API_Key' not in st.session_state:
|
| 16 |
+
st.session_state['API_Key'] = ''
|
| 17 |
+
|
| 18 |
+
# Setting page title and header
|
| 19 |
+
st.set_page_config(page_title="Chat GPT Clone", page_icon=":robot_face:")
|
| 20 |
+
st.markdown("<h1 style='text-align: center;'>I'm ur assistance, how can I help you> </h1>",
|
| 21 |
+
unsafe_allow_html=True)
|
| 22 |
+
|
| 23 |
+
st.sidebar.title("😎")
|
| 24 |
+
st.session_state['API_Key'] = st.sidebar.text_input("What's your API key?", type="password")
|
| 25 |
+
summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
|
| 26 |
+
if summarise_button:
|
| 27 |
+
summarise_placeholder = st.sidebar.write("Nice chatting with you ❤ \n \n"+
|
| 28 |
+
st.session_state['conversation'].memory.buffer)
|
| 29 |
+
# summarise_placeholder.write("Nice chatting with you ❤ \n\n" + st.session_state['conversation'].memory.buffer
|
| 30 |
+
|
| 31 |
+
def getresponse(userInput, api_key):
|
| 32 |
+
|
| 33 |
+
if st.session_state['conversation'] is None:
|
| 34 |
+
llm = OpenAI(temperature=0, openai_api_key=api_key, model_name='gpt-3.5-turbo-instruct')
|
| 35 |
+
st.session_state['conversation'] = ConversationChain(llm=llm, verbose=True,
|
| 36 |
+
memory=ConversationSummaryMemory(llm=llm))
|
| 37 |
+
|
| 38 |
+
response = st.session_state['conversation'].predict(input=userInput)
|
| 39 |
+
print(st.session_state['conversation'].memory.buffer)
|
| 40 |
+
|
| 41 |
+
return response
|
| 42 |
+
|
| 43 |
+
response_container = st.container()
|
| 44 |
+
# here we will have a container for user input text box
|
| 45 |
+
container = st.container()
|
| 46 |
+
|
| 47 |
+
with container:
|
| 48 |
+
with st.form(key='my_form', clear_on_submit=True):
|
| 49 |
+
user_input = st.text_area("Your question goes here:", key='input', height=100)
|
| 50 |
+
submit_button = st.form_submit_button(label='Send')
|
| 51 |
+
|
| 52 |
+
if submit_button:
|
| 53 |
+
st.session_state['message'].append(user_input)
|
| 54 |
+
model_response=getresponse(user_input, st.session_statep['API_Key'])
|
| 55 |
+
st.session_state['message'].append(model_response)
|
| 56 |
+
|
| 57 |
+
with response_container:
|
| 58 |
+
for i in range(len(st.session_state['message'])):
|
| 59 |
+
if(i%2)==0:
|
| 60 |
+
message(st.session_state['message'][i], is_user=True, key=str(i)+'_user')
|
| 61 |
+
else:
|
| 62 |
+
message(st.session_state['message'][i], key=str(i)+'_AI')
|
requirements.txt
ADDED
|
Binary file (156 Bytes). View file
|
|
|