Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,18 +4,18 @@ import time
|
|
4 |
import os
|
5 |
import datetime
|
6 |
|
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 |
-
# Setup Streamlit
|
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 |
-
#
|
19 |
st.markdown("""
|
20 |
<style>
|
21 |
.stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; }
|
@@ -24,9 +24,17 @@ st.markdown("""
|
|
24 |
</style>
|
25 |
""", unsafe_allow_html=True)
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Authentication
|
32 |
if "authenticated" not in st.session_state:
|
@@ -44,14 +52,14 @@ if not st.session_state.authenticated:
|
|
44 |
time.sleep(1)
|
45 |
st.rerun()
|
46 |
else:
|
47 |
-
st.error("Incorrect username or password.")
|
48 |
|
|
|
49 |
else:
|
50 |
st.divider()
|
51 |
|
52 |
if "thread_id" not in st.session_state:
|
53 |
st.session_state["thread_id"] = None
|
54 |
-
st.session_state["chat_history"] = []
|
55 |
|
56 |
input_col, clear_col = st.columns([8, 1])
|
57 |
with input_col:
|
@@ -60,7 +68,6 @@ else:
|
|
60 |
with clear_col:
|
61 |
if st.button("๐๏ธ", help="Clear Chat"):
|
62 |
st.session_state["thread_id"] = None
|
63 |
-
st.session_state["chat_history"] = []
|
64 |
st.success("Chat cleared.")
|
65 |
st.rerun()
|
66 |
|
@@ -68,19 +75,16 @@ else:
|
|
68 |
client = OpenAI(api_key=openai_key)
|
69 |
|
70 |
if user_input:
|
71 |
-
# Start new thread if needed
|
72 |
if not st.session_state["thread_id"]:
|
73 |
thread = client.beta.threads.create()
|
74 |
st.session_state["thread_id"] = thread.id
|
75 |
|
76 |
-
# Add user message to OpenAI thread
|
77 |
client.beta.threads.messages.create(
|
78 |
thread_id=st.session_state["thread_id"], role="user", content=user_input
|
79 |
)
|
80 |
-
st.session_state["chat_history"].append(("user", user_input))
|
81 |
|
82 |
try:
|
83 |
-
with st.spinner("Thinking... ๐ญ"):
|
84 |
run = client.beta.threads.runs.create(
|
85 |
thread_id=st.session_state["thread_id"], assistant_id=assistant_id
|
86 |
)
|
@@ -93,40 +97,33 @@ else:
|
|
93 |
break
|
94 |
time.sleep(1)
|
95 |
|
96 |
-
# Get assistant response
|
97 |
messages_response = client.beta.threads.messages.list(
|
98 |
thread_id=st.session_state["thread_id"]
|
99 |
)
|
100 |
|
101 |
-
#
|
102 |
-
|
103 |
-
st.session_state["chat_history"].append(("assistant", assistant_reply))
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
except Exception as e:
|
112 |
st.error(f"An error occurred: {str(e)}")
|
113 |
-
|
114 |
-
# Assistant icon inline
|
115 |
-
assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='22' style='vertical-align:middle;'/>"
|
116 |
-
|
117 |
-
# Display chat history top-down
|
118 |
-
for role, content in st.session_state["chat_history"]:
|
119 |
-
if role == "user":
|
120 |
-
st.markdown(
|
121 |
-
f"<div style='background-color:#f0f0f0; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
|
122 |
-
f"๐ค <strong>You:</strong> {content}"
|
123 |
-
f"</div>", unsafe_allow_html=True
|
124 |
-
)
|
125 |
-
else:
|
126 |
-
st.markdown(
|
127 |
-
f"<div style='background-color:#D6E9FE; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
|
128 |
-
f"{assistant_icon_html} <strong>Carfind Assistant:</strong> {content}"
|
129 |
-
f"</div>", unsafe_allow_html=True
|
130 |
-
)
|
131 |
else:
|
132 |
-
st.error("โ ๏ธ OpenAI key or Assistant ID not found.
|
|
|
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 |
</style>
|
25 |
""", unsafe_allow_html=True)
|
26 |
|
27 |
+
# Transcript saving function
|
28 |
+
def save_transcript(thread_id, messages):
|
29 |
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
30 |
+
filename = f"transcript_{thread_id}_{timestamp}.txt"
|
31 |
+
with open(filename, "w", encoding="utf-8") as file:
|
32 |
+
file.write(f"Chat Transcript - Thread: {thread_id} - Date: {timestamp}\n")
|
33 |
+
file.write("------------------------------------------------------\n\n")
|
34 |
+
for msg in reversed(messages):
|
35 |
+
role = "User" if msg.role == "user" else "Assistant"
|
36 |
+
content = msg.content[0].text.value
|
37 |
+
file.write(f"{role}: {content}\n\n")
|
38 |
|
39 |
# Authentication
|
40 |
if "authenticated" not in st.session_state:
|
|
|
52 |
time.sleep(1)
|
53 |
st.rerun()
|
54 |
else:
|
55 |
+
st.error("Incorrect username or password. Please try again.")
|
56 |
|
57 |
+
# Main chat interface
|
58 |
else:
|
59 |
st.divider()
|
60 |
|
61 |
if "thread_id" not in st.session_state:
|
62 |
st.session_state["thread_id"] = None
|
|
|
63 |
|
64 |
input_col, clear_col = st.columns([8, 1])
|
65 |
with input_col:
|
|
|
68 |
with clear_col:
|
69 |
if st.button("๐๏ธ", help="Clear Chat"):
|
70 |
st.session_state["thread_id"] = None
|
|
|
71 |
st.success("Chat cleared.")
|
72 |
st.rerun()
|
73 |
|
|
|
75 |
client = OpenAI(api_key=openai_key)
|
76 |
|
77 |
if user_input:
|
|
|
78 |
if not st.session_state["thread_id"]:
|
79 |
thread = client.beta.threads.create()
|
80 |
st.session_state["thread_id"] = thread.id
|
81 |
|
|
|
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... ๐ญ"):
|
88 |
run = client.beta.threads.runs.create(
|
89 |
thread_id=st.session_state["thread_id"], assistant_id=assistant_id
|
90 |
)
|
|
|
97 |
break
|
98 |
time.sleep(1)
|
99 |
|
|
|
100 |
messages_response = client.beta.threads.messages.list(
|
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> {msg.content[0].text.value}"
|
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> {msg.content[0].text.value}"
|
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.")
|