Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
st.title("ChatGPT-Clone")
|
| 5 |
+
|
| 6 |
+
if "openai_model" not in st.session_state:
|
| 7 |
+
st.session_state["openai_model"] = "gpt-3.5-turbo-16k"
|
| 8 |
+
|
| 9 |
+
if "messages" not in st.session_state:
|
| 10 |
+
st.session_state.messages = []
|
| 11 |
+
|
| 12 |
+
for message in st.session_state.messages:
|
| 13 |
+
with st.chat_message(message["role"]):
|
| 14 |
+
st.markdown(message["content"])
|
| 15 |
+
|
| 16 |
+
with st.sidebar:
|
| 17 |
+
openai_api_key = st.text_input("OpenAI API Key", type="password")
|
| 18 |
+
if not openai_api_key:
|
| 19 |
+
st.info("Please add your OpenAI API key to continue.")
|
| 20 |
+
st.stop()
|
| 21 |
+
if prompt := st.chat_input("What is up?"):
|
| 22 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 23 |
+
with st.chat_message("user"):
|
| 24 |
+
st.markdown(prompt)
|
| 25 |
+
|
| 26 |
+
with st.chat_message("assistant"):
|
| 27 |
+
message_placeholder = st.empty()
|
| 28 |
+
full_response = ""
|
| 29 |
+
for response in openai.ChatCompletion.create(
|
| 30 |
+
model=st.session_state["openai_model"],
|
| 31 |
+
messages=[
|
| 32 |
+
{"role": m["role"], "content": m["content"]}
|
| 33 |
+
for m in st.session_state.messages
|
| 34 |
+
],
|
| 35 |
+
max_tokens=8000,
|
| 36 |
+
stream=True,
|
| 37 |
+
):
|
| 38 |
+
full_response += response.choices[0].delta.get("content", "")
|
| 39 |
+
message_placeholder.markdown(full_response + "▌")
|
| 40 |
+
message_placeholder.markdown(full_response)
|
| 41 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|