witspathologyv2 / app.py
IAMTFRMZA's picture
Update app.py
c7d0024 verified
raw
history blame
9.86 kB
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
# ------------------ App Configuration ------------------
st.set_page_config(page_title="Schlaeger Forrestdale TechDocAIA", layout="wide", initial_sidebar_state="collapsed")
st.title("πŸ“„ Schlaeger 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. Please set it in Hugging Face Space secrets.")
st.stop()
client = OpenAI(api_key=OPENAI_API_KEY)
# ------------------ Tabs Setup ------------------
tabs = st.tabs(["πŸ“‘ Contract", "πŸ“ Technical"])
# ------------------ Contract Tab ------------------
with tabs[0]:
from urllib.parse import quote
ASSISTANT_ID_CONTRACT = "asst_KsQRedoJUnEeStzfox1o06lO"
if "contract_messages" not in st.session_state:
st.session_state.contract_messages = []
if "contract_thread_id" not in st.session_state:
st.session_state.contract_thread_id = None
if "contract_image_url" not in st.session_state:
st.session_state.contract_image_url = None
if "contract_image_updated" not in st.session_state:
st.session_state.contract_image_updated = False
if "contract_pending_prompt" not in st.session_state:
st.session_state.contract_pending_prompt = None
col1, col2 = st.columns([2, 1])
with col1:
st.markdown("### 🧠 Ask a Document-Specific Question")
user_prompt = st.chat_input("Example: What is the defects liability period?")
if user_prompt:
st.session_state.contract_messages.append({"role": "user", "content": user_prompt})
elif st.session_state.contract_pending_prompt:
st.session_state.contract_messages.append({"role": "user", "content": st.session_state.contract_pending_prompt})
st.session_state.contract_pending_prompt = None
if st.session_state.contract_messages and st.session_state.contract_messages[-1]["role"] == "user":
try:
if st.session_state.contract_thread_id is None:
thread = client.beta.threads.create()
st.session_state.contract_thread_id = thread.id
client.beta.threads.messages.create(
thread_id=st.session_state.contract_thread_id,
role="user",
content=st.session_state.contract_messages[-1]["content"]
)
run = client.beta.threads.runs.create(
thread_id=st.session_state.contract_thread_id,
assistant_id=ASSISTANT_ID_CONTRACT
)
with st.spinner("πŸ€– Parsing and responding with referenced content..."):
while True:
run_status = client.beta.threads.runs.retrieve(
thread_id=st.session_state.contract_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.contract_thread_id)
for message in reversed(messages.data):
if message.role == "assistant":
assistant_reply = message.content[0].text.value
st.session_state.contract_messages.append({"role": "assistant", "content": assistant_reply})
match = re.search(r'Document Reference:\s*(.*?),\s*Page\s*(\d+)', assistant_reply)
if match:
doc_name = match.group(1).strip()
page = int(match.group(2))
page_str = f"{page:04d}"
folder = quote(doc_name)
image_url = f"https://raw.githubusercontent.com/AndrewLORTech/c2ozschlaegerforrestdale/main/{folder}/{folder}_page_{page_str}.png"
st.session_state.contract_image_url = image_url
st.session_state.contract_image_updated = True
break
else:
st.error(f"⚠️ Assistant failed: {run_status.status}")
st.rerun()
except Exception as e:
st.error(f"❌ Error: {e}")
for msg in st.session_state.contract_messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"], unsafe_allow_html=True)
with col2:
if st.session_state.contract_image_url:
try:
response = requests.get(st.session_state.contract_image_url)
response.raise_for_status()
img = Image.open(BytesIO(response.content))
st.image(img, caption="πŸ“„ OCR Page Image", use_container_width=True)
except Exception as e:
st.error(f"❗ Failed to load image: {e}")
# ------------------ Technical Tab ------------------
with tabs[1]:
ASSISTANT_ID_TECHNICAL = "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 = []
if "tech_lightbox_url" not in st.session_state:
st.session_state.tech_lightbox_url = None
user_prompt = st.chat_input("Ask about plans, drawings or components")
if user_prompt:
st.session_state.tech_messages.append({"role": "user", "content": user_prompt})
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_TECHNICAL
)
with st.spinner("πŸ€– Parsing and responding..."):
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 message in reversed(messages.data):
if message.role == "assistant":
assistant_reply = message.content[0].text.value
st.session_state.tech_messages.append({"role": "assistant", "content": assistant_reply})
try:
json_data = json.loads(assistant_reply.strip("`json "))
st.session_state.tech_results = json_data
except:
st.session_state.tech_results = []
break
else:
st.error(f"⚠️ Assistant failed: {run_status.status}")
st.rerun()
except Exception as e:
st.error(f"❌ Error: {e}")
if st.session_state.tech_results:
disciplines = sorted(set(d.get("discipline", "") for d in st.session_state.tech_results))
selected_discipline = st.selectbox("🌍 Filter by discipline", ["All"] + disciplines)
page_size = 8
page_num = st.number_input("Page", min_value=1, step=1, value=1)
filtered_results = [r for r in st.session_state.tech_results if selected_discipline == "All" or r.get("discipline") == selected_discipline]
paged = filtered_results[(page_num - 1) * page_size : page_num * page_size]
st.markdown("---")
st.subheader("πŸ“‚ Drawing Results")
cols = st.columns(4)
for i, item in enumerate(paged):
with cols[i % 4]:
st.markdown(f"**{item['drawing_number']}**")
st.markdown(f"_Discipline: {item['discipline']}_")
st.caption(item.get("summary", ""))
for url in item.get("images", [])[:1]:
if st.button("πŸ–ΌοΈ View Image", key=f"view_{i}"):
st.session_state.tech_lightbox_url = url
if st.session_state.tech_lightbox_url:
col_a, col_b = st.columns([1, 2])
with col_b:
st.image(st.session_state.tech_lightbox_url, caption="πŸ” Enlarged Preview", use_container_width=True)
if st.button("❌ Close Viewer"):
st.session_state.tech_lightbox_url = 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)