Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
from openai import OpenAI
|
| 2 |
import streamlit as st
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
st.set_page_config(page_title="Chat with AI", layout="wide")
|
| 7 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
# Lecture du message système à partir d'un fichier texte
|
| 13 |
try:
|
|
@@ -17,31 +16,33 @@ except FileNotFoundError:
|
|
| 17 |
st.error("The system message file was not found. Please make sure 'system_message.txt' exists.")
|
| 18 |
st.stop()
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
if "messages" not in st.session_state
|
| 22 |
-
st.session_state["messages"] = [
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
st.text_area("Assistant", value=message["content"], height=100, disabled=True)
|
| 33 |
-
|
| 34 |
-
# Entrée pour de nouveaux prompts
|
| 35 |
-
prompt = st.text_input("Enter your question:")
|
| 36 |
if prompt:
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
with st.spinner('
|
| 42 |
client = OpenAI(api_key=openai_api_key)
|
| 43 |
-
response = client.chat.completions.create(model=
|
| 44 |
-
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
st.session_state.messages.append({"role": "assistant", "content":
|
|
|
|
|
|
| 1 |
from openai import OpenAI
|
| 2 |
import streamlit as st
|
| 3 |
+
import os # Import the os module
|
| 4 |
|
| 5 |
+
# Fetch the API key from an environment variable
|
|
|
|
| 6 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
+
|
| 8 |
+
# Select model using a dropdown
|
| 9 |
+
model_choice = st.selectbox('Choose a model:', ['gpt-4-turbo','gpt-3.5-turbo', 'gpt-3.5-turbo-0125','gpt-3.5-turbo-1106','gpt-3.5-turbo-0613','gpt-3.5-turbo-16k-0613','gpt-3.5-turbo-16k','gpt-4-turbo-2024-04-09','gpt-4-turbo-preview', 'gpt-4-0125-preview','gpt-4-1106-preview','gpt-4-0613'])
|
| 10 |
|
| 11 |
# Lecture du message système à partir d'un fichier texte
|
| 12 |
try:
|
|
|
|
| 16 |
st.error("The system message file was not found. Please make sure 'system_message.txt' exists.")
|
| 17 |
st.stop()
|
| 18 |
|
| 19 |
+
# Initialize session state for storing messages if it doesn't already exist
|
| 20 |
+
if "messages" not in st.session_state:
|
| 21 |
+
st.session_state["messages"] = [{"role": "system", "content": system_message},
|
| 22 |
+
{"role": "assistant", "content": "How can I help you?"}]
|
| 23 |
+
|
| 24 |
+
# Display all previous messages
|
| 25 |
+
for msg in st.session_state.messages:
|
| 26 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# Input for new prompts
|
| 30 |
+
prompt = st.chat_input("Enter your question:")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
if prompt:
|
| 32 |
+
if not openai_api_key:
|
| 33 |
+
st.error("No OpenAI API key found. Please set the OPENAI_API_KEY environment variable.")
|
| 34 |
+
st.stop()
|
| 35 |
+
|
| 36 |
+
# Append the new user message to session state
|
| 37 |
st.session_state.messages.append({"role": "user", "content": 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('PromptingAI is Thinking...'):
|
| 42 |
client = OpenAI(api_key=openai_api_key)
|
| 43 |
+
response = client.chat.completions.create(model=model_choice, messages=st.session_state.messages)
|
| 44 |
+
msg = response.choices[0].message.content
|
| 45 |
|
| 46 |
+
# Append and display the assistant's response
|
| 47 |
+
st.session_state.messages.append({"role": "assistant", "content": msg})
|
| 48 |
+
st.chat_message("assistant").write(msg)
|