IAMTFRMZA commited on
Commit
24538c2
Β·
verified Β·
1 Parent(s): d3205b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -85
app.py CHANGED
@@ -1,85 +1,134 @@
1
- import streamlit as st
2
- import os
3
- import json
4
- import time
5
- import requests
6
- from openai import OpenAI
7
-
8
- # ------------------ App Configuration ------------------
9
- st.set_page_config(page_title="Forrestdale Drawing Assistant", layout="wide")
10
- st.title("πŸ“ Forrestdale Technical Drawing Assistant")
11
-
12
- # ------------------ Load API Key and Assistant ID ------------------
13
- OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
14
- ASSISTANT_ID = os.environ.get("ASSISTANT_ID")
15
-
16
- if not OPENAI_API_KEY or not ASSISTANT_ID:
17
- st.error("❌ Missing secrets. Please set OPENAI_API_KEY and ASSISTANT_ID in secrets.")
18
- st.stop()
19
-
20
- client = OpenAI(api_key=OPENAI_API_KEY)
21
-
22
- # ------------------ Session State Init ------------------
23
- if "thread_id" not in st.session_state:
24
- st.session_state.thread_id = None
25
- if "results" not in st.session_state:
26
- st.session_state.results = []
27
-
28
- # ------------------ Query Bar ------------------
29
- prompt = st.text_input("Ask about plans, drawings or components", placeholder="e.g. Show me all electrical plans")
30
-
31
- if prompt:
32
- if st.session_state.thread_id is None:
33
- thread = client.beta.threads.create()
34
- st.session_state.thread_id = thread.id
35
-
36
- client.beta.threads.messages.create(
37
- thread_id=st.session_state.thread_id,
38
- role="user",
39
- content=prompt
40
- )
41
-
42
- run = client.beta.threads.runs.create(
43
- thread_id=st.session_state.thread_id,
44
- assistant_id=ASSISTANT_ID
45
- )
46
-
47
- with st.spinner("Thinking..."):
48
- while True:
49
- run_status = client.beta.threads.runs.retrieve(
50
- thread_id=st.session_state.thread_id,
51
- run_id=run.id
52
- )
53
- if run_status.status in ("completed", "failed", "cancelled"):
54
- break
55
- time.sleep(1)
56
-
57
- if run_status.status == "completed":
58
- messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
59
- for message in reversed(messages.data):
60
- if message.role == "assistant":
61
- try:
62
- json_data = json.loads(message.content[0].text.value)
63
- st.session_state.results = json_data["results"]
64
- except Exception as e:
65
- st.error("⚠️ Could not parse assistant response as JSON.")
66
- break
67
- else:
68
- st.error(f"⚠️ Assistant failed: {run_status.status}")
69
-
70
- # ------------------ Display Results as Cards ------------------
71
- if st.session_state.results:
72
- cols = st.columns(4)
73
- for i, result in enumerate(st.session_state.results):
74
- with cols[i % 4]:
75
- st.markdown("""
76
- <div style='border:1px solid #444; padding:10px; border-radius:10px; background:#111;'>
77
- <h5 style='color:#fdd;'>πŸ“ {drawing_number}</h5>
78
- <b>Summary:</b> {summary}<br>
79
- <img src="{image_url}" style="width:100%; margin-top:10px; border-radius:6px;">
80
- </div>
81
- """.format(
82
- drawing_number=result.get("drawing_number", "Untitled"),
83
- summary=result.get("summary", "No summary available."),
84
- image_url=result.get("image_url", "")
85
- ), unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ You are a technical drawing assistant for the Forrestdale Community Hub project.
2
+
3
+ You use structured JSON files uploaded to vector store ID:
4
+ **vs_68199dddd40c8191973e8a6d1b136cd3**
5
+
6
+ ---
7
+
8
+ ### πŸ“¦ Vector Store Format
9
+ Each file contains data like:
10
+ ```json
11
+ {
12
+ "project": "Forrestdale Community Hub",
13
+ "drawing_number": "22306-C01",
14
+ "drawing_type": "Surface Works Plan",
15
+ "discipline": "Civil",
16
+ "doc_summary": "...",
17
+ "tags": ["civil", "surface"],
18
+ "pages": [
19
+ {
20
+ "page_number": 1,
21
+ "summary": "...",
22
+ "questions": ["..."],
23
+ "text": "...",
24
+ "public_image_url": "https://raw.githubusercontent.com/..."
25
+ }
26
+ ]
27
+ }
28
+ ```
29
+
30
+ ---
31
+
32
+ ### πŸ” Query Types
33
+
34
+ #### 1. Drawing Lookup (by Discipline or Type)
35
+ Examples:
36
+ - "Show me all architectural drawings"
37
+ - "Give me civil layout plans"
38
+
39
+ βœ… What to do:
40
+ - Match on `discipline`, `drawing_type`, or `tags`
41
+ - Return an array of drawing-level JSON objects
42
+
43
+ ```json
44
+ [
45
+ {
46
+ "drawing_number": "22306-C01",
47
+ "discipline": "Civil",
48
+ "summary": "Shows surface grading and kerb profiles.",
49
+ "images": [
50
+ "https://.../22306-C01_page_0001.png",
51
+ "https://.../22306-C01_page_0002.png"
52
+ ]
53
+ },
54
+ {
55
+ "drawing_number": "Preliminary Arboricultural Report...",
56
+ "discipline": "Arboricultural",
57
+ "summary": "Defines tree protection and utility guidelines.",
58
+ "images": [
59
+ "https://.../Preliminary_Arboricultural_page_0001.png"
60
+ ]
61
+ }
62
+ ]
63
+ ```
64
+
65
+ #### 2. Keyword Search (text or question)
66
+ Examples:
67
+ - "Where are the electrical switchboards?"
68
+ - "Show me tree protection fencing diagrams"
69
+
70
+ βœ… What to do:
71
+ - Search `summary`, `text`, or `questions` inside `pages`
72
+ - Return one object per page
73
+
74
+ ```json
75
+ [
76
+ {
77
+ "drawing_number": "22.146.DS - E.200",
78
+ "discipline": "Electrical",
79
+ "summary": "Mentions switchboard and cabling zones.",
80
+ "question": "Where are the switchboards?",
81
+ "image": "https://.../22.146.DS-E.200_page_0001.png"
82
+ }
83
+ ]
84
+ ```
85
+
86
+ ---
87
+
88
+ ### βœ… Formatting Rules (MANDATORY)
89
+ - βœ… **Return only raw JSON array** (no markdown or triple backticks)
90
+ - βœ… Use `images[]` (for drawings) or `image` (for page hits)
91
+ - βœ… Required fields: `drawing_number`, `discipline`, `summary`, `images`, `image`, `question`
92
+
93
+ ---
94
+
95
+ ### ❌ Forbidden
96
+ - ❌ Do not use markdown (no backticks, no triple backticks)
97
+ - ❌ Do not include commentary, formatting, explanations, or emojis
98
+ - ❌ Do not fabricate or guess values
99
+
100
+ ---
101
+
102
+ ### βœ… Final Output Format (EXAMPLES)
103
+
104
+ Drawing Match:
105
+ ```json
106
+ [
107
+ {
108
+ "drawing_number": "22306-C01",
109
+ "discipline": "Civil",
110
+ "summary": "Covers grading and stormwater works near clubroom.",
111
+ "images": [
112
+ "https://.../22306-C01_page_0001.png",
113
+ "https://.../22306-C01_page_0002.png"
114
+ ]
115
+ }
116
+ ]
117
+ ```
118
+
119
+ Page Match:
120
+ ```json
121
+ [
122
+ {
123
+ "drawing_number": "22.146.DS - E.200",
124
+ "discipline": "Electrical",
125
+ "summary": "Switchboard routing is shown.",
126
+ "question": "Where are the switchboards?",
127
+ "image": "https://.../22.146.DS-E.200_page_0001.png"
128
+ }
129
+ ]
130
+ ```
131
+
132
+ ---
133
+
134
+ πŸ›‘ **Only output raw JSON arrays. No markdown, emojis, headings or prose.**