Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,35 +3,34 @@ import os
|
|
| 3 |
import openai
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
-
#
|
| 7 |
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 8 |
|
| 9 |
-
#
|
| 10 |
st.set_page_config(page_title="OpenAI Chatbot", layout="centered")
|
| 11 |
st.title("🤖 OpenAI Chatbot")
|
| 12 |
-
st.markdown("
|
| 13 |
|
| 14 |
-
# Initialize
|
| 15 |
if "messages" not in st.session_state:
|
| 16 |
st.session_state.messages = [
|
| 17 |
{"role": "system", "content": "You are a helpful assistant."}
|
| 18 |
]
|
| 19 |
|
| 20 |
-
#
|
| 21 |
for msg in st.session_state.messages[1:]:
|
| 22 |
with st.chat_message(msg["role"]):
|
| 23 |
st.markdown(msg["content"])
|
| 24 |
|
| 25 |
-
#
|
| 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 |
-
#
|
| 33 |
response = client.chat.completions.create(
|
| 34 |
-
model="gpt-
|
| 35 |
messages=st.session_state.messages,
|
| 36 |
temperature=0.7
|
| 37 |
)
|
|
@@ -39,4 +38,4 @@ if prompt := st.chat_input("Type your message..."):
|
|
| 39 |
st.chat_message("assistant").markdown(reply)
|
| 40 |
st.session_state.messages.append({"role": "assistant", "content": reply})
|
| 41 |
except Exception as e:
|
| 42 |
-
st.
|
|
|
|
| 3 |
import openai
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
+
# Initialize OpenAI client using v1.x API
|
| 7 |
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 8 |
|
| 9 |
+
# Streamlit page configuration
|
| 10 |
st.set_page_config(page_title="OpenAI Chatbot", layout="centered")
|
| 11 |
st.title("🤖 OpenAI Chatbot")
|
| 12 |
+
st.markdown("Jo Pocho k Wo Bataiya Jai Ga")
|
| 13 |
|
| 14 |
+
# Initialize conversation history
|
| 15 |
if "messages" not in st.session_state:
|
| 16 |
st.session_state.messages = [
|
| 17 |
{"role": "system", "content": "You are a helpful assistant."}
|
| 18 |
]
|
| 19 |
|
| 20 |
+
# Display 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 |
+
# Chat input box
|
| 26 |
+
if prompt := st.chat_input("Type your message here..."):
|
|
|
|
| 27 |
st.chat_message("user").markdown(prompt)
|
| 28 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 29 |
|
| 30 |
try:
|
| 31 |
+
# Send chat completion request
|
| 32 |
response = client.chat.completions.create(
|
| 33 |
+
model="gpt-3.5-turbo", # ✅ Compatible for all OpenAI API users
|
| 34 |
messages=st.session_state.messages,
|
| 35 |
temperature=0.7
|
| 36 |
)
|
|
|
|
| 38 |
st.chat_message("assistant").markdown(reply)
|
| 39 |
st.session_state.messages.append({"role": "assistant", "content": reply})
|
| 40 |
except Exception as e:
|
| 41 |
+
st.chat_message("assistant").markdown(f"❌ Error: {e}")
|