-
Notifications
You must be signed in to change notification settings - Fork 100
/
chatgpt.py
74 lines (52 loc) · 1.6 KB
/
chatgpt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import streamlit as st
from streamlit_chat import message
import openai
from config import open_api_key
openai.api_key = open_api_key
# openAI code
def openai_create(prompt):
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.9,
max_tokens=150,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6,
stop=[" Human:", " AI:"]
)
return response.choices[0].text
def chatgpt_clone(input, history):
history = history or []
s = list(sum(history, ()))
print(s)
s.append(input)
inp = ' '.join(s)
output = openai_create(inp)
history.append((input, output))
return history, history
# Streamlit App
st.set_page_config(
page_title="Streamlit Chat - Demo",
page_icon=":robot:"
)
st.header("ChatGPT Clone with Streamlit")
history_input = []
if 'generated' not in st.session_state:
st.session_state['generated'] = []
if 'past' not in st.session_state:
st.session_state['past'] = []
def get_text():
input_text = st.text_input("You: ", key="input")
return input_text
user_input = get_text()
if user_input:
output = chatgpt_clone(user_input, history_input)
history_input.append([user_input, output])
st.session_state.past.append(user_input)
st.session_state.generated.append(output[0])
if st.session_state['generated']:
for i in range(len(st.session_state['generated'])-1, -1, -1):
message(st.session_state["generated"][i], key=str(i))
message(st.session_state['past'][i],
is_user=True, key=str(i) + '_user')