Spaces:
Sleeping
Sleeping
gpt chat
Browse files- app.py +81 -40
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,41 +1,82 @@
|
|
1 |
-
#Hello! It seems like you want to import the Streamlit library in Python. Streamlit is a powerful open-source framework used for building web applications with interactive data visualizations and machine learning models. To import Streamlit, you'll need to ensure that you have it installed in your Python environment.
|
2 |
-
#Once you have Streamlit installed, you can import it into your Python script using the import statement,
|
3 |
-
|
4 |
import streamlit as st
|
5 |
-
|
6 |
-
|
7 |
-
from langchain.
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
st.
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
st.
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from streamlit_chat import message
|
3 |
+
from langchain import OpenAI
|
4 |
+
from langchain.chains import ConversationChain
|
5 |
+
from langchain.chains.conversation.memory import (ConversationBufferMemory,
|
6 |
+
ConversationSummaryMemory,
|
7 |
+
ConversationBufferWindowMemory
|
8 |
+
|
9 |
+
)
|
10 |
+
|
11 |
+
if 'conversation' not in st.session_state:
|
12 |
+
st.session_state['conversation'] =None
|
13 |
+
if 'messages' not in st.session_state:
|
14 |
+
st.session_state['messages'] =[]
|
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;'>How can I assist you? </h1>", unsafe_allow_html=True)
|
21 |
+
|
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 my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
|
28 |
+
#summarise_placeholder.write("Nice chatting with you my friend ❤️:\n\n"+st.session_state['conversation'].memory.buffer)
|
29 |
+
|
30 |
+
#import os
|
31 |
+
#os.environ["OPENAI_API_KEY"] = "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"
|
32 |
+
|
33 |
+
def getresponse(userInput, api_key):
|
34 |
+
|
35 |
+
if st.session_state['conversation'] is None:
|
36 |
+
|
37 |
+
llm = OpenAI(
|
38 |
+
temperature=0,
|
39 |
+
openai_api_key=api_key,
|
40 |
+
model_name='text-davinci-003' #we can also use 'gpt-3.5-turbo'
|
41 |
+
)
|
42 |
+
|
43 |
+
st.session_state['conversation'] = ConversationChain(
|
44 |
+
llm=llm,
|
45 |
+
verbose=True,
|
46 |
+
memory=ConversationSummaryMemory(llm=llm)
|
47 |
+
)
|
48 |
+
|
49 |
+
response=st.session_state['conversation'].predict(input=userInput)
|
50 |
+
print(st.session_state['conversation'].memory.buffer)
|
51 |
+
|
52 |
+
|
53 |
+
return response
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
response_container = st.container()
|
58 |
+
# Here we will have a container for user input text box
|
59 |
+
container = st.container()
|
60 |
+
|
61 |
+
|
62 |
+
with container:
|
63 |
+
with st.form(key='my_form', clear_on_submit=True):
|
64 |
+
user_input = st.text_area("Your question goes here:", key='input', height=100)
|
65 |
+
submit_button = st.form_submit_button(label='Send')
|
66 |
+
|
67 |
+
if submit_button:
|
68 |
+
st.session_state['messages'].append(user_input)
|
69 |
+
model_response=getresponse(user_input,st.session_state['API_Key'])
|
70 |
+
st.session_state['messages'].append(model_response)
|
71 |
+
|
72 |
+
|
73 |
+
with response_container:
|
74 |
+
for i in range(len(st.session_state['messages'])):
|
75 |
+
if (i % 2) == 0:
|
76 |
+
message(st.session_state['messages'][i], is_user=True, key=str(i) + '_user')
|
77 |
+
else:
|
78 |
+
message(st.session_state['messages'][i], key=str(i) + '_AI')
|
79 |
+
|
80 |
+
|
81 |
+
|
82 |
+
|
requirements.txt
CHANGED
@@ -3,4 +3,5 @@ Openai
|
|
3 |
Streamlit
|
4 |
huggingface_hub
|
5 |
python-dotenv
|
6 |
-
watchdog
|
|
|
|
3 |
Streamlit
|
4 |
huggingface_hub
|
5 |
python-dotenv
|
6 |
+
watchdog
|
7 |
+
streamlit-chat
|