IAMTFRMZA commited on
Commit
7add169
Β·
verified Β·
1 Parent(s): e70ac04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -80
app.py CHANGED
@@ -49,24 +49,16 @@ if st.sidebar.button("🧹 Clear Chat"):
49
 
50
  show_image = st.sidebar.toggle("πŸ“‘ Show Page Image", value=True)
51
 
52
- # ------------------ Center Chat UI ------------------
53
- st.markdown("### 🧠 Ask a Document-Specific Question")
54
- prompt = st.chat_input("Example: What is the defects liability period?")
55
-
56
- # -- Chat Output
57
- for msg in st.session_state.messages:
58
- with st.chat_message(msg["role"]):
59
- st.markdown(msg["content"], unsafe_allow_html=True)
60
-
61
- # -- Expandable Tools Below Chat
62
- with st.expander("πŸ” Navigate or Query Document"):
63
- col1, col2 = st.columns([1.2, 1])
64
- with col1:
65
- keyword = st.text_input("Jump to clause keyword or term", placeholder="e.g. defects, payment, WHS")
66
- with col2:
67
- if st.button("πŸ”Ž Search Keyword") and keyword:
68
- prompt = f"Find clauses or references related to: {keyword}"
69
- st.session_state.search_history.append(keyword)
70
 
71
  section_options = [
72
  "Select a section...",
@@ -82,7 +74,7 @@ with st.expander("πŸ” Navigate or Query Document"):
82
  ]
83
  selected_section = st.selectbox("πŸ“„ Browse Sections", section_options)
84
  if selected_section and selected_section != section_options[0]:
85
- prompt = f"Summarize or list key points from section: {selected_section}"
86
 
87
  actions = [
88
  "Select an action...",
@@ -94,7 +86,18 @@ with st.expander("πŸ” Navigate or Query Document"):
94
  ]
95
  selected_action = st.selectbox("βš™οΈ Common Actions", actions)
96
  if selected_action and selected_action != actions[0]:
97
- prompt = selected_action
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  if st.session_state.search_history:
100
  st.markdown("---")
@@ -102,63 +105,57 @@ with st.expander("πŸ” Navigate or Query Document"):
102
  for term in reversed(st.session_state.search_history[-5:]):
103
  st.markdown(f"- {term}")
104
 
105
- # ------------------ Image Viewer ------------------
106
- with st.expander("πŸ“„ Page Image (Toggle)"):
107
- if show_image and st.session_state.image_url:
108
- with st.spinner("Loading document preview..."):
109
- try:
110
- response = requests.get(st.session_state.image_url)
111
- response.raise_for_status()
112
- img = Image.open(BytesIO(response.content))
113
- st.image(img, caption="πŸ“„ OCR Page Image", use_container_width=True)
114
- st.session_state.image_updated = False
115
- except Exception as e:
116
- st.error(f"❗ Failed to load image: {e}")
117
-
118
- # ------------------ Prompt Handling ------------------
119
- if prompt and not st.session_state.run_in_progress:
120
- st.session_state.messages.append({"role": "user", "content": prompt})
121
- st.session_state.run_in_progress = True
122
-
123
- try:
124
- if st.session_state.thread_id is None:
125
- thread = client.beta.threads.create()
126
- st.session_state.thread_id = thread.id
127
-
128
- client.beta.threads.messages.create(thread_id=st.session_state.thread_id, role="user", content=prompt)
129
-
130
- run = client.beta.threads.runs.create(thread_id=st.session_state.thread_id, assistant_id=ASSISTANT_ID)
131
-
132
- with st.spinner("πŸ€– Parsing and responding with referenced content..."):
133
- while True:
134
- run_status = client.beta.threads.runs.retrieve(thread_id=st.session_state.thread_id, run_id=run.id)
135
- if run_status.status in ("completed", "failed", "cancelled"):
136
- break
137
- time.sleep(1)
138
-
139
- if run_status.status != "completed":
140
- st.error(f"⚠️ Assistant failed: {run_status.status}")
141
- else:
142
- messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
143
- for message in reversed(messages.data):
144
- if message.role == "assistant":
145
- assistant_message = message.content[0].text.value
146
- st.session_state.messages.append({"role": "assistant", "content": assistant_message})
147
- break
148
-
149
- match = re.search(r'Document Reference:\s+(.+?),\s+Page\s+(\d+)', assistant_message)
150
- if match:
151
- doc_name = match.group(1).strip()
152
- page = int(match.group(2))
153
- page_str = f"{page:04d}"
154
- image_url = f"https://raw.githubusercontent.com/AndrewLORTech/c2ozschlaegerforrestdale/main/{doc_name}/{doc_name}_page_{page_str}.png"
155
- st.session_state.image_url = image_url
156
- st.session_state.image_updated = True
157
-
158
- st.session_state.run_in_progress = False
159
- st.rerun()
160
-
161
- except Exception as e:
162
- st.session_state.run_in_progress = False
163
- st.error(f"❌ Error: {e}")
164
-
 
