Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import streamlit as st
|
3 |
+
from streamlit_pills import pills
|
4 |
+
|
5 |
+
openai.api_key = st.secrets['api_secret']
|
6 |
+
|
7 |
+
st.subheader("AI Assistant : Streamlit + OpenAI: `stream` *argument*")
|
8 |
+
|
9 |
+
# You can also use radio buttons instead
|
10 |
+
selected = pills("", ["NO Streaming", "Streaming"], ["π", "π"])
|
11 |
+
|
12 |
+
user_input = st.text_input("You: ",placeholder = "Ask me anything ...", key="input")
|
13 |
+
|
14 |
+
if st.button("Submit", type="primary"):
|
15 |
+
st.markdown("----")
|
16 |
+
res_box = st.empty()
|
17 |
+
|
18 |
+
if selected == "Streaming":
|
19 |
+
report = []
|
20 |
+
# Looping over the response
|
21 |
+
for resp in openai.Completion.create(model='text-davinci-003',
|
22 |
+
prompt=user_input,
|
23 |
+
max_tokens=120,
|
24 |
+
temperature = 0.5,
|
25 |
+
stream = True):
|
26 |
+
# join method to concatenate the elements of the list
|
27 |
+
# into a single string,
|
28 |
+
# then strip out any empty strings
|
29 |
+
report.append(resp.choices[0].text)
|
30 |
+
result = "".join(report).strip()
|
31 |
+
result = result.replace("\n", "")
|
32 |
+
res_box.markdown(f'*{result}*')
|
33 |
+
|
34 |
+
else:
|
35 |
+
completions = openai.Completion.create(model='text-davinci-003',
|
36 |
+
prompt=user_input,
|
37 |
+
max_tokens=120,
|
38 |
+
temperature = 0.5,
|
39 |
+
stream = False)
|
40 |
+
result = completions.choices[0].text
|
41 |
+
|
42 |
+
res_box.write(result)
|
43 |
+
st.markdown("----")
|