Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,21 +5,21 @@ import time
|
|
5 |
import datetime
|
6 |
import os
|
7 |
|
8 |
-
# Load Hugging Face
|
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.markdown("<h1 style='text-align: center;'>π Carfind.co.za AI Assistant</h1>", unsafe_allow_html=True)
|
17 |
st.caption("Chat with Carfind.co.za and find your next car fast")
|
18 |
|
19 |
-
# Light/Dark mode toggle
|
20 |
dark_mode = st.toggle("π Switch to Light Mode", value=True)
|
21 |
|
22 |
-
# Custom
|
23 |
st.markdown("""
|
24 |
<style>
|
25 |
.stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; }
|
@@ -46,21 +46,21 @@ 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 msg in st.session_state["messages"]:
|
58 |
if msg["role"] == "assistant":
|
59 |
-
message(f"
|
60 |
else:
|
61 |
message(msg["content"], is_user=True, avatar_style="adventurer")
|
62 |
|
63 |
-
# Input
|
64 |
input_col, clear_col = st.columns([8, 1])
|
65 |
with input_col:
|
66 |
user_input = st.text_input("Type your message here...", key="chat_input")
|
@@ -71,7 +71,6 @@ else:
|
|
71 |
st.success("Chat cleared.")
|
72 |
st.rerun()
|
73 |
|
74 |
-
# Handle chat
|
75 |
if openai_key and assistant_id:
|
76 |
client = OpenAI(api_key=openai_key)
|
77 |
|
@@ -99,12 +98,10 @@ else:
|
|
99 |
break
|
100 |
time.sleep(1)
|
101 |
|
102 |
-
|
103 |
-
response_messages = client.beta.threads.messages.list(thread_id=thread.id)
|
104 |
-
assistant_reply = response_messages.data[0].content[0].text.value
|
105 |
|
106 |
st.session_state.messages.append({"role": "assistant", "content": assistant_reply})
|
107 |
-
message(f"
|
108 |
|
109 |
save_transcript(st.session_state.messages)
|
110 |
|
|
|
5 |
import datetime
|
6 |
import os
|
7 |
|
8 |
+
# Load secrets from Hugging Face environment
|
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 page setup
|
15 |
st.set_page_config(page_title="Carfind.co.za AI Assistant", layout="wide")
|
16 |
st.markdown("<h1 style='text-align: center;'>π Carfind.co.za AI Assistant</h1>", unsafe_allow_html=True)
|
17 |
st.caption("Chat with Carfind.co.za and find your next car fast")
|
18 |
|
19 |
+
# Light / Dark mode toggle
|
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; }
|
|
|
46 |
else:
|
47 |
st.error("Incorrect username or password. Please try again.")
|
48 |
|
49 |
+
# Chat UI after login
|
50 |
else:
|
51 |
st.divider()
|
52 |
|
53 |
if "messages" not in st.session_state:
|
54 |
st.session_state["messages"] = []
|
55 |
|
56 |
+
# Show chat history
|
57 |
for msg in st.session_state["messages"]:
|
58 |
if msg["role"] == "assistant":
|
59 |
+
message(f"π **Carfind Assistant:** {msg['content']}", is_user=False)
|
60 |
else:
|
61 |
message(msg["content"], is_user=True, avatar_style="adventurer")
|
62 |
|
63 |
+
# Input and Clear Chat button
|
64 |
input_col, clear_col = st.columns([8, 1])
|
65 |
with input_col:
|
66 |
user_input = st.text_input("Type your message here...", key="chat_input")
|
|
|
71 |
st.success("Chat cleared.")
|
72 |
st.rerun()
|
73 |
|
|
|
74 |
if openai_key and assistant_id:
|
75 |
client = OpenAI(api_key=openai_key)
|
76 |
|
|
|
98 |
break
|
99 |
time.sleep(1)
|
100 |
|
101 |
+
assistant_reply = client.beta.threads.messages.list(thread_id=thread.id).data[0].content[0].text.value
|
|
|
|
|
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 |
|