Spaces:
Sleeping
Sleeping
File size: 15,584 Bytes
f8bae2c 4a71975 3d9fd27 d09f114 404d34f d09f114 9910527 4a71975 9571b39 404d34f a7a14c7 9571b39 404d34f a7a14c7 9571b39 404d34f 3d9fd27 42e0794 3d9fd27 a7a14c7 194ecf7 404d34f 194ecf7 404d34f 194ecf7 28a9534 194ecf7 28a9534 194ecf7 28a9534 69137bb 194ecf7 a7a14c7 194ecf7 28a9534 194ecf7 28a9534 194ecf7 28a9534 194ecf7 28a9534 194ecf7 28a9534 194ecf7 28a9534 194ecf7 28a9534 194ecf7 28a9534 194ecf7 28a9534 194ecf7 28a9534 194ecf7 9571b39 194ecf7 9571b39 020b70c 194ecf7 7ff7235 194ecf7 7ff7235 2866bc5 7ff7235 2866bc5 7ff7235 194ecf7 2866bc5 69137bb 194ecf7 7ff7235 8910d8d 194ecf7 9571b39 8910d8d 9571b39 8910d8d 9571b39 8910d8d 7ca70a2 8910d8d 7ca70a2 8910d8d 020b70c 8910d8d e8573c0 8910d8d |
1 2 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
import streamlit as st
import os
import time
import re
import requests
import json
from PIL import Image
from io import BytesIO
from urllib.parse import quote
from openai import OpenAI
# ------------------ Authentication ------------------
VALID_USERS = {
"andrew@lortechnologies.com": "Pass.123",
"asherS@schlagergroup.com.au": "Pass.123",
"daniel@schlagergroup.com.au": "Pass.123",
"admin@schlagergroup.com.au": "Pass.123",
}
def login():
st.title("π Login Required")
email = st.text_input("Email")
password = st.text_input("Password", type="password")
if st.button("Login"):
if VALID_USERS.get(email) == password:
st.session_state.authenticated = True
st.rerun()
else:
st.error("β Incorrect email or password.")
if "authenticated" not in st.session_state:
st.session_state.authenticated = False
if not st.session_state.authenticated:
login()
st.stop()
# ------------------ App Configuration ------------------
st.set_page_config(page_title="Schlager Forrestdale DocAIAssist", layout="wide", initial_sidebar_state="collapsed")
st.title("π Schlager Forrestdale Document Assistant")
st.caption("Explore City of Armadale construction documents using AI + OCR π§")
# ------------------ Load API Key ------------------
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
if not OPENAI_API_KEY:
st.error("β Missing OPENAI_API_KEY environment variable.")
st.stop()
client = OpenAI(api_key=OPENAI_API_KEY)
# ------------------ Tabs (Reordered) ------------------
tab1, tab2, tab3 = st.tabs(["π¬ Assistant", "π Contract Queries", "π Visual Queries"])
# ------------------ Tab 1: General Chat Assistant ------------------
with tab1:
CHAT_ASSISTANT_ID = "asst_KsQRedoJUnEeStzfox1o06lO"
if "chatbot_messages" not in st.session_state:
st.session_state.chatbot_messages = []
if "chatbot_thread_id" not in st.session_state:
st.session_state.chatbot_thread_id = None
with st.sidebar:
st.header("π¬ Chat Assistant Options")
if st.button("π§Ή Clear Chat Assistant"):
st.session_state.chatbot_messages = []
st.session_state.chatbot_thread_id = None
st.rerun()
st.markdown("### π€ General Chat Assistant")
user_prompt = st.chat_input("Ask anything related to the project or documents...")
if user_prompt:
st.session_state.chatbot_messages.append({"role": "user", "content": user_prompt})
for msg in st.session_state.chatbot_messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"], unsafe_allow_html=True)
if st.session_state.chatbot_messages and st.session_state.chatbot_messages[-1]["role"] == "user":
try:
if st.session_state.chatbot_thread_id is None:
thread = client.beta.threads.create()
st.session_state.chatbot_thread_id = thread.id
for m in st.session_state.chatbot_messages[:-1]:
client.beta.threads.messages.create(
thread_id=st.session_state.chatbot_thread_id,
role=m["role"],
content=m["content"]
)
client.beta.threads.messages.create(
thread_id=st.session_state.chatbot_thread_id,
role="user",
content=st.session_state.chatbot_messages[-1]["content"]
)
run = client.beta.threads.runs.create(
thread_id=st.session_state.chatbot_thread_id,
assistant_id=CHAT_ASSISTANT_ID
)
with st.spinner("π€ Assistant replying..."):
while True:
status = client.beta.threads.runs.retrieve(thread_id=st.session_state.chatbot_thread_id, run_id=run.id)
if status.status in ("completed", "failed", "cancelled"):
break
time.sleep(1)
if status.status == "completed":
messages = client.beta.threads.messages.list(thread_id=st.session_state.chatbot_thread_id)
assistant_replies = [m for m in messages.data if m.role == "assistant"]
if assistant_replies:
latest_reply = assistant_replies[0].content[0].text.value.strip()
if not any(m["content"].strip() == latest_reply for m in st.session_state.chatbot_messages if m["role"] == "assistant"):
st.session_state.chatbot_messages.append({"role": "assistant", "content": latest_reply})
st.rerun()
else:
st.error("β Assistant failed to respond.")
except Exception as e:
st.error(f"β Chat Assistant Error: {e}")
# ------------------ Tab 2: Contract Queries ------------------
with tab2:
ASSISTANT_ID = "asst_KsQRedoJUnEeStzfox1o06lO"
if "messages" not in st.session_state:
st.session_state.messages = []
if "thread_id" not in st.session_state:
st.session_state.thread_id = None
if "image_url" not in st.session_state:
st.session_state.image_url = None
if "image_updated" not in st.session_state:
st.session_state.image_updated = False
if "pending_prompt" not in st.session_state:
st.session_state.pending_prompt = None
with st.sidebar:
st.header("βΉοΈ Contract Tools")
if st.button("π§Ή Clear Chat"):
st.session_state.messages = []
st.session_state.thread_id = None
st.session_state.image_url = None
st.session_state.image_updated = False
st.session_state.pending_prompt = None
st.rerun()
show_image = st.toggle("π Show Page Image", value=True)
keyword = st.text_input("Search by Keyword", placeholder="e.g. defects, WHS, delay")
if st.button("π Search Keyword") and keyword:
st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
section_options = [
"Select a section...",
"1. Formal Instrument of Contract",
"2. Offer and Acceptance",
"3. Key Personnel",
"4. Contract Pricing",
"5. Specifications",
"6. WHS Policies",
"7. Penalties and Delays",
"8. Dispute Resolution",
"9. Principal Obligations"
]
section = st.selectbox("π Jump to Section", section_options)
if section != section_options[0]:
st.session_state.pending_prompt = f"Summarize or list key points from section: {section}"
actions = [
"Select an action...",
"List all contractual obligations",
"Summarize payment terms",
"List WHS responsibilities",
"Find delay-related penalties",
"Extract dispute resolution steps"
]
action = st.selectbox("βοΈ Common Queries", actions)
if action != actions[0]:
st.session_state.pending_prompt = action
chat_col, image_col = st.columns([2, 1])
with chat_col:
st.markdown("### π§ Ask a Document-Specific Question")
user_input = st.chat_input("Example: What is the defects liability period?")
if user_input:
st.session_state.messages.append({"role": "user", "content": user_input})
elif st.session_state.pending_prompt:
st.session_state.messages.append({"role": "user", "content": st.session_state.pending_prompt})
st.session_state.pending_prompt = None
if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
try:
if st.session_state.thread_id is None:
thread = client.beta.threads.create()
st.session_state.thread_id = thread.id
client.beta.threads.messages.create(
thread_id=st.session_state.thread_id,
role="user",
content=st.session_state.messages[-1]["content"]
)
run = client.beta.threads.runs.create(
thread_id=st.session_state.thread_id,
assistant_id=ASSISTANT_ID
)
with st.spinner("π€ Thinking..."):
while True:
status = client.beta.threads.runs.retrieve(thread_id=st.session_state.thread_id, run_id=run.id)
if status.status in ("completed", "failed", "cancelled"):
break
time.sleep(1)
if status.status == "completed":
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
for m in reversed(messages.data):
if m.role == "assistant":
reply = m.content[0].text.value.strip()
# β
Avoid near-duplicate assistant responses
is_duplicate = any(
reply in msg["content"] or msg["content"] in reply
for msg in st.session_state.messages if msg["role"] == "assistant"
)
if not is_duplicate:
st.session_state.messages.append({"role": "assistant", "content": reply})
# π Extract image reference if found
match = re.search(r'Document Reference:\s*(.*?),\s*Page\s*(\d+)', reply)
if match:
doc, page = match.group(1).strip(), int(match.group(2))
folder = quote(doc)
img_url = f"https://raw.githubusercontent.com/AndrewLORTech/c2ozschlaegerforrestdale/main/{folder}/{folder}_page_{page:04d}.png"
st.session_state.image_url = img_url
st.session_state.image_updated = True
break
else:
st.error("β Assistant failed.")
st.rerun()
except Exception as e:
st.error(f"β Error: {e}")
# π§Ύ Show messages in reverse (latest on top)
for msg in reversed(st.session_state.messages):
with st.chat_message(msg["role"]):
st.markdown(msg["content"], unsafe_allow_html=True)
with image_col:
if show_image and st.session_state.image_url:
try:
r = requests.get(st.session_state.image_url)
r.raise_for_status()
img = Image.open(BytesIO(r.content))
st.image(img, caption="π OCR Page Image", use_container_width=True)
except Exception as e:
st.error(f"πΌοΈ Image failed: {e}")
# ------------------ Technical Tab ------------------
with tab3:
ASSISTANT_ID = "asst_DjvuWBc7tCvMbAhY7n1em4BZ"
if "tech_messages" not in st.session_state:
st.session_state.tech_messages = []
if "tech_thread_id" not in st.session_state:
st.session_state.tech_thread_id = None
if "tech_results" not in st.session_state:
st.session_state.tech_results = []
st.session_state.tech_lightbox = None
# β
Input moved inside tab3 block
tech_input = st.chat_input("Ask about plans, drawings or components")
if tech_input:
st.session_state.tech_messages.append({"role": "user", "content": tech_input})
if st.session_state.tech_messages and st.session_state.tech_messages[-1]["role"] == "user":
try:
if st.session_state.tech_thread_id is None:
thread = client.beta.threads.create()
st.session_state.tech_thread_id = thread.id
client.beta.threads.messages.create(
thread_id=st.session_state.tech_thread_id,
role="user",
content=st.session_state.tech_messages[-1]["content"]
)
run = client.beta.threads.runs.create(
thread_id=st.session_state.tech_thread_id,
assistant_id=ASSISTANT_ID
)
with st.spinner("π Searching technical drawings..."):
while True:
run_status = client.beta.threads.runs.retrieve(
thread_id=st.session_state.tech_thread_id,
run_id=run.id
)
if run_status.status in ("completed", "failed", "cancelled"):
break
time.sleep(1)
if run_status.status == "completed":
messages = client.beta.threads.messages.list(thread_id=st.session_state.tech_thread_id)
for msg in reversed(messages.data):
if msg.role == "assistant":
content = msg.content[0].text.value
st.session_state.tech_messages.append({"role": "assistant", "content": content})
try:
st.session_state.tech_results = json.loads(content.strip("`json "))
except:
st.session_state.tech_results = []
break
except Exception as e:
st.error(f"β Technical Assistant Error: {e}")
with st.expander("π§ Options (Filter + Pagination)", expanded=False):
disciplines = sorted(set(d.get("discipline", "") for d in st.session_state.tech_results))
selected = st.selectbox("π Filter by discipline", ["All"] + disciplines)
page_size = 8
page = st.number_input("Page", min_value=1, step=1, value=1)
if st.session_state.tech_results:
st.subheader("π Results")
results = [r for r in st.session_state.tech_results if selected == "All" or r.get("discipline") == selected]
paged = results[(page - 1) * page_size : page * page_size]
cols = st.columns(4)
for i, item in enumerate(paged):
with cols[i % 4]:
st.markdown(f"**π {item['drawing_number']} ({item['discipline']})**")
st.caption(item.get("summary", ""))
image_urls = item.get("images", [])
if not image_urls:
st.warning("β οΈ No image available.")
else:
url = image_urls[0]
st.caption(f"π Image URL: {url}")
try:
st.image(url, caption=f"{item['drawing_number']} - Page 1", use_container_width=True)
except Exception as e:
st.error(f"β Could not load image: {e}")
if st.button("πΌοΈ View Drawing Details", key=f"thumb_{i}"):
st.session_state.tech_lightbox = url
if st.session_state.tech_lightbox:
st.image(st.session_state.tech_lightbox, caption="π Enlarged Drawing Preview", use_container_width=True)
if st.button("β Close Preview"):
st.session_state.tech_lightbox = None
st.rerun()
else:
for msg in st.session_state.tech_messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"], unsafe_allow_html=True)
|