Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,13 @@ import os
|
|
| 3 |
import openai
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
openai.api_key
|
| 8 |
|
| 9 |
-
#
|
| 10 |
st.set_page_config(page_title="OpenAI Chatbot", layout="centered")
|
| 11 |
-
|
| 12 |
-
st.
|
| 13 |
-
st.markdown("Talk with GPT-4 using OpenAI API.")
|
| 14 |
|
| 15 |
# Initialize chat history
|
| 16 |
if "messages" not in st.session_state:
|
|
@@ -18,28 +17,26 @@ if "messages" not in st.session_state:
|
|
| 18 |
{"role": "system", "content": "You are a helpful assistant."}
|
| 19 |
]
|
| 20 |
|
| 21 |
-
#
|
| 22 |
for msg in st.session_state.messages[1:]:
|
| 23 |
-
|
| 24 |
-
st.
|
| 25 |
-
elif msg["role"] == "assistant":
|
| 26 |
-
st.chat_message("assistant").markdown(msg["content"])
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
if prompt := st.chat_input("
|
|
|
|
| 30 |
st.chat_message("user").markdown(prompt)
|
| 31 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 32 |
|
| 33 |
try:
|
| 34 |
-
#
|
| 35 |
-
response =
|
| 36 |
model="gpt-4", # or "gpt-3.5-turbo"
|
| 37 |
messages=st.session_state.messages,
|
| 38 |
-
temperature=0.7
|
| 39 |
)
|
| 40 |
-
reply = response.choices[0].message
|
| 41 |
st.chat_message("assistant").markdown(reply)
|
| 42 |
st.session_state.messages.append({"role": "assistant", "content": reply})
|
| 43 |
except Exception as e:
|
| 44 |
-
|
| 45 |
-
st.chat_message("assistant").markdown(error_msg)
|
|
|
|
| 3 |
import openai
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
+
# Load API key from environment variable
|
| 7 |
+
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 8 |
|
| 9 |
+
# Page setup
|
| 10 |
st.set_page_config(page_title="OpenAI Chatbot", layout="centered")
|
| 11 |
+
st.title("🤖 OpenAI Chatbot")
|
| 12 |
+
st.markdown("Talk to GPT-4 using OpenAI's latest API.")
|
|
|
|
| 13 |
|
| 14 |
# Initialize chat history
|
| 15 |
if "messages" not in st.session_state:
|
|
|
|
| 17 |
{"role": "system", "content": "You are a helpful assistant."}
|
| 18 |
]
|
| 19 |
|
| 20 |
+
# Show chat history
|
| 21 |
for msg in st.session_state.messages[1:]:
|
| 22 |
+
with st.chat_message(msg["role"]):
|
| 23 |
+
st.markdown(msg["content"])
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# User input
|
| 26 |
+
if prompt := st.chat_input("Type your message..."):
|
| 27 |
+
# Show user message
|
| 28 |
st.chat_message("user").markdown(prompt)
|
| 29 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 30 |
|
| 31 |
try:
|
| 32 |
+
# Call OpenAI's chat model using v1.x API
|
| 33 |
+
response = client.chat.completions.create(
|
| 34 |
model="gpt-4", # or "gpt-3.5-turbo"
|
| 35 |
messages=st.session_state.messages,
|
| 36 |
+
temperature=0.7
|
| 37 |
)
|
| 38 |
+
reply = response.choices[0].message.content
|
| 39 |
st.chat_message("assistant").markdown(reply)
|
| 40 |
st.session_state.messages.append({"role": "assistant", "content": reply})
|
| 41 |
except Exception as e:
|
| 42 |
+
st.error(f"Error: {e}")
|
|
|