Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,26 +5,19 @@ import os # Import the os module
|
|
| 5 |
# Fetch the API key from an environment variable
|
| 6 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# Lecture du message système à partir d'un fichier texte
|
| 11 |
-
try:
|
| 12 |
-
with open("system_message.txt", "r") as file:
|
| 13 |
-
system_message = file.read().strip()
|
| 14 |
-
except FileNotFoundError:
|
| 15 |
-
st.error("The system message file was not found. Please make sure 'system_message.txt' exists.")
|
| 16 |
-
st.stop()
|
| 17 |
|
| 18 |
# Initialize session state for storing messages if it doesn't already exist
|
| 19 |
if "messages" not in st.session_state:
|
| 20 |
-
st.session_state["messages"] = [{"role": "
|
| 21 |
-
{"role": "assistant", "content": "How can I help you?"}]
|
| 22 |
|
| 23 |
-
# Display all previous messages
|
| 24 |
for msg in st.session_state.messages:
|
| 25 |
-
|
| 26 |
-
st.chat_message(msg["role"]).write(msg["content"])
|
| 27 |
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Input for new prompts
|
| 30 |
prompt = st.chat_input("Enter your question:")
|
|
@@ -38,9 +31,9 @@ if prompt:
|
|
| 38 |
st.chat_message("user").write(prompt)
|
| 39 |
|
| 40 |
# Use a spinner to indicate that the model is generating a response
|
| 41 |
-
with st.spinner('
|
| 42 |
client = OpenAI(api_key=openai_api_key)
|
| 43 |
-
response = client.chat.completions.create(model=
|
| 44 |
msg = response.choices[0].message.content
|
| 45 |
|
| 46 |
# Append and display the assistant's response
|
|
|
|
| 5 |
# Fetch the API key from an environment variable
|
| 6 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
+
st.title("💬 Chatbot")
|
| 9 |
+
st.caption("")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Initialize session state for storing messages if it doesn't already exist
|
| 12 |
if "messages" not in st.session_state:
|
| 13 |
+
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
|
|
|
|
| 14 |
|
| 15 |
+
# Display all previous messages
|
| 16 |
for msg in st.session_state.messages:
|
| 17 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
|
|
|
| 18 |
|
| 19 |
+
# Select model using a dropdown
|
| 20 |
+
model_choice = st.selectbox('Choose a model:', ['gpt-3.5-turbo', 'gpt-4', 'davinci', 'curie'])
|
| 21 |
|
| 22 |
# Input for new prompts
|
| 23 |
prompt = st.chat_input("Enter your question:")
|
|
|
|
| 31 |
st.chat_message("user").write(prompt)
|
| 32 |
|
| 33 |
# Use a spinner to indicate that the model is generating a response
|
| 34 |
+
with st.spinner('Thinking...'):
|
| 35 |
client = OpenAI(api_key=openai_api_key)
|
| 36 |
+
response = client.chat.completions.create(model=model_choice, messages=st.session_state.messages)
|
| 37 |
msg = response.choices[0].message.content
|
| 38 |
|
| 39 |
# Append and display the assistant's response
|