Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -53,6 +53,60 @@ if st.sidebar.button("π Clear Chat"):
|
|
53 |
|
54 |
show_image = st.sidebar.checkbox("π Show Document Image", value=True)
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
# ------------------ Layout ------------------
|
57 |
left, center, right = st.columns([1, 2, 1])
|
58 |
|
@@ -70,101 +124,26 @@ with left:
|
|
70 |
# ------------------ Center Column: Chat UI ------------------
|
71 |
with center:
|
72 |
st.subheader("π¬ Document AI Assistant")
|
73 |
-
for message in st.session_state.messages:
|
74 |
role, content = message["role"], message["content"]
|
75 |
st.chat_message(role).write(content)
|
76 |
|
77 |
if prompt := st.chat_input("Type your question about the document..."):
|
78 |
-
|
79 |
-
st.chat_message("user").write(prompt)
|
80 |
-
|
81 |
-
try:
|
82 |
-
if st.session_state.thread_id is None:
|
83 |
-
thread = client.beta.threads.create()
|
84 |
-
st.session_state.thread_id = thread.id
|
85 |
-
|
86 |
-
thread_id = st.session_state.thread_id
|
87 |
-
|
88 |
-
client.beta.threads.messages.create(
|
89 |
-
thread_id=thread_id,
|
90 |
-
role="user",
|
91 |
-
content=prompt
|
92 |
-
)
|
93 |
-
|
94 |
-
run = client.beta.threads.runs.create(
|
95 |
-
thread_id=thread_id,
|
96 |
-
assistant_id=ASSISTANT_ID
|
97 |
-
)
|
98 |
-
|
99 |
-
with st.spinner("Assistant is thinking..."):
|
100 |
-
while True:
|
101 |
-
run_status = client.beta.threads.runs.retrieve(
|
102 |
-
thread_id=thread_id,
|
103 |
-
run_id=run.id
|
104 |
-
)
|
105 |
-
if run_status.status == "completed":
|
106 |
-
break
|
107 |
-
time.sleep(1)
|
108 |
-
|
109 |
-
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
110 |
-
assistant_message = None
|
111 |
-
for message in reversed(messages.data):
|
112 |
-
if message.role == "assistant":
|
113 |
-
assistant_message = message.content[0].text.value
|
114 |
-
break
|
115 |
|
116 |
-
|
117 |
-
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
118 |
-
|
119 |
-
# Extract GitHub image URL
|
120 |
-
image_match = re.search(
|
121 |
-
r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
|
122 |
-
assistant_message
|
123 |
-
)
|
124 |
-
if image_match:
|
125 |
-
st.session_state.image_url = image_match.group(0)
|
126 |
-
st.session_state.image_updated = True
|
127 |
-
st.rerun()
|
128 |
-
|
129 |
-
except Exception as e:
|
130 |
-
st.error(f"β Error: {str(e)}")
|
131 |
-
|
132 |
-
# ------------------ Right Column: Structured Summary + FAQ (Button-based) ------------------
|
133 |
with right:
|
134 |
-
st.subheader("
|
135 |
|
136 |
col1, col2 = st.columns(2)
|
137 |
show_summary = col1.button("π Load Summary")
|
138 |
show_faq = col2.button("β Load FAQ")
|
139 |
|
140 |
-
summary_text = "Click the button to load summary."
|
141 |
-
faq_list = []
|
142 |
-
|
143 |
if st.session_state.image_url:
|
144 |
match = re.search(r'/(\d{3})\.png', st.session_state.image_url)
|
145 |
if match:
|
146 |
page_number = int(match.group(1))
|
147 |
-
|
148 |
-
|
149 |
-
if
|
150 |
-
|
151 |
-
summary_text = page_entry.get("summary", "No summary available.")
|
152 |
-
if show_faq:
|
153 |
-
faq_list = page_entry.get("faqs", []) or page_entry.get("questions", [])
|
154 |
-
|
155 |
-
# Display Summary
|
156 |
-
if show_summary:
|
157 |
-
st.subheader("π Summary")
|
158 |
-
st.markdown(summary_text)
|
159 |
-
|
160 |
-
# Display FAQs
|
161 |
-
if show_faq:
|
162 |
-
st.subheader("β Auto-Generated FAQ")
|
163 |
-
if faq_list:
|
164 |
-
for faq in faq_list:
|
165 |
-
if isinstance(faq, dict):
|
166 |
-
st.markdown(f"**Q:** {faq.get('question', '')}\n\n**A:** {faq.get('answer', '')}")
|
167 |
-
else:
|
168 |
-
st.markdown(f"**Q:** {faq}")
|
169 |
-
else:
|
170 |
-
st.info("No FAQs available for this page.")
|
|
|
53 |
|
54 |
show_image = st.sidebar.checkbox("π Show Document Image", value=True)
|
55 |
|
56 |
+
# ------------------ Assistant Query Function ------------------
|
57 |
+
def query_assistant(prompt):
|
58 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
59 |
+
st.chat_message("user").write(prompt)
|
60 |
+
|
61 |
+
try:
|
62 |
+
if st.session_state.thread_id is None:
|
63 |
+
thread = client.beta.threads.create()
|
64 |
+
st.session_state.thread_id = thread.id
|
65 |
+
|
66 |
+
thread_id = st.session_state.thread_id
|
67 |
+
|
68 |
+
client.beta.threads.messages.create(
|
69 |
+
thread_id=thread_id,
|
70 |
+
role="user",
|
71 |
+
content=prompt
|
72 |
+
)
|
73 |
+
|
74 |
+
run = client.beta.threads.runs.create(
|
75 |
+
thread_id=thread_id,
|
76 |
+
assistant_id=ASSISTANT_ID
|
77 |
+
)
|
78 |
+
|
79 |
+
with st.spinner("Assistant is thinking..."):
|
80 |
+
while True:
|
81 |
+
run_status = client.beta.threads.runs.retrieve(
|
82 |
+
thread_id=thread_id,
|
83 |
+
run_id=run.id
|
84 |
+
)
|
85 |
+
if run_status.status == "completed":
|
86 |
+
break
|
87 |
+
time.sleep(1)
|
88 |
+
|
89 |
+
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
90 |
+
for message in reversed(messages.data):
|
91 |
+
if message.role == "assistant":
|
92 |
+
assistant_message = message.content[0].text.value
|
93 |
+
st.chat_message("assistant").write(assistant_message)
|
94 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
95 |
+
|
96 |
+
# Extract GitHub image URL if available
|
97 |
+
image_match = re.search(
|
98 |
+
r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
|
99 |
+
assistant_message
|
100 |
+
)
|
101 |
+
if image_match:
|
102 |
+
st.session_state.image_url = image_match.group(0)
|
103 |
+
st.session_state.image_updated = True
|
104 |
+
return assistant_message
|
105 |
+
|
106 |
+
except Exception as e:
|
107 |
+
st.error(f"β Error: {str(e)}")
|
108 |
+
return None
|
109 |
+
|
110 |
# ------------------ Layout ------------------
|
111 |
left, center, right = st.columns([1, 2, 1])
|
112 |
|
|
|
124 |
# ------------------ Center Column: Chat UI ------------------
|
125 |
with center:
|
126 |
st.subheader("π¬ Document AI Assistant")
|
127 |
+
for message in reversed(st.session_state.messages):
|
128 |
role, content = message["role"], message["content"]
|
129 |
st.chat_message(role).write(content)
|
130 |
|
131 |
if prompt := st.chat_input("Type your question about the document..."):
|
132 |
+
query_assistant(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
+
# ------------------ Right Column: Structured Summary + FAQ (Trigger Assistant) ------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
with right:
|
136 |
+
st.subheader("οΏ½οΏ½οΏ½ Summary & FAQ (from Assistant)")
|
137 |
|
138 |
col1, col2 = st.columns(2)
|
139 |
show_summary = col1.button("π Load Summary")
|
140 |
show_faq = col2.button("β Load FAQ")
|
141 |
|
|
|
|
|
|
|
142 |
if st.session_state.image_url:
|
143 |
match = re.search(r'/(\d{3})\.png', st.session_state.image_url)
|
144 |
if match:
|
145 |
page_number = int(match.group(1))
|
146 |
+
if show_summary:
|
147 |
+
query_assistant(f"Summarize the section on page {page_number}.")
|
148 |
+
if show_faq:
|
149 |
+
query_assistant(f"Provide FAQs for the section on page {page_number}.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|