Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ 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;'
|
| 15 |
st.caption("Chat with Carfind.co.za and find your next car fast")
|
| 16 |
|
| 17 |
dark_mode = st.toggle("🌙 Switch to Light Mode", value=True)
|
|
@@ -61,19 +61,19 @@ else:
|
|
| 61 |
client = OpenAI(api_key=openai_key)
|
| 62 |
|
| 63 |
if user_input:
|
| 64 |
-
# Create thread
|
| 65 |
if not st.session_state["thread_id"]:
|
| 66 |
thread = client.beta.threads.create()
|
| 67 |
st.session_state["thread_id"] = thread.id
|
| 68 |
|
| 69 |
-
# Add user message to thread
|
| 70 |
client.beta.threads.messages.create(
|
| 71 |
thread_id=st.session_state["thread_id"], role="user", content=user_input
|
| 72 |
)
|
| 73 |
|
| 74 |
try:
|
| 75 |
with st.spinner("Thinking and typing... 💭"):
|
| 76 |
-
#
|
| 77 |
run = client.beta.threads.runs.create(
|
| 78 |
thread_id=st.session_state["thread_id"], assistant_id=assistant_id
|
| 79 |
)
|
|
@@ -86,17 +86,26 @@ else:
|
|
| 86 |
break
|
| 87 |
time.sleep(1)
|
| 88 |
|
| 89 |
-
# ✅
|
| 90 |
messages_response = client.beta.threads.messages.list(
|
| 91 |
thread_id=st.session_state["thread_id"]
|
| 92 |
)
|
| 93 |
|
| 94 |
-
#
|
|
|
|
|
|
|
|
|
|
| 95 |
for msg in reversed(messages_response.data):
|
| 96 |
if msg.role == "user":
|
| 97 |
message(msg.content[0].text.value, is_user=True, avatar_style="adventurer")
|
| 98 |
else:
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
except Exception as e:
|
| 102 |
st.error(f"An error occurred: {str(e)}")
|
|
|
|
| 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 |
dark_mode = st.toggle("🌙 Switch to Light Mode", value=True)
|
|
|
|
| 61 |
client = OpenAI(api_key=openai_key)
|
| 62 |
|
| 63 |
if user_input:
|
| 64 |
+
# Create thread if it doesn't exist
|
| 65 |
if not st.session_state["thread_id"]:
|
| 66 |
thread = client.beta.threads.create()
|
| 67 |
st.session_state["thread_id"] = thread.id
|
| 68 |
|
| 69 |
+
# Add user message to the thread
|
| 70 |
client.beta.threads.messages.create(
|
| 71 |
thread_id=st.session_state["thread_id"], role="user", content=user_input
|
| 72 |
)
|
| 73 |
|
| 74 |
try:
|
| 75 |
with st.spinner("Thinking and typing... 💭"):
|
| 76 |
+
# Trigger assistant run
|
| 77 |
run = client.beta.threads.runs.create(
|
| 78 |
thread_id=st.session_state["thread_id"], assistant_id=assistant_id
|
| 79 |
)
|
|
|
|
| 86 |
break
|
| 87 |
time.sleep(1)
|
| 88 |
|
| 89 |
+
# ✅ Retrieve entire conversation history
|
| 90 |
messages_response = client.beta.threads.messages.list(
|
| 91 |
thread_id=st.session_state["thread_id"]
|
| 92 |
)
|
| 93 |
|
| 94 |
+
# Inline assistant icon HTML
|
| 95 |
+
assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='22' style='vertical-align:middle;'/>"
|
| 96 |
+
|
| 97 |
+
# Display chat history in correct order
|
| 98 |
for msg in reversed(messages_response.data):
|
| 99 |
if msg.role == "user":
|
| 100 |
message(msg.content[0].text.value, is_user=True, avatar_style="adventurer")
|
| 101 |
else:
|
| 102 |
+
# Use markdown manually to render inline icon and text
|
| 103 |
+
st.markdown(
|
| 104 |
+
f"<div style='background-color:#00529B; color:white; padding:10px; border-radius:8px; margin:5px 0;'>"
|
| 105 |
+
f"{assistant_icon_html} <strong>Carfind Assistant:</strong> {msg.content[0].text.value}"
|
| 106 |
+
f"</div>",
|
| 107 |
+
unsafe_allow_html=True
|
| 108 |
+
)
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
st.error(f"An error occurred: {str(e)}")
|