Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,20 +32,21 @@ st.set_page_config(page_title="Carfind.co.za AI Assistant", layout="wide")
|
|
| 32 |
|
| 33 |
st.markdown("""
|
| 34 |
<style>
|
|
|
|
|
|
|
|
|
|
| 35 |
.stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; }
|
| 36 |
.stChatMessage[data-testid="stChatMessage-user"] { background: #f0f0f0; color: #000000; }
|
| 37 |
.stChatMessage[data-testid="stChatMessage-assistant"] { background: #D6E9FE; color: #000000; }
|
| 38 |
</style>
|
| 39 |
""", unsafe_allow_html=True)
|
| 40 |
|
| 41 |
-
st.divider()
|
| 42 |
-
|
| 43 |
if "thread_id" not in st.session_state:
|
| 44 |
st.session_state["thread_id"] = None
|
| 45 |
|
| 46 |
input_col, clear_col = st.columns([8, 1])
|
| 47 |
with input_col:
|
| 48 |
-
user_input = st.text_input("Type your message here...", key="chat_input")
|
| 49 |
|
| 50 |
with clear_col:
|
| 51 |
if st.button("🗑️", help="Clear Chat"):
|
|
@@ -57,12 +58,10 @@ if openai_key and assistant_id:
|
|
| 57 |
client = OpenAI(api_key=openai_key)
|
| 58 |
|
| 59 |
if user_input:
|
| 60 |
-
# Create thread if not exists
|
| 61 |
if not st.session_state["thread_id"]:
|
| 62 |
thread = client.beta.threads.create()
|
| 63 |
st.session_state["thread_id"] = thread.id
|
| 64 |
|
| 65 |
-
# Add user message
|
| 66 |
client.beta.threads.messages.create(
|
| 67 |
thread_id=st.session_state["thread_id"], role="user", content=user_input
|
| 68 |
)
|
|
@@ -81,15 +80,13 @@ if openai_key and assistant_id:
|
|
| 81 |
break
|
| 82 |
time.sleep(1)
|
| 83 |
|
| 84 |
-
# Get full conversation
|
| 85 |
messages_response = client.beta.threads.messages.list(
|
| 86 |
thread_id=st.session_state["thread_id"]
|
| 87 |
)
|
| 88 |
|
| 89 |
assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='22' style='vertical-align:middle;'/>"
|
| 90 |
|
| 91 |
-
|
| 92 |
-
messages_sorted = sorted(messages_response.data, key=lambda x: x.created_at, reverse=True)
|
| 93 |
for msg in messages_sorted:
|
| 94 |
if msg.role == "user":
|
| 95 |
st.markdown(
|
|
@@ -108,16 +105,7 @@ if openai_key and assistant_id:
|
|
| 108 |
unsafe_allow_html=True
|
| 109 |
)
|
| 110 |
|
| 111 |
-
# Scroll to top automatically
|
| 112 |
-
st.markdown("""
|
| 113 |
-
<script>
|
| 114 |
-
window.scrollTo(0, 0);
|
| 115 |
-
</script>
|
| 116 |
-
""", unsafe_allow_html=True)
|
| 117 |
-
|
| 118 |
except Exception as e:
|
| 119 |
st.error(f"An error occurred: {str(e)}")
|
| 120 |
else:
|
| 121 |
st.error("⚠️ OpenAI key or Assistant ID not found. Please ensure both are set as Hugging Face secrets.")
|
| 122 |
-
|
| 123 |
-
|
|
|
|
| 32 |
|
| 33 |
st.markdown("""
|
| 34 |
<style>
|
| 35 |
+
.block-container {padding-top: 1rem; padding-bottom: 0rem;}
|
| 36 |
+
header {visibility: hidden;}
|
| 37 |
+
.st-emotion-cache-18ni7ap {visibility: hidden;}
|
| 38 |
.stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; }
|
| 39 |
.stChatMessage[data-testid="stChatMessage-user"] { background: #f0f0f0; color: #000000; }
|
| 40 |
.stChatMessage[data-testid="stChatMessage-assistant"] { background: #D6E9FE; color: #000000; }
|
| 41 |
</style>
|
| 42 |
""", unsafe_allow_html=True)
|
| 43 |
|
|
|
|
|
|
|
| 44 |
if "thread_id" not in st.session_state:
|
| 45 |
st.session_state["thread_id"] = None
|
| 46 |
|
| 47 |
input_col, clear_col = st.columns([8, 1])
|
| 48 |
with input_col:
|
| 49 |
+
user_input = st.text_input("", placeholder="Type your message here...", key="chat_input")
|
| 50 |
|
| 51 |
with clear_col:
|
| 52 |
if st.button("🗑️", help="Clear Chat"):
|
|
|
|
| 58 |
client = OpenAI(api_key=openai_key)
|
| 59 |
|
| 60 |
if user_input:
|
|
|
|
| 61 |
if not st.session_state["thread_id"]:
|
| 62 |
thread = client.beta.threads.create()
|
| 63 |
st.session_state["thread_id"] = thread.id
|
| 64 |
|
|
|
|
| 65 |
client.beta.threads.messages.create(
|
| 66 |
thread_id=st.session_state["thread_id"], role="user", content=user_input
|
| 67 |
)
|
|
|
|
| 80 |
break
|
| 81 |
time.sleep(1)
|
| 82 |
|
|
|
|
| 83 |
messages_response = client.beta.threads.messages.list(
|
| 84 |
thread_id=st.session_state["thread_id"]
|
| 85 |
)
|
| 86 |
|
| 87 |
assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='22' style='vertical-align:middle;'/>"
|
| 88 |
|
| 89 |
+
messages_sorted = sorted(messages_response.data, key=lambda x: x.created_at, reverse=False)
|
|
|
|
| 90 |
for msg in messages_sorted:
|
| 91 |
if msg.role == "user":
|
| 92 |
st.markdown(
|
|
|
|
| 105 |
unsafe_allow_html=True
|
| 106 |
)
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
except Exception as e:
|
| 109 |
st.error(f"An error occurred: {str(e)}")
|
| 110 |
else:
|
| 111 |
st.error("⚠️ OpenAI key or Assistant ID not found. Please ensure both are set as Hugging Face secrets.")
|
|
|
|
|
|