Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,20 +2,18 @@ import streamlit as st
|
|
2 |
from openai import OpenAI
|
3 |
import time
|
4 |
import os
|
|
|
5 |
import datetime
|
6 |
|
7 |
-
# Get secrets from environment
|
8 |
generated_user = os.getenv("User")
|
9 |
generated_password = os.getenv("Password")
|
10 |
openai_key = os.getenv("openai_key")
|
11 |
assistant_id = os.getenv("ASSISTANT_ID")
|
12 |
|
13 |
-
# Setup Streamlit app
|
14 |
st.set_page_config(page_title="Carfind.co.za AI Assistant", layout="wide")
|
15 |
st.markdown("<h1 style='text-align: center;'>🚗 Carfind.co.za AI Assistant</h1>", unsafe_allow_html=True)
|
16 |
st.caption("Chat with Carfind.co.za and find your next car fast")
|
17 |
|
18 |
-
# Style tweaks for chat bubbles
|
19 |
st.markdown("""
|
20 |
<style>
|
21 |
.stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; }
|
@@ -24,19 +22,29 @@ st.markdown("""
|
|
24 |
</style>
|
25 |
""", unsafe_allow_html=True)
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
if "authenticated" not in st.session_state:
|
41 |
st.session_state.authenticated = False
|
42 |
|
@@ -54,7 +62,6 @@ if not st.session_state.authenticated:
|
|
54 |
else:
|
55 |
st.error("Incorrect username or password. Please try again.")
|
56 |
|
57 |
-
# Main chat interface
|
58 |
else:
|
59 |
st.divider()
|
60 |
|
@@ -82,6 +89,7 @@ else:
|
|
82 |
client.beta.threads.messages.create(
|
83 |
thread_id=st.session_state["thread_id"], role="user", content=user_input
|
84 |
)
|
|
|
85 |
|
86 |
try:
|
87 |
with st.spinner("Thinking and typing... 💭"):
|
@@ -101,29 +109,28 @@ else:
|
|
101 |
thread_id=st.session_state["thread_id"]
|
102 |
)
|
103 |
|
104 |
-
# Save the transcript automatically
|
105 |
-
save_transcript(st.session_state["thread_id"], messages_response.data)
|
106 |
-
|
107 |
assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='22' style='vertical-align:middle;'/>"
|
108 |
|
109 |
-
# Display entire conversation in proper order
|
110 |
for msg in reversed(messages_response.data):
|
|
|
111 |
if msg.role == "user":
|
112 |
st.markdown(
|
113 |
f"<div style='background-color:#f0f0f0; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
|
114 |
-
f"👤 <strong>You:</strong> {
|
115 |
f"</div>",
|
116 |
unsafe_allow_html=True
|
117 |
)
|
118 |
else:
|
119 |
st.markdown(
|
120 |
f"<div style='background-color:#D6E9FE; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
|
121 |
-
f"{assistant_icon_html} <strong>Carfind Assistant:</strong> {
|
122 |
f"</div>",
|
123 |
unsafe_allow_html=True
|
124 |
)
|
|
|
125 |
|
126 |
except Exception as e:
|
127 |
st.error(f"An error occurred: {str(e)}")
|
|
|
128 |
else:
|
129 |
st.error("⚠️ OpenAI key or Assistant ID not found. Please ensure both are set as Hugging Face secrets.")
|
|
|
2 |
from openai import OpenAI
|
3 |
import time
|
4 |
import os
|
5 |
+
import pandas as pd
|
6 |
import datetime
|
7 |
|
|
|
8 |
generated_user = os.getenv("User")
|
9 |
generated_password = os.getenv("Password")
|
10 |
openai_key = os.getenv("openai_key")
|
11 |
assistant_id = os.getenv("ASSISTANT_ID")
|
12 |
|
|
|
13 |
st.set_page_config(page_title="Carfind.co.za AI Assistant", layout="wide")
|
14 |
st.markdown("<h1 style='text-align: center;'>🚗 Carfind.co.za AI Assistant</h1>", unsafe_allow_html=True)
|
15 |
st.caption("Chat with Carfind.co.za and find your next car fast")
|
16 |
|
|
|
17 |
st.markdown("""
|
18 |
<style>
|
19 |
.stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; }
|
|
|
22 |
</style>
|
23 |
""", unsafe_allow_html=True)
|
24 |
|
25 |
+
# Load existing transcript excel or create one
|
26 |
+
transcript_file = "transcripts.xlsx"
|
27 |
+
|
28 |
+
def append_to_transcript(thread_id, role, message):
|
29 |
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
30 |
+
new_entry = {
|
31 |
+
"Timestamp": timestamp,
|
32 |
+
"Thread_ID": thread_id,
|
33 |
+
"Role": role,
|
34 |
+
"Message": message
|
35 |
+
}
|
36 |
+
|
37 |
+
try:
|
38 |
+
if os.path.exists(transcript_file):
|
39 |
+
df = pd.read_excel(transcript_file)
|
40 |
+
df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
|
41 |
+
else:
|
42 |
+
df = pd.DataFrame([new_entry])
|
43 |
+
|
44 |
+
df.to_excel(transcript_file, index=False)
|
45 |
+
except Exception as e:
|
46 |
+
st.error(f"Failed to log conversation: {str(e)}")
|
47 |
+
|
48 |
if "authenticated" not in st.session_state:
|
49 |
st.session_state.authenticated = False
|
50 |
|
|
|
62 |
else:
|
63 |
st.error("Incorrect username or password. Please try again.")
|
64 |
|
|
|
65 |
else:
|
66 |
st.divider()
|
67 |
|
|
|
89 |
client.beta.threads.messages.create(
|
90 |
thread_id=st.session_state["thread_id"], role="user", content=user_input
|
91 |
)
|
92 |
+
append_to_transcript(st.session_state["thread_id"], "User", user_input)
|
93 |
|
94 |
try:
|
95 |
with st.spinner("Thinking and typing... 💭"):
|
|
|
109 |
thread_id=st.session_state["thread_id"]
|
110 |
)
|
111 |
|
|
|
|
|
|
|
112 |
assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='22' style='vertical-align:middle;'/>"
|
113 |
|
|
|
114 |
for msg in reversed(messages_response.data):
|
115 |
+
content = msg.content[0].text.value
|
116 |
if msg.role == "user":
|
117 |
st.markdown(
|
118 |
f"<div style='background-color:#f0f0f0; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
|
119 |
+
f"👤 <strong>You:</strong> {content}"
|
120 |
f"</div>",
|
121 |
unsafe_allow_html=True
|
122 |
)
|
123 |
else:
|
124 |
st.markdown(
|
125 |
f"<div style='background-color:#D6E9FE; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
|
126 |
+
f"{assistant_icon_html} <strong>Carfind Assistant:</strong> {content}"
|
127 |
f"</div>",
|
128 |
unsafe_allow_html=True
|
129 |
)
|
130 |
+
append_to_transcript(st.session_state["thread_id"], "Assistant", content)
|
131 |
|
132 |
except Exception as e:
|
133 |
st.error(f"An error occurred: {str(e)}")
|
134 |
+
|
135 |
else:
|
136 |
st.error("⚠️ OpenAI key or Assistant ID not found. Please ensure both are set as Hugging Face secrets.")
|