Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,117 +1,119 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
"
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
#
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import time
|
| 4 |
+
import os
|
| 5 |
+
import uuid
|
| 6 |
+
import firebase_admin
|
| 7 |
+
from firebase_admin import credentials, firestore
|
| 8 |
+
|
| 9 |
+
# π Firebase setup
|
| 10 |
+
if not firebase_admin._apps:
|
| 11 |
+
cred = credentials.Certificate("firebase-service-account.json")
|
| 12 |
+
firebase_admin.initialize_app(cred)
|
| 13 |
+
|
| 14 |
+
db = firestore.client()
|
| 15 |
+
|
| 16 |
+
# π OpenAI setup
|
| 17 |
+
openai_key = os.getenv("openai_key")
|
| 18 |
+
assistant_id = os.getenv("ASSISTANT_ID")
|
| 19 |
+
client = OpenAI(api_key=openai_key)
|
| 20 |
+
|
| 21 |
+
# π Streamlit Config
|
| 22 |
+
st.set_page_config(page_title="LeaseTracker AI Assistant", layout="wide")
|
| 23 |
+
|
| 24 |
+
# π― Session + User ID
|
| 25 |
+
if "user_id" not in st.session_state:
|
| 26 |
+
st.session_state["user_id"] = str(uuid.uuid4())
|
| 27 |
+
user_id = st.session_state["user_id"]
|
| 28 |
+
|
| 29 |
+
# πΌοΈ LeaseTracker Branding + Styling
|
| 30 |
+
st.markdown("""
|
| 31 |
+
<style>
|
| 32 |
+
.block-container {padding-top: 1rem; padding-bottom: 0rem;}
|
| 33 |
+
header {visibility: hidden;}
|
| 34 |
+
.stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; margin-bottom: 10px; }
|
| 35 |
+
.stChatMessage[data-testid="stChatMessage-user"] { background: #f0f0f0; color: #000000; }
|
| 36 |
+
.stChatMessage[data-testid="stChatMessage-assistant"] { background: #e3f2fd; color: #000000; }
|
| 37 |
+
.lt-logo { vertical-align: middle; }
|
| 38 |
+
</style>
|
| 39 |
+
""", unsafe_allow_html=True)
|
| 40 |
+
|
| 41 |
+
st.markdown("""
|
| 42 |
+
<div style='text-align: center; margin-top: 20px; margin-bottom: -10px;'>
|
| 43 |
+
<span style='display: inline-flex; align-items: center; gap: 8px;'>
|
| 44 |
+
<img src='https://leasetracker.co.za/wp-content/uploads/2020/10/Lease-Tracker-Logo.png' width='100' class='lt-logo'/>
|
| 45 |
+
<span style='font-size: 12px; color: gray;'>Powered by LeaseTracker</span>
|
| 46 |
+
</span>
|
| 47 |
+
</div>
|
| 48 |
+
""", unsafe_allow_html=True)
|
| 49 |
+
|
| 50 |
+
# π Get or create a thread ID
|
| 51 |
+
def get_or_create_thread_id():
|
| 52 |
+
doc_ref = db.collection("users").document(user_id)
|
| 53 |
+
doc = doc_ref.get()
|
| 54 |
+
if doc.exists:
|
| 55 |
+
return doc.to_dict()["thread_id"]
|
| 56 |
+
else:
|
| 57 |
+
thread = client.beta.threads.create()
|
| 58 |
+
doc_ref.set({"thread_id": thread.id, "created_at": firestore.SERVER_TIMESTAMP})
|
| 59 |
+
return thread.id
|
| 60 |
+
|
| 61 |
+
# πΎ Save a message
|
| 62 |
+
def save_message(role, content):
|
| 63 |
+
db.collection("users").document(user_id).collection("messages").add({
|
| 64 |
+
"role": role,
|
| 65 |
+
"content": 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://leasetracker.co.za/wp-content/uploads/2020/10/Lease-Tracker-Logo.png' width='20' style='vertical-align:middle;'/>"
|
| 73 |
+
for msg in list(messages)[::-1]:
|
| 74 |
+
data = msg.to_dict()
|
| 75 |
+
if data["role"] == "user":
|
| 76 |
+
st.markdown(f"<div class='stChatMessage' data-testid='stChatMessage-user'>π€ <strong>You:</strong> {data['content']}</div>", unsafe_allow_html=True)
|
| 77 |
+
else:
|
| 78 |
+
st.markdown(f"<div class='stChatMessage' data-testid='stChatMessage-assistant'>{assistant_icon_html} <strong>LeaseTracker Assistant:</strong> {data['content']}</div>", unsafe_allow_html=True)
|
| 79 |
+
|
| 80 |
+
# π Main Chat UI
|
| 81 |
+
input_col, clear_col = st.columns([9, 1])
|
| 82 |
+
with input_col:
|
| 83 |
+
user_input = st.chat_input("Type your message here...")
|
| 84 |
+
|
| 85 |
+
with clear_col:
|
| 86 |
+
if st.button("ποΈ", key="clear-chat", help="Clear Chat"):
|
| 87 |
+
try:
|
| 88 |
+
user_doc_ref = db.collection("users").document(user_id)
|
| 89 |
+
for msg in user_doc_ref.collection("messages").stream():
|
| 90 |
+
msg.reference.delete()
|
| 91 |
+
user_doc_ref.delete()
|
| 92 |
+
st.session_state.clear()
|
| 93 |
+
st.rerun()
|
| 94 |
+
except Exception as e:
|
| 95 |
+
st.error(f"Failed to clear chat: {e}")
|
| 96 |
+
|
| 97 |
+
thread_id = get_or_create_thread_id()
|
| 98 |
+
display_chat_history()
|
| 99 |
+
|
| 100 |
+
if user_input:
|
| 101 |
+
# Send user message to OpenAI thread
|
| 102 |
+
client.beta.threads.messages.create(thread_id=thread_id, role="user", content=user_input)
|
| 103 |
+
save_message("user", user_input)
|
| 104 |
+
|
| 105 |
+
with st.spinner("Thinking and typing... π"):
|
| 106 |
+
run = client.beta.threads.runs.create(thread_id=thread_id, assistant_id=assistant_id)
|
| 107 |
+
while True:
|
| 108 |
+
run_status = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
|
| 109 |
+
if run_status.status == "completed":
|
| 110 |
+
break
|
| 111 |
+
time.sleep(1)
|
| 112 |
+
|
| 113 |
+
messages_response = client.beta.threads.messages.list(thread_id=thread_id)
|
| 114 |
+
latest_response = sorted(messages_response.data, key=lambda x: x.created_at)[-1]
|
| 115 |
+
assistant_message = latest_response.content[0].text.value
|
| 116 |
+
|
| 117 |
+
save_message("assistant", assistant_message)
|
| 118 |
+
time.sleep(0.5)
|
| 119 |
+
st.rerun()
|