49
 
50
  show_image = st.sidebar.toggle("πŸ“‘ Show Page Image", value=True)
51
 
52
+ # ------------------ Horizontal Layout ------------------
53
+ left, center = st.columns([1, 2])
54
+
55
+ # ------------------ Expandable Left Section ------------------
56
+ with left.expander("πŸ“‚ Navigate or Query Document", expanded=False):
57
+ keyword = st.text_input("Jump to clause keyword or term", placeholder="e.g. defects, payment, WHS")
58
+ if st.button("πŸ”Ž Search Keyword") and keyword:
59
+ st.session_state.search_history.append(keyword)
60
+ if not st.session_state.run_in_progress:
61
+ st.session_state.messages.append({"role": "user", "content": f"Find clauses or references related to: {keyword}"})
 
 
 
 
 
 
 
 
62
 
63
  section_options = [
64
  "Select a section...",
 
74
  ]
75
  selected_section = st.selectbox("πŸ“„ Browse Sections", section_options)
76
  if selected_section and selected_section != section_options[0]:
77
+ st.session_state.messages.append({"role": "user", "content": f"Summarize or list key points from section: {selected_section}"})
78
 
79
  actions = [
80
  "Select an action...",
 
86
  ]
87
  selected_action = st.selectbox("βš™οΈ Common Actions", actions)
88
  if selected_action and selected_action != actions[0]:
89
+ st.session_state.messages.append({"role": "user", "content": selected_action})
90
+
91
+ if show_image and st.session_state.image_url:
92
+ st.markdown("---")
93
+ st.markdown("### πŸ“„ Page Preview")
94
+ try:
95
+ response = requests.get(st.session_state.image_url)
96
+ response.raise_for_status()
97
+ img = Image.open(BytesIO(response.content))
98
+ st.image(img, caption="πŸ“„ OCR Page Image", use_container_width=True)
99
+ except Exception as e:
100
+ st.error(f"❗ Failed to load image: {e}")
101
 
102
  if st.session_state.search_history:
103
  st.markdown("---")
 
105
  for term in reversed(st.session_state.search_history[-5:]):
106
  st.markdown(f"- {term}")
107
 
108
+ # ------------------ Chat Section ------------------
109
+ with center:
110
+ st.markdown("### 🧠 Ask a Document-Specific Question")
111
+ prompt = st.chat_input("Example: What is the defects liability period?")
112
+
113
+ for msg in st.session_state.messages:
114
+ with st.chat_message(msg["role"]):
115
+ st.markdown(msg["content"], unsafe_allow_html=True)
116
+
117
+ if prompt and not st.session_state.run_in_progress:
118
+ st.session_state.messages.append({"role": "user", "content": prompt})
119
+ st.session_state.run_in_progress = True
120
+
121
+ try:
122
+ if st.session_state.thread_id is None:
123
+ thread = client.beta.threads.create()
124
+ st.session_state.thread_id = thread.id
125
+
126
+ client.beta.threads.messages.create(thread_id=st.session_state.thread_id, role="user", content=prompt)
127
+
128
+ run = client.beta.threads.runs.create(thread_id=st.session_state.thread_id, assistant_id=ASSISTANT_ID)
129
+
130
+ with st.spinner("πŸ€– Parsing and responding with referenced content..."):
131
+ while True:
132
+ run_status = client.beta.threads.runs.retrieve(thread_id=st.session_state.thread_id, run_id=run.id)
133
+ if run_status.status in ("completed", "failed", "cancelled"):
134
+ break
135
+ time.sleep(1)
136
+
137
+ if run_status.status != "completed":
138
+ st.error(f"⚠️ Assistant failed: {run_status.status}")
139
+ else:
140
+ messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
141
+ for message in reversed(messages.data):
142
+ if message.role == "assistant":
143
+ assistant_message = message.content[0].text.value
144
+ st.session_state.messages.append({"role": "assistant", "content": assistant_message})
145
+ break
146
+
147
+ match = re.search(r'Document Reference:\s+(.+?),\s+Page\s+(\d+)', assistant_message)
148
+ if match:
149
+ doc_name = match.group(1).strip()
150
+ page = int(match.group(2))
151
+ page_str = f"{page:04d}"
152
+ image_url = f"https://raw.githubusercontent.com/AndrewLORTech/c2ozschlaegerforrestdale/main/{doc_name}/{doc_name}_page_{page_str}.png"
153
+ st.session_state.image_url = image_url
154
+ st.session_state.image_updated = True
155
+
156
+ st.session_state.run_in_progress = False
157
+ st.rerun()
158
+
159
+ except Exception as e:
160
+ st.session_state.run_in_progress = False
161
+ st.error(f"❌ Error: {e}")