Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,46 @@
|
|
| 1 |
import openai
|
| 2 |
import streamlit as st
|
| 3 |
|
|
|
|
| 4 |
openai.api_key = st.secrets["OPENAI_API_KEY"]
|
| 5 |
|
| 6 |
# Fine-tuned model name
|
| 7 |
MODEL_NAME = "ft:gpt-3.5-turbo-0125:brenin::AjNcIvnw"
|
| 8 |
|
|
|
|
| 9 |
st.title("Chat with Fine-Tuned GPT-3.5 Turbo")
|
| 10 |
st.markdown("This chatbot uses a fine-tuned GPT-3.5 Turbo model.")
|
| 11 |
|
|
|
|
| 12 |
if "messages" not in st.session_state:
|
| 13 |
st.session_state.messages = [
|
| 14 |
{"role": "system", "content": "You are a German chatbot. You should help the user by answering their questions."}
|
| 15 |
]
|
| 16 |
|
|
|
|
| 17 |
user_input = st.text_input("Enter your question:")
|
| 18 |
|
|
|
|
| 19 |
if user_input:
|
|
|
|
| 20 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 21 |
|
|
|
|
| 22 |
try:
|
| 23 |
response = openai.ChatCompletion.create(
|
| 24 |
model=MODEL_NAME,
|
| 25 |
messages=st.session_state.messages
|
| 26 |
)
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
st.session_state.messages.append({"role": "assistant", "content": assistant_reply})
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
assistant_reply = f"Error: {str(e)}"
|
|
|
|
| 31 |
|
|
|
|
| 32 |
for message in st.session_state.messages:
|
| 33 |
if message["role"] == "user":
|
| 34 |
st.markdown(f"**User:** {message['content']}")
|
|
|
|
| 1 |
import openai
|
| 2 |
import streamlit as st
|
| 3 |
|
| 4 |
+
# Load API key from secrets
|
| 5 |
openai.api_key = st.secrets["OPENAI_API_KEY"]
|
| 6 |
|
| 7 |
# Fine-tuned model name
|
| 8 |
MODEL_NAME = "ft:gpt-3.5-turbo-0125:brenin::AjNcIvnw"
|
| 9 |
|
| 10 |
+
# Streamlit App UI
|
| 11 |
st.title("Chat with Fine-Tuned GPT-3.5 Turbo")
|
| 12 |
st.markdown("This chatbot uses a fine-tuned GPT-3.5 Turbo model.")
|
| 13 |
|
| 14 |
+
# Initialize session state for messages
|
| 15 |
if "messages" not in st.session_state:
|
| 16 |
st.session_state.messages = [
|
| 17 |
{"role": "system", "content": "You are a German chatbot. You should help the user by answering their questions."}
|
| 18 |
]
|
| 19 |
|
| 20 |
+
# User input field
|
| 21 |
user_input = st.text_input("Enter your question:")
|
| 22 |
|
| 23 |
+
# If user submits a question
|
| 24 |
if user_input:
|
| 25 |
+
# Add user message to conversation history
|
| 26 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 27 |
|
| 28 |
+
# Call the OpenAI API using client.chat.completions.create()
|
| 29 |
try:
|
| 30 |
response = openai.ChatCompletion.create(
|
| 31 |
model=MODEL_NAME,
|
| 32 |
messages=st.session_state.messages
|
| 33 |
)
|
| 34 |
+
|
| 35 |
+
# Extract the assistant's reply
|
| 36 |
+
assistant_reply = response['choices'][0]['message']['content']
|
| 37 |
st.session_state.messages.append({"role": "assistant", "content": assistant_reply})
|
| 38 |
+
|
| 39 |
except Exception as e:
|
| 40 |
assistant_reply = f"Error: {str(e)}"
|
| 41 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_reply})
|
| 42 |
|
| 43 |
+
# Display the conversation
|
| 44 |
for message in st.session_state.messages:
|
| 45 |
if message["role"] == "user":
|
| 46 |
st.markdown(f"**User:** {message['content']}")
|