Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,37 @@
|
|
| 1 |
from openai import OpenAI
|
| 2 |
import streamlit as st
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
st.set_page_config(page_title="Mon Application AI", page_icon="path/to/favicon.ico", layout='wide')
|
| 7 |
-
|
| 8 |
-
# Chargement et affichage du logo
|
| 9 |
-
logo_path = "M-LAI.com.png" # Ajustez le chemin vers votre logo
|
| 10 |
-
st.image(logo_path, width=100)
|
| 11 |
-
|
| 12 |
-
# Récupération de la clé API à partir d'une variable d'environnement
|
| 13 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 14 |
if not openai_api_key:
|
| 15 |
st.error("No OpenAI API key found. Please set the OPENAI_API_KEY environment variable.")
|
| 16 |
st.stop()
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
model_choice =
|
| 20 |
-
'Choose a model:',
|
| 21 |
-
[
|
| 22 |
-
'gpt-4-turbo', 'gpt-3.5-turbo', 'gpt-3.5-turbo-0125', 'gpt-3.5-turbo-1106',
|
| 23 |
-
'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-16k-0613', 'gpt-3.5-turbo-16k',
|
| 24 |
-
'gpt-4-turbo-2024-04-09', 'gpt-4-turbo-preview', 'gpt-4-0125-preview',
|
| 25 |
-
'gpt-4-1106-preview', 'gpt-4-0613'
|
| 26 |
-
]
|
| 27 |
-
)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
if "messages" not in st.session_state:
|
| 31 |
-
st.session_state["messages"] = []
|
| 32 |
|
| 33 |
-
#
|
| 34 |
for msg in st.session_state.messages:
|
| 35 |
-
|
| 36 |
-
st.success(msg["content"])
|
| 37 |
-
else:
|
| 38 |
-
st.info(msg["content"])
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
prompt = st.
|
| 42 |
if prompt:
|
| 43 |
-
#
|
| 44 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
with st.spinner('
|
| 48 |
client = OpenAI(api_key=openai_api_key)
|
| 49 |
response = client.chat.completions.create(model=model_choice, messages=st.session_state.messages)
|
| 50 |
-
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
st.session_state.messages.append({"role": "assistant", "content":
|
| 54 |
-
st.
|
|
|
|
| 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 |
if not openai_api_key:
|
| 8 |
st.error("No OpenAI API key found. Please set the OPENAI_API_KEY environment variable.")
|
| 9 |
st.stop()
|
| 10 |
|
| 11 |
+
# Utilisation fixe du modèle gpt-4-turbo
|
| 12 |
+
model_choice = 'gpt-4-turbo'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Initialize session state for storing messages if it doesn't already exist
|
| 15 |
if "messages" not in st.session_state:
|
| 16 |
+
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
|
| 17 |
|
| 18 |
+
# Display all previous messages
|
| 19 |
for msg in st.session_state.messages:
|
| 20 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Input for new prompts
|
| 23 |
+
prompt = st.chat_input("Enter your question:")
|
| 24 |
if prompt:
|
| 25 |
+
# Append the new user message to session state
|
| 26 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 27 |
+
st.chat_message("user").write(prompt)
|
| 28 |
|
| 29 |
+
# Use a spinner to indicate that the model is generating a response
|
| 30 |
+
with st.spinner('M-LAI is Thinking...'):
|
| 31 |
client = OpenAI(api_key=openai_api_key)
|
| 32 |
response = client.chat.completions.create(model=model_choice, messages=st.session_state.messages)
|
| 33 |
+
msg = response.choices[0].message.content
|
| 34 |
|
| 35 |
+
# Append and display the assistant's response
|
| 36 |
+
st.session_state.messages.append({"role": "assistant", "content": msg})
|
| 37 |
+
st.chat_message("assistant").write(msg)
|