Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,42 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
-
import anthropic
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
client = anthropic.Anthropic(api_key=api_key)
|
| 8 |
|
| 9 |
-
st.title("💬 Chatbot
|
|
|
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
| 12 |
if "messages" not in st.session_state:
|
| 13 |
-
st.session_state["messages"] = []
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
st.
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
response = client.messages.create(
|
| 41 |
-
model="claude-3-opus-20240229",
|
| 42 |
-
max_tokens=1024,
|
| 43 |
-
messages=[{"role": msg["role"], "content": msg["content"]} for msg in st.session_state.messages]
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
# Récupérer et traiter le contenu de la réponse
|
| 47 |
-
if isinstance(response.content, list):
|
| 48 |
-
msg = " ".join(block.text for block in response.content)
|
| 49 |
-
elif isinstance(response.content, dict) and 'text' in response.content:
|
| 50 |
-
msg = response.content['text']
|
| 51 |
-
else:
|
| 52 |
-
msg = str(response.content)
|
| 53 |
-
|
| 54 |
-
# Ajouter et afficher la réponse de l'assistant
|
| 55 |
-
st.session_state.messages.append({"role": "assistant", "content": msg})
|
| 56 |
-
st.markdown(f"<div style='margin-right: 10px; text-align: right;'><img src='https://www.example.com/bot_icon.png' width='24'> Assistant: {msg}</div>", unsafe_allow_html=True)
|
|
|
|
| 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 |
+
st.title("💬 Chatbot")
|
| 9 |
+
st.caption("")
|
| 10 |
|
| 11 |
+
# Select model using a dropdown
|
| 12 |
+
model_choice = st.selectbox('Choose a model:', ['gpt-3.5-turbo', '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 |
+
|
| 23 |
+
# Input for new prompts
|
| 24 |
+
prompt = st.chat_input("Enter your question:")
|
| 25 |
+
if prompt:
|
| 26 |
+
if not openai_api_key:
|
| 27 |
+
st.error("No OpenAI API key found. Please set the OPENAI_API_KEY environment variable.")
|
| 28 |
+
st.stop()
|
| 29 |
+
|
| 30 |
+
# Append the new user message to session state
|
| 31 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 32 |
+
st.chat_message("user").write(prompt)
|
| 33 |
+
|
| 34 |
+
# Use a spinner to indicate that the model is generating a response
|
| 35 |
+
with st.spinner('Thinking...'):
|
| 36 |
+
client = OpenAI(api_key=openai_api_key)
|
| 37 |
+
response = client.chat.completions.create(model=model_choice, messages=st.session_state.messages)
|
| 38 |
+
msg = response.choices[0].message.content
|
| 39 |
+
|
| 40 |
+
# Append and display the assistant's response
|
| 41 |
+
st.session_state.messages.append({"role": "assistant", "content": msg})
|
| 42 |
+
st.chat_message("assistant").write(msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|