Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
from openai import OpenAI
|
3 |
import time
|
4 |
import datetime
|
@@ -9,46 +10,11 @@ generated_user = os.getenv("User")
|
|
9 |
generated_password = os.getenv("Password")
|
10 |
openai_key = os.getenv("openai_key")
|
11 |
|
12 |
-
# Streamlit
|
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 |
-
# Custom CSS for styling chat input and clear button inside it
|
18 |
-
st.markdown(
|
19 |
-
"""
|
20 |
-
<style>
|
21 |
-
.chat-container {
|
22 |
-
overflow-y: auto;
|
23 |
-
max-height: 70vh;
|
24 |
-
padding: 10px;
|
25 |
-
border-radius: 10px;
|
26 |
-
border: 1px solid #444;
|
27 |
-
background-color: #111;
|
28 |
-
margin-bottom: 80px;
|
29 |
-
}
|
30 |
-
.stChatInputContainer {
|
31 |
-
position: fixed;
|
32 |
-
bottom: 20px;
|
33 |
-
width: 80%;
|
34 |
-
left: 10%;
|
35 |
-
background-color: #222;
|
36 |
-
padding: 10px;
|
37 |
-
border-radius: 10px;
|
38 |
-
box-shadow: 0 0 15px rgba(0,0,0,0.5);
|
39 |
-
display: flex;
|
40 |
-
align-items: center;
|
41 |
-
}
|
42 |
-
.clear-chat-icon {
|
43 |
-
cursor: pointer;
|
44 |
-
margin-left: 10px;
|
45 |
-
font-size: 1.4em;
|
46 |
-
}
|
47 |
-
</style>
|
48 |
-
""",
|
49 |
-
unsafe_allow_html=True
|
50 |
-
)
|
51 |
-
|
52 |
# Initialize authentication state
|
53 |
if "authenticated" not in st.session_state:
|
54 |
st.session_state.authenticated = False
|
@@ -68,45 +34,32 @@ if not st.session_state.authenticated:
|
|
68 |
else:
|
69 |
st.error("Incorrect username or password. Please try again.")
|
70 |
|
71 |
-
# Main chat
|
72 |
else:
|
73 |
-
st.divider()
|
74 |
-
|
75 |
if "messages" not in st.session_state:
|
76 |
st.session_state["messages"] = []
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
with
|
88 |
-
user_input = st.
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
</script>
|
100 |
-
<span class='clear-chat-icon' onclick='clearChat()'>🗑️</span>
|
101 |
-
""",
|
102 |
-
unsafe_allow_html=True
|
103 |
-
)
|
104 |
-
|
105 |
-
# Listen for clear chat POST
|
106 |
-
if st.experimental_get_query_params().get("clear_chat"):
|
107 |
-
st.session_state.messages = []
|
108 |
-
|
109 |
-
if openai_key:
|
110 |
client = OpenAI(api_key=openai_key)
|
111 |
ASSISTANT_ID = "asst_5gQR21fOsmHil11FGBzEArA7"
|
112 |
|
@@ -117,47 +70,40 @@ else:
|
|
117 |
log.write(f"{msg['role'].capitalize()}: {msg['content']}\n")
|
118 |
log.write("--- End Chat ---\n")
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
st.markdown(user_input)
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
128 |
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
)
|
134 |
|
135 |
-
|
|
|
136 |
thread_id=thread_id,
|
137 |
-
|
138 |
)
|
|
|
|
|
|
|
139 |
|
140 |
-
|
141 |
-
|
142 |
-
thread_id=thread_id,
|
143 |
-
run_id=run.id
|
144 |
-
)
|
145 |
-
if run_status.status == "completed":
|
146 |
-
break
|
147 |
-
time.sleep(1)
|
148 |
-
|
149 |
-
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
150 |
-
assistant_message = messages.data[0].content[0].text.value
|
151 |
-
|
152 |
-
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
153 |
|
154 |
-
|
155 |
-
|
156 |
|
157 |
-
|
158 |
|
159 |
-
|
160 |
-
|
161 |
|
162 |
-
|
163 |
st.error("OpenAI key not found. Please ensure it is set as a Hugging Face secret.")
|
|
|
1 |
import streamlit as st
|
2 |
+
from streamlit_chat import message
|
3 |
from openai import OpenAI
|
4 |
import time
|
5 |
import datetime
|
|
|
10 |
generated_password = os.getenv("Password")
|
11 |
openai_key = os.getenv("openai_key")
|
12 |
|
13 |
+
# Streamlit app configuration
|
14 |
st.set_page_config(page_title="Carfind.co.za AI Assistant", layout="wide")
|
15 |
st.title("🚗 Carfind.co.za AI Assistant")
|
16 |
st.caption("Chat with Carfind.co.za and find your next car fast")
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Initialize authentication state
|
19 |
if "authenticated" not in st.session_state:
|
20 |
st.session_state.authenticated = False
|
|
|
34 |
else:
|
35 |
st.error("Incorrect username or password. Please try again.")
|
36 |
|
37 |
+
# Main chat UI using streamlit-chat
|
38 |
else:
|
|
|
|
|
39 |
if "messages" not in st.session_state:
|
40 |
st.session_state["messages"] = []
|
41 |
|
42 |
+
st.divider()
|
43 |
+
|
44 |
+
# Display chat history
|
45 |
+
for i, msg in enumerate(st.session_state["messages"]):
|
46 |
+
message(msg["content"], is_user=(msg["role"] == "user"), key=str(i))
|
47 |
+
|
48 |
+
# Chat input with clear chat icon
|
49 |
+
col_input, col_clear = st.columns([10, 1])
|
50 |
+
|
51 |
+
with col_input:
|
52 |
+
user_input = st.text_input("Type your message here:", key="input")
|
53 |
+
|
54 |
+
with col_clear:
|
55 |
+
if st.button("🗑️", help="Clear chat"):
|
56 |
+
st.session_state.messages = []
|
57 |
+
st.experimental_rerun()
|
58 |
+
|
59 |
+
# Handle OpenAI assistant
|
60 |
+
if user_input and openai_key:
|
61 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
62 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
client = OpenAI(api_key=openai_key)
|
64 |
ASSISTANT_ID = "asst_5gQR21fOsmHil11FGBzEArA7"
|
65 |
|
|
|
70 |
log.write(f"{msg['role'].capitalize()}: {msg['content']}\n")
|
71 |
log.write("--- End Chat ---\n")
|
72 |
|
73 |
+
try:
|
74 |
+
thread = client.beta.threads.create()
|
75 |
+
thread_id = thread.id
|
|
|
76 |
|
77 |
+
client.beta.threads.messages.create(
|
78 |
+
thread_id=thread_id,
|
79 |
+
role="user",
|
80 |
+
content=user_input
|
81 |
+
)
|
82 |
|
83 |
+
run = client.beta.threads.runs.create(
|
84 |
+
thread_id=thread_id,
|
85 |
+
assistant_id=ASSISTANT_ID
|
86 |
+
)
|
|
|
87 |
|
88 |
+
while True:
|
89 |
+
run_status = client.beta.threads.runs.retrieve(
|
90 |
thread_id=thread_id,
|
91 |
+
run_id=run.id
|
92 |
)
|
93 |
+
if run_status.status == "completed":
|
94 |
+
break
|
95 |
+
time.sleep(1)
|
96 |
|
97 |
+
messages_response = client.beta.threads.messages.list(thread_id=thread_id)
|
98 |
+
assistant_reply = messages_response.data[0].content[0].text.value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_reply})
|
101 |
+
save_transcript(st.session_state.messages)
|
102 |
|
103 |
+
st.experimental_rerun()
|
104 |
|
105 |
+
except Exception as e:
|
106 |
+
st.error(f"An error occurred: {str(e)}")
|
107 |
|
108 |
+
elif not openai_key:
|
109 |
st.error("OpenAI key not found. Please ensure it is set as a Hugging Face secret.")
|