Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from streamlit_chat import message
|
| 3 |
from openai import OpenAI
|
| 4 |
import time
|
| 5 |
import datetime
|
|
@@ -10,49 +9,14 @@ generated_user = os.getenv("User")
|
|
| 10 |
generated_password = os.getenv("Password")
|
| 11 |
openai_key = os.getenv("openai_key")
|
| 12 |
|
| 13 |
-
# Streamlit
|
| 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 |
-
#
|
| 19 |
-
if "theme" not in st.session_state:
|
| 20 |
-
st.session_state.theme = "dark"
|
| 21 |
-
|
| 22 |
-
toggle = st.toggle("Switch to Light Mode")
|
| 23 |
-
if toggle:
|
| 24 |
-
st.session_state.theme = "light"
|
| 25 |
-
else:
|
| 26 |
-
st.session_state.theme = "dark"
|
| 27 |
-
|
| 28 |
-
if st.session_state.theme == "light":
|
| 29 |
-
st.markdown(
|
| 30 |
-
"""
|
| 31 |
-
<style>
|
| 32 |
-
body { background-color: #f0f0f0; color: #000; }
|
| 33 |
-
</style>
|
| 34 |
-
""",
|
| 35 |
-
unsafe_allow_html=True
|
| 36 |
-
)
|
| 37 |
-
else:
|
| 38 |
-
st.markdown(
|
| 39 |
-
"""
|
| 40 |
-
<style>
|
| 41 |
-
body { background-color: #0e0e0e; color: #fff; }
|
| 42 |
-
</style>
|
| 43 |
-
""",
|
| 44 |
-
unsafe_allow_html=True
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
# Initialize authentication and state
|
| 48 |
if "authenticated" not in st.session_state:
|
| 49 |
st.session_state.authenticated = False
|
| 50 |
-
if "messages" not in st.session_state:
|
| 51 |
-
st.session_state["messages"] = []
|
| 52 |
-
if "last_user_input" not in st.session_state:
|
| 53 |
-
st.session_state.last_user_input = ""
|
| 54 |
-
if "last_assistant_response" not in st.session_state:
|
| 55 |
-
st.session_state.last_assistant_response = ""
|
| 56 |
|
| 57 |
# Login screen
|
| 58 |
if not st.session_state.authenticated:
|
|
@@ -69,88 +33,82 @@ if not st.session_state.authenticated:
|
|
| 69 |
else:
|
| 70 |
st.error("Incorrect username or password. Please try again.")
|
| 71 |
|
| 72 |
-
# Main chat
|
| 73 |
else:
|
| 74 |
st.divider()
|
| 75 |
|
|
|
|
|
|
|
|
|
|
| 76 |
# Display chat history
|
| 77 |
-
for
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
else:
|
| 81 |
-
|
| 82 |
-
msg["content"],
|
| 83 |
-
is_user=True,
|
| 84 |
-
key=str(i),
|
| 85 |
-
avatar_style="adventurer",
|
| 86 |
-
seed="user-seed"
|
| 87 |
-
)
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
with col_input:
|
| 92 |
-
user_input = st.text_input("Type your message here:", key="input")
|
| 93 |
|
| 94 |
with col_clear:
|
| 95 |
-
if st.button("
|
| 96 |
st.session_state.messages = []
|
| 97 |
-
st.
|
| 98 |
-
st.session_state.last_assistant_response = ""
|
| 99 |
-
st.rerun()
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
log.write(f"
|
| 113 |
-
|
| 114 |
-
log.write(f"{msg['role'].capitalize()}: {msg['content']}\n")
|
| 115 |
-
log.write("--- End Chat ---\n")
|
| 116 |
-
|
| 117 |
-
try:
|
| 118 |
-
thread = client.beta.threads.create()
|
| 119 |
-
thread_id = thread.id
|
| 120 |
-
|
| 121 |
-
client.beta.threads.messages.create(
|
| 122 |
-
thread_id=thread_id,
|
| 123 |
-
role="user",
|
| 124 |
-
content=user_input
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
run = client.beta.threads.runs.create(
|
| 128 |
-
thread_id=thread_id,
|
| 129 |
-
assistant_id=ASSISTANT_ID
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
while True:
|
| 133 |
-
run_status = client.beta.threads.runs.retrieve(
|
| 134 |
-
thread_id=thread_id,
|
| 135 |
-
run_id=run.id
|
| 136 |
-
)
|
| 137 |
-
if run_status.status == "completed":
|
| 138 |
-
break
|
| 139 |
-
time.sleep(1)
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
st.session_state.last_assistant_response = assistant_reply
|
| 148 |
-
save_transcript(st.session_state.messages)
|
| 149 |
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
st.error("OpenAI key not found. Please ensure it is set as a Hugging Face secret.")
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import time
|
| 4 |
import datetime
|
|
|
|
| 9 |
generated_password = os.getenv("Password")
|
| 10 |
openai_key = os.getenv("openai_key")
|
| 11 |
|
| 12 |
+
# Streamlit app configuration
|
| 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 |
+
# Initialize authentication state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
if "authenticated" not in st.session_state:
|
| 19 |
st.session_state.authenticated = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Login screen
|
| 22 |
if not st.session_state.authenticated:
|
|
|
|
| 33 |
else:
|
| 34 |
st.error("Incorrect username or password. Please try again.")
|
| 35 |
|
| 36 |
+
# Main chat interface
|
| 37 |
else:
|
| 38 |
st.divider()
|
| 39 |
|
| 40 |
+
if "messages" not in st.session_state:
|
| 41 |
+
st.session_state["messages"] = []
|
| 42 |
+
|
| 43 |
# Display chat history
|
| 44 |
+
for message in st.session_state.messages:
|
| 45 |
+
role = message["role"]
|
| 46 |
+
content = message["content"]
|
| 47 |
+
if role == "assistant":
|
| 48 |
+
st.markdown(f'<img src="https://www.carfind.co.za/images/Carfind-Icon.svg" width="30" style="vertical-align: middle;"> <strong>Carfind Assistant:</strong> {content}', unsafe_allow_html=True)
|
| 49 |
else:
|
| 50 |
+
st.chat_message(role).write(content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# Input with clear chat button
|
| 53 |
+
col_clear, col_input = st.columns([1, 8])
|
|
|
|
|
|
|
| 54 |
|
| 55 |
with col_clear:
|
| 56 |
+
if st.button("🧹 Clear Chat"):
|
| 57 |
st.session_state.messages = []
|
| 58 |
+
st.success("Chat history cleared.")
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
with col_input:
|
| 61 |
+
user_input = st.chat_input("Type your message here...")
|
| 62 |
+
|
| 63 |
+
if openai_key:
|
| 64 |
+
client = OpenAI(api_key=openai_key)
|
| 65 |
+
ASSISTANT_ID = "asst_5gQR21fOsmHil11FGBzEArA7"
|
| 66 |
+
|
| 67 |
+
def save_transcript(messages):
|
| 68 |
+
with open("chat_logs.txt", "a") as log:
|
| 69 |
+
log.write(f"\n--- New Chat ({datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}) ---\n")
|
| 70 |
+
for msg in messages:
|
| 71 |
+
log.write(f"{msg['role'].capitalize()}: {msg['content']}\n")
|
| 72 |
+
log.write("--- End Chat ---\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
if user_input:
|
| 75 |
+
if len(st.session_state.messages) == 0 or user_input != st.session_state.messages[-1]["content"]:
|
| 76 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 77 |
+
st.chat_message("user").write(user_input)
|
| 78 |
|
| 79 |
+
try:
|
| 80 |
+
thread = client.beta.threads.create()
|
| 81 |
+
thread_id = thread.id
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
client.beta.threads.messages.create(
|
| 84 |
+
thread_id=thread_id,
|
| 85 |
+
role="user",
|
| 86 |
+
content=user_input
|
| 87 |
+
)
|
| 88 |
|
| 89 |
+
run = client.beta.threads.runs.create(
|
| 90 |
+
thread_id=thread_id,
|
| 91 |
+
assistant_id=ASSISTANT_ID
|
| 92 |
+
)
|
| 93 |
|
| 94 |
+
while True:
|
| 95 |
+
run_status = client.beta.threads.runs.retrieve(
|
| 96 |
+
thread_id=thread_id,
|
| 97 |
+
run_id=run.id
|
| 98 |
+
)
|
| 99 |
+
if run_status.status == "completed":
|
| 100 |
+
break
|
| 101 |
+
time.sleep(1)
|
| 102 |
+
|
| 103 |
+
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
| 104 |
+
assistant_message = messages.data[0].content[0].text.value.strip()
|
| 105 |
+
|
| 106 |
+
if assistant_message.lower() != user_input.lower():
|
| 107 |
+
st.markdown(f'<img src="https://www.carfind.co.za/images/Carfind-Icon.svg" width="30" style="vertical-align: middle;"> <strong>Carfind Assistant:</strong> {assistant_message}', unsafe_allow_html=True)
|
| 108 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
| 109 |
+
save_transcript(st.session_state.messages)
|
| 110 |
+
|
| 111 |
+
except Exception as e:
|
| 112 |
+
st.error(f"An error occurred: {str(e)}")
|
| 113 |
+
else:
|
| 114 |
st.error("OpenAI key not found. Please ensure it is set as a Hugging Face secret.")
|