Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ st.set_page_config(page_title="Document AI Assistant", layout="wide")
|
|
12 |
st.title("π Document AI Assistant")
|
13 |
st.caption("Chat with an AI Assistant on your medical/pathology documents")
|
14 |
|
15 |
-
# ------------------ Load API Key and Assistant ID
|
16 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
17 |
ASSISTANT_ID = os.environ.get("ASSISTANT_ID")
|
18 |
|
@@ -45,10 +45,10 @@ show_image = st.sidebar.checkbox("π Show Document Image", value=True)
|
|
45 |
|
46 |
# ------------------ Load Structured Summary/FAQ ------------------
|
47 |
with open("51940670-Manual-of-Surgical-Pathology-Third-Edition_1_structured_output.json", "r") as f:
|
48 |
-
structured_data = json.load(f)
|
49 |
|
50 |
# ------------------ Three-Column Layout ------------------
|
51 |
-
left, center, right = st.columns([1, 2, 1])
|
52 |
|
53 |
# ------------------ Left Column: Document Image ------------------
|
54 |
with left:
|
@@ -56,7 +56,7 @@ with left:
|
|
56 |
if show_image and st.session_state.image_url:
|
57 |
try:
|
58 |
image = Image.open(requests.get(st.session_state.image_url, stream=True).raw)
|
59 |
-
st.image(image, caption="π Extracted Page",
|
60 |
st.session_state.image_updated = False
|
61 |
except Exception as e:
|
62 |
st.warning("β οΈ Could not load image.")
|
@@ -79,20 +79,17 @@ with center:
|
|
79 |
|
80 |
thread_id = st.session_state.thread_id
|
81 |
|
82 |
-
# Send user prompt
|
83 |
client.beta.threads.messages.create(
|
84 |
thread_id=thread_id,
|
85 |
role="user",
|
86 |
content=prompt
|
87 |
)
|
88 |
|
89 |
-
# Run assistant
|
90 |
run = client.beta.threads.runs.create(
|
91 |
thread_id=thread_id,
|
92 |
assistant_id=ASSISTANT_ID
|
93 |
)
|
94 |
|
95 |
-
# Poll until done
|
96 |
with st.spinner("Assistant is thinking..."):
|
97 |
while True:
|
98 |
run_status = client.beta.threads.runs.retrieve(
|
@@ -103,7 +100,6 @@ with center:
|
|
103 |
break
|
104 |
time.sleep(1)
|
105 |
|
106 |
-
# Get assistant message
|
107 |
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
108 |
assistant_message = None
|
109 |
for message in reversed(messages.data):
|
@@ -114,7 +110,7 @@ with center:
|
|
114 |
st.chat_message("assistant").write(assistant_message)
|
115 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
116 |
|
117 |
-
#
|
118 |
image_match = re.search(
|
119 |
r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
|
120 |
assistant_message
|
@@ -131,17 +127,25 @@ with center:
|
|
131 |
with right:
|
132 |
st.subheader("π Summary")
|
133 |
|
|
|
134 |
if st.session_state.image_url:
|
135 |
match = re.search(r'page_(\d+)', st.session_state.image_url)
|
136 |
-
page_number = int(match.group(1)) if match else
|
137 |
else:
|
138 |
page_number = 151 # default
|
139 |
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
st.markdown(summary_text)
|
142 |
|
143 |
st.subheader("β Auto-Generated FAQ")
|
144 |
-
faq_list = structured_data.get(str(page_number), {}).get("faqs", [])
|
145 |
if faq_list:
|
146 |
for faq in faq_list:
|
147 |
st.markdown(f"**Q:** {faq.get('question', '')}\n\n**A:** {faq.get('answer', '')}")
|
|
|
12 |
st.title("π Document AI Assistant")
|
13 |
st.caption("Chat with an AI Assistant on your medical/pathology documents")
|
14 |
|
15 |
+
# ------------------ Load API Key and Assistant ID ------------------
|
16 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
17 |
ASSISTANT_ID = os.environ.get("ASSISTANT_ID")
|
18 |
|
|
|
45 |
|
46 |
# ------------------ Load Structured Summary/FAQ ------------------
|
47 |
with open("51940670-Manual-of-Surgical-Pathology-Third-Edition_1_structured_output.json", "r") as f:
|
48 |
+
structured_data = json.load(f) # This is a list of dicts, not a dict
|
49 |
|
50 |
# ------------------ Three-Column Layout ------------------
|
51 |
+
left, center, right = st.columns([1, 2, 1])
|
52 |
|
53 |
# ------------------ Left Column: Document Image ------------------
|
54 |
with left:
|
|
|
56 |
if show_image and st.session_state.image_url:
|
57 |
try:
|
58 |
image = Image.open(requests.get(st.session_state.image_url, stream=True).raw)
|
59 |
+
st.image(image, caption="π Extracted Page", use_container_width=True)
|
60 |
st.session_state.image_updated = False
|
61 |
except Exception as e:
|
62 |
st.warning("β οΈ Could not load image.")
|
|
|
79 |
|
80 |
thread_id = st.session_state.thread_id
|
81 |
|
|
|
82 |
client.beta.threads.messages.create(
|
83 |
thread_id=thread_id,
|
84 |
role="user",
|
85 |
content=prompt
|
86 |
)
|
87 |
|
|
|
88 |
run = client.beta.threads.runs.create(
|
89 |
thread_id=thread_id,
|
90 |
assistant_id=ASSISTANT_ID
|
91 |
)
|
92 |
|
|
|
93 |
with st.spinner("Assistant is thinking..."):
|
94 |
while True:
|
95 |
run_status = client.beta.threads.runs.retrieve(
|
|
|
100 |
break
|
101 |
time.sleep(1)
|
102 |
|
|
|
103 |
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
104 |
assistant_message = None
|
105 |
for message in reversed(messages.data):
|
|
|
110 |
st.chat_message("assistant").write(assistant_message)
|
111 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
112 |
|
113 |
+
# Extract GitHub image URL
|
114 |
image_match = re.search(
|
115 |
r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
|
116 |
assistant_message
|
|
|
127 |
with right:
|
128 |
st.subheader("π Summary")
|
129 |
|
130 |
+
# Parse page number from image URL if available
|
131 |
if st.session_state.image_url:
|
132 |
match = re.search(r'page_(\d+)', st.session_state.image_url)
|
133 |
+
page_number = int(match.group(1)) if match else 151
|
134 |
else:
|
135 |
page_number = 151 # default
|
136 |
|
137 |
+
# Get entry from structured data
|
138 |
+
page_entry = next((entry for entry in structured_data if entry.get("page") == page_number), None)
|
139 |
+
if page_entry:
|
140 |
+
summary_text = page_entry.get("summary", "No summary available.")
|
141 |
+
faq_list = page_entry.get("faqs", [])
|
142 |
+
else:
|
143 |
+
summary_text = "No summary available."
|
144 |
+
faq_list = []
|
145 |
+
|
146 |
st.markdown(summary_text)
|
147 |
|
148 |
st.subheader("β Auto-Generated FAQ")
|
|
|
149 |
if faq_list:
|
150 |
for faq in faq_list:
|
151 |
st.markdown(f"**Q:** {faq.get('question', '')}\n\n**A:** {faq.get('answer', '')}")
|