Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,11 +17,24 @@ OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
|
17 |
ASSISTANT_ID = os.environ.get("ASSISTANT_ID")
|
18 |
|
19 |
if not OPENAI_API_KEY or not ASSISTANT_ID:
|
20 |
-
st.error("❌ Missing secrets. Please set both OPENAI_API_KEY and ASSISTANT_ID in
|
21 |
st.stop()
|
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 = []
|
@@ -87,20 +100,19 @@ with col2:
|
|
87 |
thread = client.beta.threads.create()
|
88 |
st.session_state.thread_id = thread.id
|
89 |
|
90 |
-
# Send user
|
91 |
client.beta.threads.messages.create(
|
92 |
thread_id=st.session_state.thread_id,
|
93 |
role="user",
|
94 |
content=prompt
|
95 |
)
|
96 |
|
97 |
-
#
|
98 |
run = client.beta.threads.runs.create(
|
99 |
thread_id=st.session_state.thread_id,
|
100 |
assistant_id=ASSISTANT_ID
|
101 |
)
|
102 |
|
103 |
-
# Wait for completion
|
104 |
with st.spinner("Assistant is thinking..."):
|
105 |
while True:
|
106 |
run_status = client.beta.threads.runs.retrieve(
|
@@ -111,7 +123,7 @@ with col2:
|
|
111 |
break
|
112 |
time.sleep(1)
|
113 |
|
114 |
-
#
|
115 |
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
|
116 |
assistant_message = None
|
117 |
for message in reversed(messages.data):
|
@@ -121,13 +133,23 @@ with col2:
|
|
121 |
|
122 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
123 |
|
124 |
-
# ✅ Extract
|
125 |
-
|
126 |
-
r'https://raw\.githubusercontent\.com/AndrewLORTech/[
|
127 |
assistant_message
|
128 |
)
|
129 |
-
if
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
st.session_state.image_updated = True
|
132 |
|
133 |
st.rerun()
|
|
|
17 |
ASSISTANT_ID = os.environ.get("ASSISTANT_ID")
|
18 |
|
19 |
if not OPENAI_API_KEY or not ASSISTANT_ID:
|
20 |
+
st.error("❌ Missing secrets. Please set both OPENAI_API_KEY and ASSISTANT_ID in Hugging Face Space secrets.")
|
21 |
st.stop()
|
22 |
|
23 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
24 |
|
25 |
+
# ------------------ Map document titles to exact 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 |
+
"22.146.DS Electrical Specification IFC Rev_0": "22.146.DS Electrical Specification IFC Rev_0",
|
31 |
+
"2302-FORRESTDALE SPORTING PAVILION-TECHNICAL SPECIFICATION": "2302-FORRESTDALE SPORTING PAVILION-TECHNICAL SPECIFICATION",
|
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 = []
|
|
|
100 |
thread = client.beta.threads.create()
|
101 |
st.session_state.thread_id = thread.id
|
102 |
|
103 |
+
# Send user message to assistant
|
104 |
client.beta.threads.messages.create(
|
105 |
thread_id=st.session_state.thread_id,
|
106 |
role="user",
|
107 |
content=prompt
|
108 |
)
|
109 |
|
110 |
+
# Trigger assistant run
|
111 |
run = client.beta.threads.runs.create(
|
112 |
thread_id=st.session_state.thread_id,
|
113 |
assistant_id=ASSISTANT_ID
|
114 |
)
|
115 |
|
|
|
116 |
with st.spinner("Assistant is thinking..."):
|
117 |
while True:
|
118 |
run_status = client.beta.threads.runs.retrieve(
|
|
|
123 |
break
|
124 |
time.sleep(1)
|
125 |
|
126 |
+
# Retrieve latest assistant message
|
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 |
|
134 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
135 |
|
136 |
+
# ✅ Extract image URL and correct document folder
|
137 |
+
match = re.search(
|
138 |
+
r'https://raw\.githubusercontent\.com/AndrewLORTech/[^/]+/([^/]+)/\1_page_(\d{4})\.png',
|
139 |
assistant_message
|
140 |
)
|
141 |
+
if match:
|
142 |
+
doc_display = match.group(1).replace("%20", " ")
|
143 |
+
page_num = match.group(2)
|
144 |
+
|
145 |
+
# Correct using folder map
|
146 |
+
correct_folder = DOCUMENT_FOLDER_MAP.get(doc_display, doc_display)
|
147 |
+
correct_url = (
|
148 |
+
f"https://raw.githubusercontent.com/AndrewLORTech/c2ozschlaegerforrestdale/main/"
|
149 |
+
f"{correct_folder}/{correct_folder}_page_{page_num}.png"
|
150 |
+
)
|
151 |
+
|
152 |
+
st.session_state.image_url = correct_url
|
153 |
st.session_state.image_updated = True
|
154 |
|
155 |
st.rerun()
|