Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import time
|
3 |
+
import plotly.graph_objects as go
|
4 |
+
|
5 |
+
# Set page layout to wide mode
|
6 |
+
st.set_page_config(layout="wide")
|
7 |
+
|
8 |
+
# Treatment outcomes and possibilities
|
9 |
+
outcomes = [
|
10 |
+
("Doctor prescribes Treatment 1 (Personalized Drug A)", "info"),
|
11 |
+
("Nurse monitors Patient - Slight improvement", "success"),
|
12 |
+
("Clinician evaluates lab results - Treatment seems effective", "info"),
|
13 |
+
("Doctor prescribes Treatment 2 (Personalized Drug B)", "warning"),
|
14 |
+
("Nurse monitors Patient - Side effects observed", "error"),
|
15 |
+
("Clinician suggests modifying dosage", "warning"),
|
16 |
+
("Doctor prescribes modified Treatment 2", "info"),
|
17 |
+
("Patient shows significant improvement", "success")
|
18 |
+
]
|
19 |
+
|
20 |
+
# Different possible outcomes of each treatment
|
21 |
+
possibilities = {
|
22 |
+
1: [
|
23 |
+
("Possibility 1: Patient responds well to Treatment 1", "success"),
|
24 |
+
("Possibility 2: Slight improvement, but inconclusive results", "info"),
|
25 |
+
("Possibility 3: No response, reevaluation needed", "error")
|
26 |
+
],
|
27 |
+
2: [
|
28 |
+
("Possibility 1: Significant improvement", "success"),
|
29 |
+
("Possibility 2: Mild side effects", "warning")
|
30 |
+
],
|
31 |
+
3: [
|
32 |
+
("Possibility 1: Treatment is effective", "success"),
|
33 |
+
("Possibility 2: Inconclusive lab results", "info")
|
34 |
+
],
|
35 |
+
4: [
|
36 |
+
("Possibility 1: Improvement with Drug B", "success"),
|
37 |
+
("Possibility 2: Significant side effects", "error")
|
38 |
+
],
|
39 |
+
5: [
|
40 |
+
("Possibility 1: Side effects worsen, modify dosage", "warning"),
|
41 |
+
("Possibility 2: Manageable side effects", "info")
|
42 |
+
],
|
43 |
+
6: [
|
44 |
+
("Possibility 1: Dosage adjustment successful", "success"),
|
45 |
+
("Possibility 2: Further modification needed", "warning")
|
46 |
+
],
|
47 |
+
7: [
|
48 |
+
("Possibility 1: Patient responds well to modified treatment", "success"),
|
49 |
+
("Possibility 2: Limited response, consider alternatives", "warning")
|
50 |
+
],
|
51 |
+
8: [
|
52 |
+
("Possibility 1: Complete recovery", "success"),
|
53 |
+
("Possibility 2: Partial improvement, continue monitoring", "info")
|
54 |
+
]
|
55 |
+
}
|
56 |
+
|
57 |
+
# Create a tree diagram using Plotly with animations
|
58 |
+
def create_tree_diagram(stage):
|
59 |
+
labels = [f"Stage {stage}: {outcomes[stage - 1][0]}"]
|
60 |
+
parents = [""] # Root node
|
61 |
+
values = [1] # Root node value
|
62 |
+
colors = ['lightgrey'] # Root node color
|
63 |
+
|
64 |
+
stage_possibilities = possibilities.get(stage, [("No specific possibilities defined", "info")])
|
65 |
+
|
66 |
+
for possibility, outcome_type in stage_possibilities:
|
67 |
+
labels.append(possibility)
|
68 |
+
parents.append(f"Stage {stage}: {outcomes[stage - 1][0]}")
|
69 |
+
values.append(1) # Equal weight for all possibilities
|
70 |
+
if outcome_type == "success":
|
71 |
+
colors.append('#d4edda')
|
72 |
+
elif outcome_type == "info":
|
73 |
+
colors.append('#cce5ff')
|
74 |
+
elif outcome_type == "warning":
|
75 |
+
colors.append('#fff3cd')
|
76 |
+
elif outcome_type == "error":
|
77 |
+
colors.append('#f8d7da')
|
78 |
+
else:
|
79 |
+
colors.append('lightgrey')
|
80 |
+
|
81 |
+
fig = go.Figure(go.Treemap(
|
82 |
+
labels=labels,
|
83 |
+
parents=parents,
|
84 |
+
values=values,
|
85 |
+
marker=dict(colors=colors),
|
86 |
+
textinfo="label",
|
87 |
+
textfont=dict(size=14),
|
88 |
+
hoverinfo="label+value+percent entry"
|
89 |
+
))
|
90 |
+
|
91 |
+
fig.update_layout(
|
92 |
+
margin=dict(t=10, l=10, r=10, b=10),
|
93 |
+
width=600, height=400,
|
94 |
+
uniformtext=dict(minsize=12, mode='show'),
|
95 |
+
transition_duration=500 # Animation speed
|
96 |
+
)
|
97 |
+
|
98 |
+
return fig
|
99 |
+
|
100 |
+
# Streamlit app layout
|
101 |
+
st.title("Precision Medicine AI Agents - Treatment Decision Tree with Animations")
|
102 |
+
|
103 |
+
# Add a start animation button
|
104 |
+
start_button = st.button("Start Animation")
|
105 |
+
|
106 |
+
if start_button:
|
107 |
+
for i, (outcome, outcome_type) in enumerate(outcomes, 1):
|
108 |
+
with st.container():
|
109 |
+
col1, col2 = st.columns([1, 2])
|
110 |
+
|
111 |
+
with col1:
|
112 |
+
if outcome_type == "success":
|
113 |
+
st.markdown(f"<div style='padding:10px; border-radius:5px; background-color:#d4edda; color:#155724;'><strong>Stage {i}:</strong> {outcome}</div>", unsafe_allow_html=True)
|
114 |
+
elif outcome_type == "info":
|
115 |
+
st.markdown(f"<div style='padding:10px; border-radius:5px; background-color:#cce5ff; color:#004085;'><strong>Stage {i}:</strong> {outcome}</div>", unsafe_allow_html=True)
|
116 |
+
elif outcome_type == "warning":
|
117 |
+
st.markdown(f"<div style='padding:10px; border-radius:5px; background-color:#fff3cd; color:#856404;'><strong>Stage {i}:</strong> {outcome}</div>", unsafe_allow_html=True)
|
118 |
+
elif outcome_type == "error":
|
119 |
+
st.markdown(f"<div style='padding:10px; border-radius:5px; background-color:#f8d7da; color:#721c24;'><strong>Stage {i}:</strong> {outcome}</div>", unsafe_allow_html=True)
|
120 |
+
|
121 |
+
st.markdown("### Possibilities:")
|
122 |
+
stage_possibilities = possibilities.get(i, [("No specific possibilities defined", "info")])
|
123 |
+
for possibility, possibility_type in stage_possibilities:
|
124 |
+
if possibility_type == "success":
|
125 |
+
st.markdown(f"<div style='padding:5px; background-color:#d4edda; color:#155724;'><strong>{possibility}</strong></div>", unsafe_allow_html=True)
|
126 |
+
elif possibility_type == "info":
|
127 |
+
st.markdown(f"<div style='padding:5px; background-color:#cce5ff; color:#004085;'><strong>{possibility}</strong></div>", unsafe_allow_html=True)
|
128 |
+
elif possibility_type == "warning":
|
129 |
+
st.markdown(f"<div style='padding:5px; background-color:#fff3cd; color:#856404;'><strong>{possibility}</strong></div>", unsafe_allow_html=True)
|
130 |
+
elif possibility_type == "error":
|
131 |
+
st.markdown(f"<div style='padding:5px; background-color:#f8d7da; color:#721c24;'><strong>{possibility}</strong></div>", unsafe_allow_html=True)
|
132 |
+
|
133 |
+
with col2:
|
134 |
+
fig = create_tree_diagram(i)
|
135 |
+
st.plotly_chart(fig, use_container_width=True)
|
136 |
+
|
137 |
+
time.sleep(3) # Shorter delay for smoother transitions
|