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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import asyncio
|
3 |
+
from agent_manager import AgentManager
|
4 |
+
|
5 |
+
# Set page configuration with custom theme
|
6 |
+
st.set_page_config(
|
7 |
+
page_title="Medical AI Agents",
|
8 |
+
layout="wide",
|
9 |
+
initial_sidebar_state="expanded"
|
10 |
+
)
|
11 |
+
|
12 |
+
# Custom CSS for styling
|
13 |
+
st.markdown("""
|
14 |
+
<style>
|
15 |
+
.main-header {
|
16 |
+
font-size: 2.5rem;
|
17 |
+
color: white;
|
18 |
+
text-align: center;
|
19 |
+
padding: 1.5rem;
|
20 |
+
margin-bottom: 1rem;
|
21 |
+
font-weight: bold;
|
22 |
+
background: linear-gradient(120deg, #1E88E5 0%, #1565C0 100%);
|
23 |
+
border-radius: 10px;
|
24 |
+
box-shadow: 0 2px 10px rgba(30,136,229,0.2);
|
25 |
+
}
|
26 |
+
.sub-header {
|
27 |
+
font-size: 1.8rem;
|
28 |
+
color: #0D47A1;
|
29 |
+
padding: 0.5rem 0;
|
30 |
+
border-bottom: 2px solid #1E88E5;
|
31 |
+
margin-bottom: 1rem;
|
32 |
+
}
|
33 |
+
.task-container {
|
34 |
+
background-color: #F8F9FA;
|
35 |
+
padding: 2rem;
|
36 |
+
border-radius: 10px;
|
37 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
38 |
+
}
|
39 |
+
.result-box {
|
40 |
+
background-color: white;
|
41 |
+
padding: 1.5rem;
|
42 |
+
border-radius: 8px;
|
43 |
+
border-left: 4px solid #1E88E5;
|
44 |
+
margin: 1rem 0;
|
45 |
+
}
|
46 |
+
.validation-box {
|
47 |
+
padding: 1rem;
|
48 |
+
border-radius: 8px;
|
49 |
+
margin-top: 1rem;
|
50 |
+
}
|
51 |
+
.stButton>button {
|
52 |
+
background-color: #1E88E5;
|
53 |
+
color: white;
|
54 |
+
border-radius: 25px;
|
55 |
+
padding: 0.5rem 2rem;
|
56 |
+
border: none;
|
57 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
58 |
+
transition: all 0.3s ease;
|
59 |
+
}
|
60 |
+
.stButton>button:hover {
|
61 |
+
background-color: #1565C0;
|
62 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
63 |
+
transform: translateY(-2px);
|
64 |
+
}
|
65 |
+
.stTextArea>div>div {
|
66 |
+
border-radius: 8px;
|
67 |
+
border: 2px solid #E3F2FD;
|
68 |
+
}
|
69 |
+
.sidebar-content {
|
70 |
+
padding: 1rem;
|
71 |
+
background-color: #F8F9FA;
|
72 |
+
border-radius: 8px;
|
73 |
+
}
|
74 |
+
</style>
|
75 |
+
""", unsafe_allow_html=True)
|
76 |
+
|
77 |
+
@st.cache_resource
|
78 |
+
def get_agent_manager():
|
79 |
+
return AgentManager()
|
80 |
+
|
81 |
+
def show_results_page(result_data):
|
82 |
+
st.markdown("<h1 class='main-header'>Final Validated Content</h1>", unsafe_allow_html=True)
|
83 |
+
|
84 |
+
# Add a subheader based on the content type
|
85 |
+
if "summary" in result_data["result"]:
|
86 |
+
st.markdown("<h2 class='sub-header'>Medical Text Summary</h2>", unsafe_allow_html=True)
|
87 |
+
elif "article" in result_data["result"]:
|
88 |
+
st.markdown("<h2 class='sub-header'>Research Article</h2>", unsafe_allow_html=True)
|
89 |
+
elif "sanitized_data" in result_data["result"]:
|
90 |
+
st.markdown("<h2 class='sub-header'>Redacted PHI Content</h2>", unsafe_allow_html=True)
|
91 |
+
|
92 |
+
# Display content in a styled box
|
93 |
+
st.markdown("<div class='result-box'>", unsafe_allow_html=True)
|
94 |
+
if "summary" in result_data["result"]:
|
95 |
+
st.write(result_data["result"]["summary"])
|
96 |
+
elif "article" in result_data["result"]:
|
97 |
+
st.write(result_data["result"]["article"])
|
98 |
+
elif "sanitized_data" in result_data["result"]:
|
99 |
+
st.write(result_data["result"]["sanitized_data"])
|
100 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
101 |
+
|
102 |
+
# Action buttons in columns
|
103 |
+
col1, col2, col3 = st.columns([1, 1, 1])
|
104 |
+
with col2:
|
105 |
+
# Export button
|
106 |
+
if st.button("π₯ Export Results"):
|
107 |
+
export_data = ""
|
108 |
+
if "summary" in result_data["result"]:
|
109 |
+
export_data = result_data["result"]["summary"]
|
110 |
+
elif "article" in result_data["result"]:
|
111 |
+
export_data = result_data["result"]["article"]
|
112 |
+
elif "sanitized_data" in result_data["result"]:
|
113 |
+
export_data = result_data["result"]["sanitized_data"]
|
114 |
+
|
115 |
+
st.download_button(
|
116 |
+
label="πΎ Download Content",
|
117 |
+
data=export_data,
|
118 |
+
file_name="final_content.txt",
|
119 |
+
mime="text/plain"
|
120 |
+
)
|
121 |
+
|
122 |
+
with col3:
|
123 |
+
# Return button
|
124 |
+
if st.button("π Return to Main Page"):
|
125 |
+
st.session_state.show_results = False
|
126 |
+
st.rerun()
|
127 |
+
|
128 |
+
def main():
|
129 |
+
# Sidebar styling
|
130 |
+
with st.sidebar:
|
131 |
+
st.markdown("<h2 style='text-align: center; color: #1E88E5;'>Tasks</h2>", unsafe_allow_html=True)
|
132 |
+
st.markdown("<div class='sidebar-content'>", unsafe_allow_html=True)
|
133 |
+
task_type = st.radio(
|
134 |
+
"", # Empty label as we're using custom header
|
135 |
+
["summarize", "write_article", "Redact PHI"],
|
136 |
+
format_func=lambda x: {
|
137 |
+
"summarize": "π Summarize Medical Text",
|
138 |
+
"write_article": "π Write Research Article",
|
139 |
+
"Redact PHI": "π Redact PHI"
|
140 |
+
}[x]
|
141 |
+
)
|
142 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
143 |
+
|
144 |
+
# Main content - Single header for the entire page
|
145 |
+
st.markdown("<h1 class='main-header'>Medical Multi-Agent System</h1>", unsafe_allow_html=True)
|
146 |
+
|
147 |
+
# Initialize session state
|
148 |
+
if 'show_results' not in st.session_state:
|
149 |
+
st.session_state.show_results = False
|
150 |
+
if 'result_data' not in st.session_state:
|
151 |
+
st.session_state.result_data = None
|
152 |
+
|
153 |
+
if st.session_state.show_results:
|
154 |
+
show_results_page(st.session_state.result_data)
|
155 |
+
return
|
156 |
+
|
157 |
+
agent_manager = get_agent_manager()
|
158 |
+
|
159 |
+
# Task containers with consistent styling
|
160 |
+
st.markdown("<div class='task-container'>", unsafe_allow_html=True)
|
161 |
+
|
162 |
+
if task_type == "summarize":
|
163 |
+
st.markdown("<h2 class='sub-header'>π Summarize Medical Text</h2>", unsafe_allow_html=True)
|
164 |
+
input_text = st.text_area("Enter medical text to summarize", height=200)
|
165 |
+
col1, col2 = st.columns(2)
|
166 |
+
|
167 |
+
with col1:
|
168 |
+
if st.button("π Generate Summary"):
|
169 |
+
with st.spinner("Processing..."):
|
170 |
+
result = asyncio.run(agent_manager.process_task("summarize", input_text))
|
171 |
+
st.session_state.result_data = result
|
172 |
+
st.markdown("<div class='result-box'>", unsafe_allow_html=True)
|
173 |
+
st.subheader("Summary")
|
174 |
+
st.write(result["result"]["summary"])
|
175 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
176 |
+
st.markdown("<div class='validation-box'>", unsafe_allow_html=True)
|
177 |
+
st.subheader("Validation")
|
178 |
+
|
179 |
+
# Extract and display score
|
180 |
+
feedback = result["validation"]["feedback"]
|
181 |
+
if "Score:" in feedback:
|
182 |
+
score = feedback.split("Score:")[1].split("\n")[0].strip()
|
183 |
+
st.markdown(f"""
|
184 |
+
<div style='background-color: #E3F2FD; padding: 1rem; border-radius: 8px; margin-bottom: 1rem;'>
|
185 |
+
<h3 style='margin: 0; color: #1565C0; text-align: center;'>Validation Score: {score}</h3>
|
186 |
+
</div>
|
187 |
+
""", unsafe_allow_html=True)
|
188 |
+
|
189 |
+
st.write(feedback)
|
190 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
191 |
+
|
192 |
+
with col2:
|
193 |
+
if st.session_state.result_data and st.button("ποΈ View Edited Content"):
|
194 |
+
st.session_state.show_results = True
|
195 |
+
st.rerun()
|
196 |
+
|
197 |
+
elif task_type == "write_article":
|
198 |
+
st.markdown("<h2 class='sub-header'>π Write Research Article</h2>", unsafe_allow_html=True)
|
199 |
+
topic = st.text_input("Enter research topic")
|
200 |
+
key_points = st.text_area("Enter key points (one per line)", height=150)
|
201 |
+
col1, col2 = st.columns(2)
|
202 |
+
|
203 |
+
with col1:
|
204 |
+
if st.button("π Generate Article"):
|
205 |
+
with st.spinner("Processing..."):
|
206 |
+
input_data = {"topic": topic, "key_points": key_points}
|
207 |
+
result = asyncio.run(agent_manager.process_task("write_article", input_data))
|
208 |
+
st.session_state.result_data = result
|
209 |
+
st.markdown("<div class='result-box'>", unsafe_allow_html=True)
|
210 |
+
st.subheader("Article")
|
211 |
+
st.write(result["result"]["article"])
|
212 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
213 |
+
st.markdown("<div class='validation-box'>", unsafe_allow_html=True)
|
214 |
+
st.subheader("Validation")
|
215 |
+
|
216 |
+
# Extract and display score
|
217 |
+
feedback = result["validation"]["feedback"]
|
218 |
+
if "Score:" in feedback:
|
219 |
+
score = feedback.split("Score:")[1].split("\n")[0].strip()
|
220 |
+
st.markdown(f"""
|
221 |
+
<div style='background-color: #E3F2FD; padding: 1rem; border-radius: 8px; margin-bottom: 1rem;'>
|
222 |
+
<h3 style='margin: 0; color: #1565C0; text-align: center;'>Validation Score: {score}</h3>
|
223 |
+
</div>
|
224 |
+
""", unsafe_allow_html=True)
|
225 |
+
|
226 |
+
st.write(feedback)
|
227 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
228 |
+
|
229 |
+
with col2:
|
230 |
+
if st.session_state.result_data and st.button("ποΈ View Edited Content"):
|
231 |
+
st.session_state.show_results = True
|
232 |
+
st.rerun()
|
233 |
+
|
234 |
+
elif task_type == "Redact PHI":
|
235 |
+
st.markdown("<h2 class='sub-header'>π Redact Protected Health Information (PHI)</h2>", unsafe_allow_html=True)
|
236 |
+
input_text = st.text_area("Enter medical text to redact PHI", height=200)
|
237 |
+
col1, col2 = st.columns(2)
|
238 |
+
|
239 |
+
with col1:
|
240 |
+
if st.button("π Redact PHI"):
|
241 |
+
with st.spinner("Processing..."):
|
242 |
+
result = asyncio.run(agent_manager.process_task("sanitize", input_text))
|
243 |
+
st.session_state.result_data = result
|
244 |
+
st.markdown("<div class='result-box'>", unsafe_allow_html=True)
|
245 |
+
st.subheader("Redacted Text")
|
246 |
+
st.write(result["result"]["sanitized_data"])
|
247 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
248 |
+
st.markdown("<div class='validation-box'>", unsafe_allow_html=True)
|
249 |
+
st.subheader("Validation")
|
250 |
+
|
251 |
+
# Extract and display score
|
252 |
+
feedback = result["validation"]["feedback"]
|
253 |
+
if "Score:" in feedback:
|
254 |
+
score = feedback.split("Score:")[1].split("\n")[0].strip()
|
255 |
+
st.markdown(f"""
|
256 |
+
<div style='background-color: #E3F2FD; padding: 1rem; border-radius: 8px; margin-bottom: 1rem;'>
|
257 |
+
<h3 style='margin: 0; color: #1565C0; text-align: center;'>Validation Score: {score}</h3>
|
258 |
+
</div>
|
259 |
+
""", unsafe_allow_html=True)
|
260 |
+
|
261 |
+
st.write(feedback)
|
262 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
263 |
+
|
264 |
+
with col2:
|
265 |
+
if st.session_state.result_data and st.button("ποΈ View Edited Content"):
|
266 |
+
st.session_state.show_results = True
|
267 |
+
st.rerun()
|
268 |
+
|
269 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
270 |
+
|
271 |
+
if __name__ == "__main__":
|
272 |
+
main()
|