IAMTFRMZA commited on
Commit
212c196
Β·
verified Β·
1 Parent(s): 74246a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -29
app.py CHANGED
@@ -31,6 +31,8 @@ if "image_url" not in st.session_state:
31
  st.session_state.image_url = None
32
  if "image_updated" not in st.session_state:
33
  st.session_state.image_updated = False
 
 
34
 
35
  # ------------------ Sidebar ------------------
36
  st.sidebar.header("βš™οΈ Settings")
@@ -39,6 +41,7 @@ if st.sidebar.button("🧹 Clear Chat"):
39
  st.session_state.thread_id = None
40
  st.session_state.image_url = None
41
  st.session_state.image_updated = False
 
42
  st.rerun()
43
 
44
  show_image = st.sidebar.toggle("πŸ“‘ Show Page Image", value=True)
@@ -64,19 +67,46 @@ with chat_col:
64
  st.markdown("### 🧠 Ask a Document-Specific Question")
65
  prompt = st.chat_input("Example: What is the defects liability period?")
66
 
 
67
  st.markdown("<hr style='margin:0.5em 0;'>", unsafe_allow_html=True)
68
- st.markdown("#### ⚑ Prompt Templates")
69
- col_btn1, col_btn2, col_btn3 = st.columns(3)
70
- with col_btn1:
71
- if st.button("Summarize Current Page"):
72
- prompt = "Summarize the current page."
73
- with col_btn2:
74
- if st.button("List Contractual Obligations"):
75
- prompt = "List the contractual obligations from this section."
76
- with col_btn3:
77
- if st.button("Find Related AS Clauses"):
78
- prompt = "Which clauses reference AS 4000-1997 or similar standards?"
79
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  if prompt:
81
  st.session_state.messages.append({"role": "user", "content": prompt})
82
  try:
@@ -91,30 +121,40 @@ with chat_col:
91
  with st.spinner("πŸ€– Parsing and responding with referenced content..."):
92
  while True:
93
  run_status = client.beta.threads.runs.retrieve(thread_id=st.session_state.thread_id, run_id=run.id)
94
- if run_status.status == "completed":
95
  break
96
  time.sleep(1)
97
 
98
- messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
99
- for message in reversed(messages.data):
100
- if message.role == "assistant":
101
- assistant_message = message.content[0].text.value
102
- st.session_state.messages.append({"role": "assistant", "content": assistant_message})
103
- break
104
-
105
- match = re.search(r'Document Reference:\s+(.+?),\s+Page\s+(\d+)', assistant_message)
106
- if match:
107
- doc_name = match.group(1).strip()
108
- page = int(match.group(2))
109
- page_str = f"{page:04d}"
110
- image_url = f"https://raw.githubusercontent.com/AndrewLORTech/c2ozschlaegerforrestdale/main/{doc_name}/{doc_name}_page_{page_str}.png"
111
- st.session_state.image_url = image_url
112
- st.session_state.image_updated = True
 
 
 
113
 
114
  st.rerun()
115
  except Exception as e:
116
  st.error(f"❌ Error: {e}")
117
 
 
118
  for msg in st.session_state.messages:
119
  with st.chat_message(msg["role"]):
120
- st.markdown(msg["content"], unsafe_allow_html=True)
 
 
 
 
 
 
 
31
  st.session_state.image_url = None
32
  if "image_updated" not in st.session_state:
33
  st.session_state.image_updated = False
34
+ if "search_history" not in st.session_state:
35
+ st.session_state.search_history = []
36
 
37
  # ------------------ Sidebar ------------------
38
  st.sidebar.header("βš™οΈ Settings")
 
41
  st.session_state.thread_id = None
42
  st.session_state.image_url = None
43
  st.session_state.image_updated = False
44
+ st.session_state.search_history = []
45
  st.rerun()
46
 
47
  show_image = st.sidebar.toggle("πŸ“‘ Show Page Image", value=True)
 
