Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,26 @@
|
|
1 |
import streamlit as st
|
2 |
-
from streamlit_chat import message
|
3 |
from openai import OpenAI
|
4 |
import time
|
5 |
import datetime
|
6 |
import os
|
7 |
|
8 |
-
#
|
9 |
generated_user = os.getenv("User")
|
10 |
generated_password = os.getenv("Password")
|
11 |
openai_key = os.getenv("openai_key")
|
12 |
-
assistant_id = os.getenv("ASSISTANT_ID")
|
13 |
|
14 |
-
# Streamlit
|
15 |
st.set_page_config(page_title="Carfind.co.za AI Assistant", layout="wide")
|
16 |
-
st.
|
17 |
st.caption("Chat with Carfind.co.za and find your next car fast")
|
18 |
|
19 |
-
#
|
20 |
-
dark_mode = st.toggle("π Switch to Light Mode", value=True)
|
21 |
-
|
22 |
-
# Custom styling for bubbles
|
23 |
-
st.markdown("""
|
24 |
-
<style>
|
25 |
-
.stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; }
|
26 |
-
.stChatMessage[data-testid="stChatMessage-user"] { background: #333; color: white; }
|
27 |
-
.stChatMessage[data-testid="stChatMessage-assistant"] { background: #00529B; color: white; }
|
28 |
-
</style>
|
29 |
-
""", unsafe_allow_html=True)
|
30 |
-
|
31 |
-
# Authentication
|
32 |
if "authenticated" not in st.session_state:
|
33 |
st.session_state.authenticated = False
|
34 |
|
|
|
35 |
if not st.session_state.authenticated:
|
36 |
-
st.subheader("
|
37 |
username = st.text_input("Username")
|
38 |
password = st.text_input("Password", type="password")
|
39 |
|
@@ -46,33 +33,36 @@ if not st.session_state.authenticated:
|
|
46 |
else:
|
47 |
st.error("Incorrect username or password. Please try again.")
|
48 |
|
49 |
-
#
|
50 |
else:
|
51 |
st.divider()
|
52 |
|
53 |
if "messages" not in st.session_state:
|
54 |
st.session_state["messages"] = []
|
55 |
|
56 |
-
#
|
57 |
-
for
|
58 |
-
|
59 |
-
|
|
|
|
|
60 |
else:
|
61 |
-
|
62 |
|
63 |
-
# Input
|
64 |
-
|
65 |
-
with input_col:
|
66 |
-
user_input = st.text_input("Type your message here...", key="chat_input")
|
67 |
|
68 |
-
with
|
69 |
-
if st.button("
|
70 |
st.session_state.messages = []
|
71 |
-
st.success("Chat cleared.")
|
72 |
-
|
|
|
|
|
73 |
|
74 |
-
if openai_key
|
75 |
client = OpenAI(api_key=openai_key)
|
|
|
76 |
|
77 |
def save_transcript(messages):
|
78 |
with open("chat_logs.txt", "a") as log:
|
@@ -82,31 +72,43 @@ else:
|
|
82 |
log.write("--- End Chat ---\n")
|
83 |
|
84 |
if user_input:
|
85 |
-
st.session_state.messages
|
86 |
-
|
|
|
87 |
|
88 |
-
|
89 |
-
with st.spinner("Thinking and typing... π"):
|
90 |
thread = client.beta.threads.create()
|
91 |
-
|
92 |
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
while True:
|
96 |
-
run_status = client.beta.threads.runs.retrieve(
|
|
|
|
|
|
|
97 |
if run_status.status == "completed":
|
98 |
break
|
99 |
time.sleep(1)
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
st.session_state.messages.append({"role": "assistant", "content": assistant_reply})
|
104 |
-
message(f"π **Carfind Assistant:** {assistant_reply}", is_user=False)
|
105 |
-
|
106 |
-
save_transcript(st.session_state.messages)
|
107 |
|
108 |
-
|
109 |
-
|
|
|
|
|
110 |
|
|
|
|
|
111 |
else:
|
112 |
-
st.error("
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from openai import OpenAI
|
3 |
import time
|
4 |
import datetime
|
5 |
import os
|
6 |
|
7 |
+
# Securely get credentials
|
8 |
generated_user = os.getenv("User")
|
9 |
generated_password = os.getenv("Password")
|
10 |
openai_key = os.getenv("openai_key")
|
|
|
11 |
|
12 |
+
# Streamlit app configuration
|
13 |
st.set_page_config(page_title="Carfind.co.za AI Assistant", layout="wide")
|
14 |
+
st.title("π Carfind.co.za AI Assistant")
|
15 |
st.caption("Chat with Carfind.co.za and find your next car fast")
|
16 |
|
17 |
+
# Initialize authentication state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
if "authenticated" not in st.session_state:
|
19 |
st.session_state.authenticated = False
|
20 |
|
21 |
+
# Login screen
|
22 |
if not st.session_state.authenticated:
|
23 |
+
st.subheader("π Login")
|
24 |
username = st.text_input("Username")
|
25 |
password = st.text_input("Password", type="password")
|
26 |
|
|
|
33 |
else:
|
34 |
st.error("Incorrect username or password. Please try again.")
|
35 |
|
36 |
+
# Main chat interface
|
37 |
else:
|
38 |
st.divider()
|
39 |
|
40 |
if "messages" not in st.session_state:
|
41 |
st.session_state["messages"] = []
|
42 |
|
43 |
+
# Display chat history
|
44 |
+
for message in st.session_state.messages:
|
45 |
+
role = message["role"]
|
46 |
+
content = message["content"]
|
47 |
+
if role == "assistant":
|
48 |
+
st.markdown(f'<img src="https://www.carfind.co.za/images/Carfind-Icon.svg" width="30" style="vertical-align: middle;"> <strong>Carfind Assistant:</strong> {content}', unsafe_allow_html=True)
|
49 |
else:
|
50 |
+
st.chat_message(role).write(content)
|
51 |
|
52 |
+
# Input with clear chat button
|
53 |
+
col_clear, col_input = st.columns([1, 8])
|
|
|
|
|
54 |
|
55 |
+
with col_clear:
|
56 |
+
if st.button("π§Ή Clear Chat"):
|
57 |
st.session_state.messages = []
|
58 |
+
st.success("Chat history cleared.")
|
59 |
+
|
60 |
+
with col_input:
|
61 |
+
user_input = st.chat_input("Type your message here...")
|
62 |
|
63 |
+
if openai_key:
|
64 |
client = OpenAI(api_key=openai_key)
|
65 |
+
ASSISTANT_ID = "asst_5gQR21fOsmHil11FGBzEArA7"
|
66 |
|
67 |
def save_transcript(messages):
|
68 |
with open("chat_logs.txt", "a") as log:
|
|
|
72 |
log.write("--- End Chat ---\n")
|
73 |
|
74 |
if user_input:
|
75 |
+
if len(st.session_state.messages) == 0 or user_input != st.session_state.messages[-1]["content"]:
|
76 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
77 |
+
st.chat_message("user").write(user_input)
|
78 |
|
79 |
+
try:
|
|
|
80 |
thread = client.beta.threads.create()
|
81 |
+
thread_id = thread.id
|
82 |
|
83 |
+
client.beta.threads.messages.create(
|
84 |
+
thread_id=thread_id,
|
85 |
+
role="user",
|
86 |
+
content=user_input
|
87 |
+
)
|
88 |
+
|
89 |
+
run = client.beta.threads.runs.create(
|
90 |
+
thread_id=thread_id,
|
91 |
+
assistant_id=ASSISTANT_ID
|
92 |
+
)
|
93 |
|
94 |
while True:
|
95 |
+
run_status = client.beta.threads.runs.retrieve(
|
96 |
+
thread_id=thread_id,
|
97 |
+
run_id=run.id
|
98 |
+
)
|
99 |
if run_status.status == "completed":
|
100 |
break
|
101 |
time.sleep(1)
|
102 |
|
103 |
+
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
104 |
+
assistant_message = messages.data[0].content[0].text.value.strip()
|
|
|
|
|
|
|
|
|
105 |
|
106 |
+
if assistant_message.lower() != user_input.lower():
|
107 |
+
st.markdown(f'<img src="https://www.carfind.co.za/images/Carfind-Icon.svg" width="30" style="vertical-align: middle;"> <strong>Carfind Assistant:</strong> {assistant_message}', unsafe_allow_html=True)
|
108 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
109 |
+
save_transcript(st.session_state.messages)
|
110 |
|
111 |
+
except Exception as e:
|
112 |
+
st.error(f"An error occurred: {str(e)}")
|
113 |
else:
|
114 |
+
st.error("OpenAI key not found. Please ensure it is set as a Hugging Face secret.")
|