MichaelPuhara
commited on
Commit
•
f5bc031
1
Parent(s):
038aa76
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
import streamlit as st
|
4 |
+
from streamlit_chat import message
|
5 |
+
|
6 |
+
headers = {
|
7 |
+
"authorization": st.secrets["API_KEY"],
|
8 |
+
}
|
9 |
+
|
10 |
+
#openai.api_key = ""
|
11 |
+
|
12 |
+
#openai.api_key = os.getenv("OPENAI_API_KEY")
|
13 |
+
# openAI code
|
14 |
+
|
15 |
+
def openai_create(prompt):
|
16 |
+
|
17 |
+
response = openai.Completion.create(
|
18 |
+
model="text-davinci-003",
|
19 |
+
prompt=prompt,
|
20 |
+
temperature=0.9,
|
21 |
+
max_tokens=150,
|
22 |
+
top_p=1,
|
23 |
+
frequency_penalty=0,
|
24 |
+
presence_penalty=0.6,
|
25 |
+
stop=[" Human:", " AI:"]
|
26 |
+
)
|
27 |
+
|
28 |
+
return response.choices[0].text
|
29 |
+
|
30 |
+
|
31 |
+
def chatgpt_clone(input, history):
|
32 |
+
history = history or []
|
33 |
+
s = list(sum(history, ()))
|
34 |
+
print(s)
|
35 |
+
s.append(input)
|
36 |
+
inp = ' '.join(s)
|
37 |
+
output = openai_create(inp)
|
38 |
+
history.append((input, output))
|
39 |
+
return history, history
|
40 |
+
|
41 |
+
# Streamlit App
|
42 |
+
st.set_page_config(
|
43 |
+
page_title="Streamlit Chat - Demo",
|
44 |
+
page_icon=":robot:"
|
45 |
+
)
|
46 |
+
|
47 |
+
st.header("ChatGPT Bot with Streamlit")
|
48 |
+
|
49 |
+
history_input = []
|
50 |
+
|
51 |
+
if 'generated' not in st.session_state:
|
52 |
+
st.session_state['generated'] = []
|
53 |
+
|
54 |
+
if 'past' not in st.session_state:
|
55 |
+
st.session_state['past'] = []
|
56 |
+
|
57 |
+
|
58 |
+
def get_text():
|
59 |
+
input_text = st.text_input("You: ", key="input")
|
60 |
+
return input_text
|
61 |
+
|
62 |
+
|
63 |
+
user_input = get_text()
|
64 |
+
|
65 |
+
|
66 |
+
if user_input:
|
67 |
+
output = chatgpt_clone(user_input, history_input)
|
68 |
+
history_input.append([user_input, output])
|
69 |
+
st.session_state.past.append(user_input)
|
70 |
+
st.session_state.generated.append(output[0])
|
71 |
+
|
72 |
+
if st.session_state['generated']:
|
73 |
+
|
74 |
+
for i in range(len(st.session_state['generated'])-1, -1, -1):
|
75 |
+
message(st.session_state["generated"][i], key=str(i))
|
76 |
+
message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
|