67
  st.markdown("### 🧠 Ask a Document-Specific Question")
68
  prompt = st.chat_input("Example: What is the defects liability period?")
69
 
70
+ # -- Smart Query Controls
71
  st.markdown("<hr style='margin:0.5em 0;'>", unsafe_allow_html=True)
72
+ st.markdown("#### πŸ” Navigate or Query Document")
73
+ col1, col2 = st.columns([1.2, 1])
74
+ with col1:
75
+ keyword = st.text_input("Jump to clause keyword or term", placeholder="e.g. defects, payment, WHS")
76
+ with col2:
77
+ if st.button("πŸ”Ž Search Keyword") and keyword:
78
+ prompt = f"Find clauses or references related to: {keyword}"
79
+ st.session_state.search_history.append(keyword)
80
+
81
+ section_options = [
82
+ "Select a section...",
83
+ "1. Formal Instrument of Contract",
84
+ "2. Offer and Acceptance",
85
+ "3. Key Personnel",
86
+ "4. Contract Pricing",
87
+ "5. Contract Specifications",
88
+ "6. WHS Policies",
89
+ "7. Penalties and Delays",
90
+ "8. Dispute Resolution",
91
+ "9. Principal Obligations"
92
+ ]
93
+ selected_section = st.selectbox("πŸ“„ Browse Sections", section_options)
94
+ if selected_section and selected_section != section_options[0]:
95
+ prompt = f"Summarize or list key points from section: {selected_section}"
96
+
97
+ actions = [
98
+ "Select an action...",
99
+ "List all contractual obligations",
100
+ "Summarize payment terms",
101
+ "List WHS responsibilities",
102
+ "Find delay-related penalties",
103
+ "Extract dispute resolution steps"
104
+ ]
105
+ selected_action = st.selectbox("βš™οΈ Common Actions", actions)
106
+ if selected_action and selected_action != actions[0]:
107
+ prompt = selected_action
108
+
109
+ # -- Send prompt to assistant
110
  if prompt:
111
  st.session_state.messages.append({"role": "user", "content": prompt})
112
  try:
 
121
  with st.spinner("πŸ€– Parsing and responding with referenced content..."):
122
  while True:
123
  run_status = client.beta.threads.runs.retrieve(thread_id=st.session_state.thread_id, run_id=run.id)
124
+ if run_status.status in ("completed", "failed", "cancelled"):
125
  break
126
  time.sleep(1)
127
 
128
+ if run_status.status != "completed":
129
+ st.error(f"⚠️ Assistant failed: {run_status.status}")
130
+ else:
131
+ messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
132
+ for message in reversed(messages.data):
133
+ if message.role == "assistant":
134
+ assistant_message = message.content[0].text.value
135
+ st.session_state.messages.append({"role": "assistant", "content": assistant_message})
136
+ break
137
+
138
+ match = re.search(r'Document Reference:\s+(.+?),\s+Page\s+(\d+)', assistant_message)
139
+ if match:
140
+ doc_name = match.group(1).strip()
141
+ page = int(match.group(2))
142
+ page_str = f"{page:04d}"
143
+ image_url = f"https://raw.githubusercontent.com/AndrewLORTech/c2ozschlaegerforrestdale/main/{doc_name}/{doc_name}_page_{page_str}.png"
144
+ st.session_state.image_url = image_url
145
+ st.session_state.image_updated = True
146
 
147
  st.rerun()
148
  except Exception as e:
149
  st.error(f"❌ Error: {e}")
150
 
151
+ # -- Display Chat History
152
  for msg in st.session_state.messages:
153
  with st.chat_message(msg["role"]):
154
+ st.markdown(msg["content"], unsafe_allow_html=True)
155
+
156
+ if st.session_state.search_history:
157
+ st.markdown("---")
158
+ st.markdown("#### πŸ” Recent Searches")
159
+ for term in reversed(st.session_state.search_history[-5:]):
160
+ st.markdown(f"- {term}")