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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -69
app.py CHANGED
@@ -33,6 +33,8 @@ 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")
@@ -42,34 +44,22 @@ if st.sidebar.button("🧹 Clear Chat"):
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)
48
 
49
- # ------------------ Layout ------------------
50
- image_col, chat_col = st.columns([1, 2])
 
51
 
52
- # ------------------ Image Viewer ------------------
53
- with image_col:
54
- if show_image and st.session_state.image_url:
55
- with st.spinner("Loading document preview..."):
56
- try:
57
- response = requests.get(st.session_state.image_url)
58
- response.raise_for_status()
59
- img = Image.open(BytesIO(response.content))
60
- st.image(img, caption="πŸ“„ OCR Page Image", use_container_width=True)
61
- st.session_state.image_updated = False
62
- except Exception as e:
63
- st.error(f"❗ Failed to load image: {e}")
64
 
65
- # ------------------ Chat Interface ------------------
66
- with chat_col:
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")
@@ -106,55 +96,69 @@ with chat_col:
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:
113
- if st.session_state.thread_id is None:
114
- thread = client.beta.threads.create()
115
- st.session_state.thread_id = thread.id
116
-
117
- client.beta.threads.messages.create(thread_id=st.session_state.thread_id, role="user", content=prompt)
118
-
119
- run = client.beta.threads.runs.create(thread_id=st.session_state.thread_id, assistant_id=ASSISTANT_ID)
120
-
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}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  st.session_state.image_updated = False
34
  if "search_history" not in st.session_state:
35
  st.session_state.search_history = []
36
+ if "run_in_progress" not in st.session_state:
37
+ st.session_state.run_in_progress = False
38
 
39
  # ------------------ Sidebar ------------------
40
  st.sidebar.header("βš™οΈ Settings")
 
44
  st.session_state.image_url = None
45
  st.session_state.image_updated = False
46
  st.session_state.search_history = []
47
+ st.session_state.run_in_progress = False
48
  st.rerun()
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")
 
96
  if selected_action and selected_action != actions[0]:
97
  prompt = selected_action
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  if st.session_state.search_history:
100
  st.markdown("---")
101
  st.markdown("#### πŸ” Recent Searches")
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
+