Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ from openai import OpenAI
|
|
10 |
# ------------------ App Configuration ------------------
|
11 |
st.set_page_config(page_title="Document AI Assistant", layout="wide")
|
12 |
st.title("📄 Document AI Assistant")
|
13 |
-
st.caption("Chat with an AI Assistant on your
|
14 |
|
15 |
# ------------------ Load API Key and Assistant ID ------------------
|
16 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
@@ -22,19 +22,6 @@ if not OPENAI_API_KEY or not ASSISTANT_ID:
|
|
22 |
|
23 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
24 |
|
25 |
-
# ------------------ Map document titles to GitHub folder names ------------------
|
26 |
-
DOCUMENT_FOLDER_MAP = {
|
27 |
-
"Tender 5 of 2024 - Forrestdale": "Schlager Group Pty Ltd - Tender 5 of 2024 - Forrestdale ~ Pavilion and External Works Part 1 of 2",
|
28 |
-
"Tender 5 of 2024 - Forrestdale Part 2": "Schlager Group Pty Ltd - Tender 5 of 2024 - Forrestdale ~ Pavilion and External Works Part 2 of 2",
|
29 |
-
"Armadale Forrestdale Methodology": "Armadale Forrestdale Methodology",
|
30 |
-
"2302-FORRESTDALE SPORTING PAVILION-TECHNICAL SPECIFICATION": "2302-FORRESTDALE SPORTING PAVILION-TECHNICAL SPECIFICATION",
|
31 |
-
"22.146.DS Electrical Specification IFC Rev_0": "22.146.DS Electrical Specification IFC Rev_0",
|
32 |
-
"Forrrestdale Community Hub Program 23-04-2025": "Forrrestdale Community Hub Program 23-04-2025",
|
33 |
-
"Landscape Specification - Forrestdale Community Hub_02": "Landscape Specification - Forrestdale Community Hub_02",
|
34 |
-
"Mechanical_Spec_-_Forrestdale Sporting Club Room IFC": "Mechanical_Spec_-_Forrestdale Sporting Club Room IFC",
|
35 |
-
"V-23-021-HY-SPEC-R0": "V-23-021-HY-SPEC-R0"
|
36 |
-
}
|
37 |
-
|
38 |
# ------------------ Session State Initialization ------------------
|
39 |
if "messages" not in st.session_state:
|
40 |
st.session_state.messages = []
|
@@ -123,7 +110,7 @@ with col2:
|
|
123 |
break
|
124 |
time.sleep(1)
|
125 |
|
126 |
-
# Retrieve assistant
|
127 |
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
|
128 |
assistant_message = None
|
129 |
for message in reversed(messages.data):
|
@@ -133,21 +120,16 @@ with col2:
|
|
133 |
|
134 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
135 |
|
136 |
-
# ✅
|
137 |
match = re.search(r'Document Reference:\s+(.+?),\s+Page\s+(\d+)', assistant_message)
|
138 |
if match:
|
139 |
-
|
140 |
-
|
141 |
-
page_str = f"{
|
142 |
-
|
143 |
-
# Map to actual GitHub folder name
|
144 |
-
folder_name = DOCUMENT_FOLDER_MAP.get(doc_name_raw, doc_name_raw)
|
145 |
-
|
146 |
corrected_url = (
|
147 |
f"https://raw.githubusercontent.com/AndrewLORTech/c2ozschlaegerforrestdale/main/"
|
148 |
-
f"{
|
149 |
)
|
150 |
-
|
151 |
st.session_state.image_url = corrected_url
|
152 |
st.session_state.image_updated = True
|
153 |
|
|
|
10 |
# ------------------ App Configuration ------------------
|
11 |
st.set_page_config(page_title="Document AI Assistant", layout="wide")
|
12 |
st.title("📄 Document AI Assistant")
|
13 |
+
st.caption("Chat with an AI Assistant on your construction documents")
|
14 |
|
15 |
# ------------------ Load API Key and Assistant ID ------------------
|
16 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
|
|
22 |
|
23 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
# ------------------ Session State Initialization ------------------
|
26 |
if "messages" not in st.session_state:
|
27 |
st.session_state.messages = []
|
|
|
110 |
break
|
111 |
time.sleep(1)
|
112 |
|
113 |
+
# Retrieve assistant reply
|
114 |
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
|
115 |
assistant_message = None
|
116 |
for message in reversed(messages.data):
|
|
|
120 |
|
121 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
122 |
|
123 |
+
# ✅ Parse Document Reference line and generate correct image URL
|
124 |
match = re.search(r'Document Reference:\s+(.+?),\s+Page\s+(\d+)', assistant_message)
|
125 |
if match:
|
126 |
+
doc_name = match.group(1).strip()
|
127 |
+
page = int(match.group(2))
|
128 |
+
page_str = f"{page:04d}"
|
|
|
|
|
|
|
|
|
129 |
corrected_url = (
|
130 |
f"https://raw.githubusercontent.com/AndrewLORTech/c2ozschlaegerforrestdale/main/"
|
131 |
+
f"{doc_name}/{doc_name}_page_{page_str}.png"
|
132 |
)
|
|
|
133 |
st.session_state.image_url = corrected_url
|
134 |
st.session_state.image_updated = True
|
135 |
|