Spaces:
Sleeping
Sleeping
darthPanda
commited on
Commit
·
14d9eb1
1
Parent(s):
222ffdf
hooha
Browse files- .gitignore +1 -0
- app.py +37 -0
- requirements.txt +2 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.streamlit
|
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
import streamlit as st
|
3 |
+
|
4 |
+
st.title("ChatGPT-like clone")
|
5 |
+
|
6 |
+
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
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 |
+
message_placeholder = st.empty()
|
25 |
+
full_response = ""
|
26 |
+
for response in client.chat.completions.create(
|
27 |
+
model=st.session_state["openai_model"],
|
28 |
+
messages=[
|
29 |
+
{"role": m["role"], "content": m["content"]}
|
30 |
+
for m in st.session_state.messages
|
31 |
+
],
|
32 |
+
stream=True,
|
33 |
+
):
|
34 |
+
full_response += (response.choices[0].delta.content or "")
|
35 |
+
message_placeholder.markdown(full_response + "▌")
|
36 |
+
message_placeholder.markdown(full_response)
|
37 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
streamlit==1.25.0
|