Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import os
|
|
3 |
import time
|
4 |
import re
|
5 |
import requests
|
|
|
6 |
from PIL import Image
|
7 |
from io import BytesIO
|
8 |
from urllib.parse import quote
|
@@ -11,7 +12,7 @@ from openai import OpenAI
|
|
11 |
# ------------------ App Configuration ------------------
|
12 |
st.set_page_config(page_title="Schlaeger Forrestdale TechDocAIA", layout="wide", initial_sidebar_state="collapsed")
|
13 |
st.title("π Schlaeger Forrestdale Document Assistant")
|
14 |
-
st.caption("Explore City of Armadale construction documents using AI + OCR
|
15 |
|
16 |
# ------------------ Load API Key and Assistant ID ------------------
|
17 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
@@ -28,143 +29,95 @@ if "messages" not in st.session_state:
|
|
28 |
st.session_state.messages = []
|
29 |
if "thread_id" not in st.session_state:
|
30 |
st.session_state.thread_id = None
|
31 |
-
if "image_url" not in st.session_state:
|
32 |
-
st.session_state.image_url = None
|
33 |
-
if "image_updated" not in st.session_state:
|
34 |
-
st.session_state.image_updated = False
|
35 |
if "pending_prompt" not in st.session_state:
|
36 |
st.session_state.pending_prompt = None
|
|
|
|
|
37 |
|
38 |
# ------------------ Sidebar ------------------
|
39 |
st.sidebar.header("βΉοΈ Information")
|
40 |
if st.sidebar.button("π§Ή Clear Chat"):
|
41 |
st.session_state.messages = []
|
42 |
st.session_state.thread_id = None
|
43 |
-
st.session_state.image_url = None
|
44 |
-
st.session_state.image_updated = False
|
45 |
st.session_state.pending_prompt = None
|
|
|
46 |
st.rerun()
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
st.
|
52 |
-
|
53 |
-
|
54 |
-
if st.
|
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 |
-
st.
|
96 |
-
|
97 |
-
st.
|
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 |
-
if run_status.status in ("completed", "failed", "cancelled"):
|
124 |
-
break
|
125 |
-
time.sleep(1)
|
126 |
-
|
127 |
-
if run_status.status != "completed":
|
128 |
-
st.error(f"β οΈ Assistant failed: {run_status.status}")
|
129 |
-
else:
|
130 |
-
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
|
131 |
-
for message in reversed(messages.data):
|
132 |
-
if message.role == "assistant":
|
133 |
-
assistant_reply = message.content[0].text.value
|
134 |
-
st.session_state.messages.append({"role": "assistant", "content": assistant_reply})
|
135 |
-
|
136 |
-
# Parse Document Reference and Page, then construct image URL with encoding
|
137 |
-
match = re.search(r'Document Reference:\s*(.*?),\s*Page\s*(\d+)', assistant_reply)
|
138 |
-
if match:
|
139 |
-
doc_name = match.group(1).strip()
|
140 |
-
page = int(match.group(2))
|
141 |
-
page_str = f"{page:04d}"
|
142 |
-
folder = quote(doc_name)
|
143 |
-
image_url = (
|
144 |
-
f"https://raw.githubusercontent.com/AndrewLORTech/c2ozschlaegerforrestdale/main/"
|
145 |
-
f"{folder}/{folder}_page_{page_str}.png"
|
146 |
-
)
|
147 |
-
st.session_state.image_url = image_url
|
148 |
-
st.session_state.image_updated = True
|
149 |
-
break
|
150 |
-
|
151 |
-
st.rerun()
|
152 |
-
except Exception as e:
|
153 |
-
st.error(f"β Error: {e}")
|
154 |
-
|
155 |
for msg in st.session_state.messages:
|
156 |
with st.chat_message(msg["role"]):
|
157 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
158 |
-
|
159 |
-
# ------------------ Image Display ------------------
|
160 |
-
with image_col:
|
161 |
-
if show_image and st.session_state.image_url:
|
162 |
-
with st.spinner("Loading document preview..."):
|
163 |
-
try:
|
164 |
-
response = requests.get(st.session_state.image_url)
|
165 |
-
response.raise_for_status()
|
166 |
-
img = Image.open(BytesIO(response.content))
|
167 |
-
st.image(img, caption="π OCR Page Image", use_container_width=True)
|
168 |
-
st.session_state.image_updated = False
|
169 |
-
except Exception as e:
|
170 |
-
st.error(f"β Failed to load image: {e}")
|
|
|
3 |
import time
|
4 |
import re
|
5 |
import requests
|
6 |
+
import json
|
7 |
from PIL import Image
|
8 |
from io import BytesIO
|
9 |
from urllib.parse import quote
|
|
|
12 |
# ------------------ App Configuration ------------------
|
13 |
st.set_page_config(page_title="Schlaeger Forrestdale TechDocAIA", layout="wide", initial_sidebar_state="collapsed")
|
14 |
st.title("π Schlaeger Forrestdale Document Assistant")
|
15 |
+
st.caption("Explore City of Armadale construction documents using AI + OCR π§")
|
16 |
|
17 |
# ------------------ Load API Key and Assistant ID ------------------
|
18 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
|
|
29 |
st.session_state.messages = []
|
30 |
if "thread_id" not in st.session_state:
|
31 |
st.session_state.thread_id = None
|
|
|
|
|
|
|
|
|
32 |
if "pending_prompt" not in st.session_state:
|
33 |
st.session_state.pending_prompt = None
|
34 |
+
if "results" not in st.session_state:
|
35 |
+
st.session_state.results = []
|
36 |
|
37 |
# ------------------ Sidebar ------------------
|
38 |
st.sidebar.header("βΉοΈ Information")
|
39 |
if st.sidebar.button("π§Ή Clear Chat"):
|
40 |
st.session_state.messages = []
|
41 |
st.session_state.thread_id = None
|
|
|
|
|
42 |
st.session_state.pending_prompt = None
|
43 |
+
st.session_state.results = []
|
44 |
st.rerun()
|
45 |
|
46 |
+
# ------------------ Chat Input ------------------
|
47 |
+
user_prompt = st.chat_input("Ask about plans, drawings or components")
|
48 |
+
if user_prompt:
|
49 |
+
st.session_state.messages.append({"role": "user", "content": user_prompt})
|
50 |
+
|
51 |
+
# ------------------ Assistant Query ------------------
|
52 |
+
if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
|
53 |
+
try:
|
54 |
+
if st.session_state.thread_id is None:
|
55 |
+
thread = client.beta.threads.create()
|
56 |
+
st.session_state.thread_id = thread.id
|
57 |
+
|
58 |
+
client.beta.threads.messages.create(
|
59 |
+
thread_id=st.session_state.thread_id,
|
60 |
+
role="user",
|
61 |
+
content=st.session_state.messages[-1]["content"]
|
62 |
+
)
|
63 |
+
|
64 |
+
run = client.beta.threads.runs.create(
|
65 |
+
thread_id=st.session_state.thread_id,
|
66 |
+
assistant_id=ASSISTANT_ID
|
67 |
+
)
|
68 |
+
|
69 |
+
with st.spinner("π€ Parsing and responding..."):
|
70 |
+
while True:
|
71 |
+
run_status = client.beta.threads.runs.retrieve(
|
72 |
+
thread_id=st.session_state.thread_id,
|
73 |
+
run_id=run.id
|
74 |
+
)
|
75 |
+
if run_status.status in ("completed", "failed", "cancelled"):
|
76 |
+
break
|
77 |
+
time.sleep(1)
|
78 |
+
|
79 |
+
if run_status.status != "completed":
|
80 |
+
st.error(f"β οΈ Assistant failed: {run_status.status}")
|
81 |
+
else:
|
82 |
+
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
|
83 |
+
for message in reversed(messages.data):
|
84 |
+
if message.role == "assistant":
|
85 |
+
assistant_reply = message.content[0].text.value
|
86 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_reply})
|
87 |
+
try:
|
88 |
+
json_data = json.loads(assistant_reply.strip("`json "))
|
89 |
+
st.session_state.results = json_data
|
90 |
+
except:
|
91 |
+
st.session_state.results = []
|
92 |
+
break
|
93 |
+
st.rerun()
|
94 |
+
except Exception as e:
|
95 |
+
st.error(f"β Error: {e}")
|
96 |
+
|
97 |
+
# ------------------ Sorting and Filters ------------------
|
98 |
+
if st.session_state.results:
|
99 |
+
disciplines = sorted(set(d.get("discipline", "") for d in st.session_state.results))
|
100 |
+
selected_discipline = st.selectbox("π Filter by discipline", ["All"] + disciplines)
|
101 |
+
page_size = 8
|
102 |
+
page_num = st.number_input("Page", min_value=1, step=1, value=1)
|
103 |
+
|
104 |
+
filtered_results = [r for r in st.session_state.results if selected_discipline == "All" or r.get("discipline") == selected_discipline]
|
105 |
+
paged = filtered_results[(page_num - 1) * page_size : page_num * page_size]
|
106 |
+
|
107 |
+
st.markdown("---")
|
108 |
+
st.subheader("π Drawing Results")
|
109 |
+
cols = st.columns(4)
|
110 |
+
|
111 |
+
for i, item in enumerate(paged):
|
112 |
+
with cols[i % 4]:
|
113 |
+
st.markdown(f"**{item['drawing_number']}**")
|
114 |
+
st.markdown(f"_Discipline: {item['discipline']}_")
|
115 |
+
st.caption(item.get("summary", ""))
|
116 |
+
for url in item.get("images", [])[:1]:
|
117 |
+
if st.button("πΌοΈ View Image", key=f"view_{i}"):
|
118 |
+
st.image(url, use_column_width=True)
|
119 |
+
|
120 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
for msg in st.session_state.messages:
|
122 |
with st.chat_message(msg["role"]):
|
123 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|