Spaces:
Runtime error
Runtime error
Commit
•
ce287ed
1
Parent(s):
6294fd6
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,43 @@
|
|
1 |
-
from openai import OpenAI
|
2 |
import streamlit as st
|
|
|
|
|
3 |
|
4 |
-
st.title("ChatGPT-like clone")
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
if "openai_model" not in st.session_state:
|
9 |
-
st.session_state["openai_model"] = "gpt-3.5-turbo"
|
10 |
|
|
|
|
|
|
|
11 |
if "messages" not in st.session_state:
|
12 |
st.session_state.messages = []
|
13 |
|
|
|
14 |
for message in st.session_state.messages:
|
15 |
with st.chat_message(message["role"]):
|
16 |
st.markdown(message["content"])
|
17 |
|
|
|
18 |
if prompt := st.chat_input("What is up?"):
|
|
|
19 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
20 |
with st.chat_message("user"):
|
21 |
st.markdown(prompt)
|
22 |
|
|
|
23 |
with st.chat_message("assistant"):
|
24 |
-
|
25 |
-
|
26 |
-
messages=[
|
27 |
-
{"role": m["role"], "content": m["content"]}
|
28 |
-
for m in st.session_state.messages
|
29 |
-
],
|
30 |
-
stream=True,
|
31 |
-
)
|
32 |
-
response = st.write_stream(stream)
|
33 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import random
|
3 |
+
import time
|
4 |
|
|
|
5 |
|
6 |
+
# Streamed response emulator
|
7 |
+
def response_generator():
|
8 |
+
response = random.choice(
|
9 |
+
[
|
10 |
+
"Hello there! How can I assist you today?",
|
11 |
+
"Hi, human! Is there anything I can help you with?",
|
12 |
+
"Do you need help?",
|
13 |
+
]
|
14 |
+
)
|
15 |
+
for word in response.split():
|
16 |
+
yield word + " "
|
17 |
+
time.sleep(0.05)
|
18 |
|
|
|
|
|
19 |
|
20 |
+
st.title("Simple chat")
|
21 |
+
|
22 |
+
# Initialize chat history
|
23 |
if "messages" not in st.session_state:
|
24 |
st.session_state.messages = []
|
25 |
|
26 |
+
# Display chat messages from history on app rerun
|
27 |
for message in st.session_state.messages:
|
28 |
with st.chat_message(message["role"]):
|
29 |
st.markdown(message["content"])
|
30 |
|
31 |
+
# Accept user input
|
32 |
if prompt := st.chat_input("What is up?"):
|
33 |
+
# Add user message to chat history
|
34 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
35 |
+
# Display user message in chat message container
|
36 |
with st.chat_message("user"):
|
37 |
st.markdown(prompt)
|
38 |
|
39 |
+
# Display assistant response in chat message container
|
40 |
with st.chat_message("assistant"):
|
41 |
+
response = st.write_stream(response_generator())
|
42 |
+
# Add assistant response to chat history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
st.session_state.messages.append({"role": "assistant", "content": response})
|