Spaces:
Sleeping
Sleeping
new file
Browse files
app.py
CHANGED
@@ -5,47 +5,34 @@ from tools import sentiment_analysis_util
|
|
5 |
import numpy as np
|
6 |
from dotenv import load_dotenv
|
7 |
import os
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
|
|
|
|
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
@st.cache_resource
|
20 |
-
def initialize_session_state():
|
21 |
-
if "chat_history" not in st.session_state:
|
22 |
-
st.session_state["messages"] = [{"role":"system", "content":"""
|
23 |
-
You are a sentiment analysis expert. Answer all questions related to cryptocurrency investment reccommendations. Say I don't know if you don't know.
|
24 |
-
"""}]
|
25 |
-
|
26 |
-
initialize_session_state()
|
27 |
-
|
28 |
-
client = OpenAI(api_key=OPENAI_API_KEY)
|
29 |
-
|
30 |
-
if "openai_model" not in st.session_state:
|
31 |
-
st.session_state["openai_model"] = "gpt-3.5-turbo"
|
32 |
-
|
33 |
-
if prompt := st.chat_input("Any other questions? "):
|
34 |
-
# Add user message to chat history
|
35 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
messages=[
|
44 |
-
{"role": m["role"], "content": m["content"]}
|
45 |
-
for m in st.session_state.messages
|
46 |
-
],
|
47 |
-
stream=True,
|
48 |
-
)
|
49 |
-
response = st.write_stream(stream)
|
50 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
51 |
|
|
|
|
|
|
|
|
5 |
import numpy as np
|
6 |
from dotenv import load_dotenv
|
7 |
import os
|
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 |
+
# Input for new prompts
|
20 |
+
prompt = st.chat_input("Enter your question:")
|
21 |
+
if prompt:
|
22 |
+
if not openai_api_key:
|
23 |
+
st.error("No OpenAI API key found. Please set the OPENAI_API_KEY environment variable.")
|
24 |
+
st.stop()
|
25 |
|
26 |
+
# Append the new user message to session state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
28 |
+
st.chat_message("user").write(prompt)
|
29 |
+
|
30 |
+
# Use a spinner to indicate that the model is generating a response
|
31 |
+
with st.spinner('Thinking...'):
|
32 |
+
client = OpenAI(api_key=openai_api_key)
|
33 |
+
response = client.chat.completions.create(model="gpt-3.5-turbo", messages=st.session_state.messages)
|
34 |
+
msg = response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
# Append and display the assistant's response
|
37 |
+
st.session_state.messages.append({"role": "assistant", "content": msg})
|
38 |
+
st.chat_message("assistant").write(msg)
|