Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from openai import OpenAI # Changed import
|
| 3 |
+
|
| 4 |
+
# Set up OpenAI API key
|
| 5 |
+
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"]) # Changed to client
|
| 6 |
+
|
| 7 |
+
# Initialize session state for messages
|
| 8 |
+
if "messages" not in st.session_state:
|
| 9 |
+
st.session_state.messages = []
|
| 10 |
+
|
| 11 |
+
# Display existing messages
|
| 12 |
+
for message in st.session_state.messages:
|
| 13 |
+
with st.chat_message(message["role"]):
|
| 14 |
+
st.markdown(message["content"])
|
| 15 |
+
|
| 16 |
+
# Chat input
|
| 17 |
+
prompt = st.chat_input("Say something")
|
| 18 |
+
|
| 19 |
+
if prompt:
|
| 20 |
+
# Add user message to session state
|
| 21 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 22 |
+
with st.chat_message("user"):
|
| 23 |
+
st.markdown(prompt)
|
| 24 |
+
|
| 25 |
+
# Get OpenAI response
|
| 26 |
+
completion = client.chat.completions.create( # Changed to client
|
| 27 |
+
model="gpt-3.5-turbo",
|
| 28 |
+
messages=[
|
| 29 |
+
{"role": m["role"], "content": m["content"]}
|
| 30 |
+
for m in st.session_state.messages
|
| 31 |
+
]
|
| 32 |
+
)
|
| 33 |
+
response = completion.choices[0].message.content
|
| 34 |
+
|
| 35 |
+
# Add assistant message to session state
|
| 36 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 37 |
+
with st.chat_message("assistant"):
|
| 38 |
+
st.markdown(response)
|