Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import streamlit as st
|
2 |
from openai import OpenAI
|
3 |
-
from PIL import Image
|
4 |
import time
|
5 |
import os
|
6 |
import uuid
|
@@ -47,7 +46,7 @@ st.markdown("""
|
|
47 |
</div>
|
48 |
""", unsafe_allow_html=True)
|
49 |
|
50 |
-
#
|
51 |
def get_or_create_thread_id():
|
52 |
doc_ref = db.collection("users").document(user_id)
|
53 |
doc = doc_ref.get()
|
@@ -58,7 +57,7 @@ def get_or_create_thread_id():
|
|
58 |
doc_ref.set({"thread_id": thread.id, "created_at": firestore.SERVER_TIMESTAMP})
|
59 |
return thread.id
|
60 |
|
61 |
-
# Save message
|
62 |
def save_message(role, content):
|
63 |
db.collection("users").document(user_id).collection("messages").add({
|
64 |
"role": role,
|
@@ -66,7 +65,7 @@ def save_message(role, content):
|
|
66 |
"timestamp": firestore.SERVER_TIMESTAMP
|
67 |
})
|
68 |
|
69 |
-
# Display chat history
|
70 |
def display_chat_history():
|
71 |
messages = db.collection("users").document(user_id).collection("messages").order_by("timestamp").stream()
|
72 |
assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='20' style='vertical-align:middle;'/>"
|
@@ -77,10 +76,11 @@ def display_chat_history():
|
|
77 |
else:
|
78 |
st.markdown(f"<div class='stChatMessage' data-testid='stChatMessage-assistant'>{assistant_icon_html} <strong>Carfind Assistant:</strong> {data['content']}</div>", unsafe_allow_html=True)
|
79 |
|
80 |
-
# π Main
|
81 |
input_col, clear_col = st.columns([9, 1])
|
82 |
with input_col:
|
83 |
user_input = st.chat_input("Type your message here...")
|
|
|
84 |
with clear_col:
|
85 |
if st.button("ποΈ", key="clear-chat", help="Clear Chat"):
|
86 |
try:
|
@@ -99,6 +99,7 @@ display_chat_history()
|
|
99 |
if user_input:
|
100 |
client.beta.threads.messages.create(thread_id=thread_id, role="user", content=user_input)
|
101 |
save_message("user", user_input)
|
|
|
102 |
with st.spinner("Thinking and typing... π"):
|
103 |
run = client.beta.threads.runs.create(thread_id=thread_id, assistant_id=assistant_id)
|
104 |
while True:
|
@@ -106,9 +107,11 @@ if user_input:
|
|
106 |
if run_status.status == "completed":
|
107 |
break
|
108 |
time.sleep(1)
|
|
|
109 |
messages_response = client.beta.threads.messages.list(thread_id=thread_id)
|
110 |
latest_response = sorted(messages_response.data, key=lambda x: x.created_at)[-1]
|
111 |
assistant_message = latest_response.content[0].text.value
|
|
|
112 |
save_message("assistant", assistant_message)
|
113 |
time.sleep(0.5)
|
114 |
st.rerun()
|
|
|
1 |
import streamlit as st
|
2 |
from openai import OpenAI
|
|
|
3 |
import time
|
4 |
import os
|
5 |
import uuid
|
|
|
46 |
</div>
|
47 |
""", unsafe_allow_html=True)
|
48 |
|
49 |
+
# π Get or create a thread ID
|
50 |
def get_or_create_thread_id():
|
51 |
doc_ref = db.collection("users").document(user_id)
|
52 |
doc = doc_ref.get()
|
|
|
57 |
doc_ref.set({"thread_id": thread.id, "created_at": firestore.SERVER_TIMESTAMP})
|
58 |
return thread.id
|
59 |
|
60 |
+
# πΎ Save a message
|
61 |
def save_message(role, content):
|
62 |
db.collection("users").document(user_id).collection("messages").add({
|
63 |
"role": role,
|
|
|
65 |
"timestamp": firestore.SERVER_TIMESTAMP
|
66 |
})
|
67 |
|
68 |
+
# π¬ Display chat history
|
69 |
def display_chat_history():
|
70 |
messages = db.collection("users").document(user_id).collection("messages").order_by("timestamp").stream()
|
71 |
assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='20' style='vertical-align:middle;'/>"
|
|
|
76 |
else:
|
77 |
st.markdown(f"<div class='stChatMessage' data-testid='stChatMessage-assistant'>{assistant_icon_html} <strong>Carfind Assistant:</strong> {data['content']}</div>", unsafe_allow_html=True)
|
78 |
|
79 |
+
# π Main Chat UI
|
80 |
input_col, clear_col = st.columns([9, 1])
|
81 |
with input_col:
|
82 |
user_input = st.chat_input("Type your message here...")
|
83 |
+
|
84 |
with clear_col:
|
85 |
if st.button("ποΈ", key="clear-chat", help="Clear Chat"):
|
86 |
try:
|
|
|
99 |
if user_input:
|
100 |
client.beta.threads.messages.create(thread_id=thread_id, role="user", content=user_input)
|
101 |
save_message("user", user_input)
|
102 |
+
|
103 |
with st.spinner("Thinking and typing... π"):
|
104 |
run = client.beta.threads.runs.create(thread_id=thread_id, assistant_id=assistant_id)
|
105 |
while True:
|
|
|
107 |
if run_status.status == "completed":
|
108 |
break
|
109 |
time.sleep(1)
|
110 |
+
|
111 |
messages_response = client.beta.threads.messages.list(thread_id=thread_id)
|
112 |
latest_response = sorted(messages_response.data, key=lambda x: x.created_at)[-1]
|
113 |
assistant_message = latest_response.content[0].text.value
|
114 |
+
|
115 |
save_message("assistant", assistant_message)
|
116 |
time.sleep(0.5)
|
117 |
st.rerun()
|