Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import streamlit as st
|
2 |
import time
|
3 |
-
import random
|
4 |
import plotly.graph_objects as go
|
5 |
|
6 |
# Set page layout to wide mode
|
@@ -20,114 +19,145 @@ outcomes = [
|
|
20 |
|
21 |
# Different possible outcomes of each treatment
|
22 |
possibilities = {
|
23 |
-
1: [
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
}
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
"
|
47 |
-
|
48 |
-
|
49 |
-
"
|
50 |
-
|
51 |
-
"
|
52 |
-
|
53 |
-
|
54 |
-
#
|
55 |
-
def
|
56 |
-
|
57 |
-
last_names = ["Smith", "Doe", "Brown", "Wilson", "Taylor", "Lee"]
|
58 |
-
return f"{random.choice(first_names)} {random.choice(last_names)}"
|
59 |
-
|
60 |
-
# Run simulation and update patient record based on random outcomes
|
61 |
-
def simulate_patient_outcome():
|
62 |
-
for stage in range(1, 9):
|
63 |
-
patient_name = generate_patient_name()
|
64 |
-
outcome_list = possibilities[stage]
|
65 |
-
outcome = random.choice(outcome_list)
|
66 |
-
patients_records[outcome] += 1 # Increment count for that outcome
|
67 |
-
|
68 |
-
# Create a tree diagram using Plotly with patient information
|
69 |
-
def create_tree_diagram():
|
70 |
-
labels = ["Patients"]
|
71 |
parents = [""]
|
72 |
-
values = [
|
73 |
-
colors = ['lightgrey']
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
88 |
))
|
89 |
|
90 |
fig.update_layout(
|
91 |
margin=dict(t=10, l=10, r=10, b=10),
|
92 |
width=600, height=400,
|
93 |
-
uniformtext=dict(minsize=12, mode='show')
|
94 |
-
transition_duration=500
|
95 |
)
|
96 |
|
97 |
return fig
|
98 |
|
99 |
# Streamlit app layout
|
100 |
-
st.title("Precision Medicine AI Agents - Treatment Decision Tree
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
# Add a start animation button
|
103 |
-
|
104 |
-
|
105 |
-
if
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import time
|
|
|
3 |
import plotly.graph_objects as go
|
4 |
|
5 |
# Set page layout to wide mode
|
|
|
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 |
+
# Initialize session state for patient counts if not already present
|
58 |
+
if 'well_count' not in st.session_state:
|
59 |
+
st.session_state['well_count'] = 0
|
60 |
+
if 'slight_improvement_count' not in st.session_state:
|
61 |
+
st.session_state['slight_improvement_count'] = 0
|
62 |
+
if 'no_response_count' not in st.session_state:
|
63 |
+
st.session_state['no_response_count'] = 0
|
64 |
+
|
65 |
+
# Function to simulate patient response
|
66 |
+
def simulate_patient_response():
|
67 |
+
# Simulating possible outcomes - can be adjusted for more complexity
|
68 |
+
# Example random patient responses (you can adjust this logic)
|
69 |
+
import random
|
70 |
+
outcome = random.choice(["well", "slight improvement", "no response"])
|
71 |
+
if outcome == "well":
|
72 |
+
st.session_state['well_count'] += 1
|
73 |
+
elif outcome == "slight improvement":
|
74 |
+
st.session_state['slight_improvement_count'] += 1
|
75 |
+
elif outcome == "no response":
|
76 |
+
st.session_state['no_response_count'] += 1
|
77 |
+
|
78 |
+
# Create a tree diagram using Plotly (unchanged from original)
|
79 |
+
def create_tree_diagram(stage):
|
80 |
+
labels = [f"Stage {stage}: {outcomes[stage - 1][0]}"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
parents = [""]
|
82 |
+
values = [1] # Root node value
|
83 |
+
colors = ['lightgrey'] # Root node color
|
84 |
|
85 |
+
stage_possibilities = possibilities.get(stage, [("No specific possibilities defined", "info")])
|
86 |
+
|
87 |
+
for possibility, outcome_type in stage_possibilities:
|
88 |
+
labels.append(possibility)
|
89 |
+
parents.append(f"Stage {stage}: {outcomes[stage - 1][0]}")
|
90 |
+
values.append(1) # Equal weight for all possibilities
|
91 |
+
if outcome_type == "success":
|
92 |
+
colors.append('#d4edda')
|
93 |
+
elif outcome_type == "info":
|
94 |
+
colors.append('#cce5ff')
|
95 |
+
elif outcome_type == "warning":
|
96 |
+
colors.append('#fff3cd')
|
97 |
+
elif outcome_type == "error":
|
98 |
+
colors.append('#f8d7da')
|
99 |
+
else:
|
100 |
+
colors.append('lightgrey')
|
101 |
|
102 |
fig = go.Figure(go.Treemap(
|
103 |
labels=labels,
|
104 |
parents=parents,
|
105 |
values=values,
|
106 |
marker=dict(colors=colors),
|
107 |
+
textinfo="label",
|
108 |
+
textfont=dict(size=14),
|
109 |
))
|
110 |
|
111 |
fig.update_layout(
|
112 |
margin=dict(t=10, l=10, r=10, b=10),
|
113 |
width=600, height=400,
|
114 |
+
uniformtext=dict(minsize=12, mode='show')
|
|
|
115 |
)
|
116 |
|
117 |
return fig
|
118 |
|
119 |
# Streamlit app layout
|
120 |
+
st.title("Precision Medicine AI Agents - Treatment Decision Tree")
|
121 |
+
|
122 |
+
# Display the current patient counts
|
123 |
+
st.write(f"### Well: {st.session_state['well_count']}")
|
124 |
+
st.write(f"### Slight Improvement: {st.session_state['slight_improvement_count']}")
|
125 |
+
st.write(f"### No Response: {st.session_state['no_response_count']}")
|
126 |
|
127 |
# Add a start animation button
|
128 |
+
start_button = st.button("Start Animation")
|
129 |
+
|
130 |
+
if start_button:
|
131 |
+
simulate_patient_response() # Simulate patient response when button is pressed
|
132 |
+
|
133 |
+
for i, (outcome, outcome_type) in enumerate(outcomes, 1):
|
134 |
+
with st.container():
|
135 |
+
col1, col2 = st.columns([1, 2])
|
136 |
+
|
137 |
+
with col1:
|
138 |
+
if outcome_type == "success":
|
139 |
+
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)
|
140 |
+
elif outcome_type == "info":
|
141 |
+
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)
|
142 |
+
elif outcome_type == "warning":
|
143 |
+
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)
|
144 |
+
elif outcome_type == "error":
|
145 |
+
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)
|
146 |
+
|
147 |
+
st.markdown("### Possibilities:")
|
148 |
+
stage_possibilities = possibilities.get(i, [("No specific possibilities defined", "info")])
|
149 |
+
for possibility, possibility_type in stage_possibilities:
|
150 |
+
if possibility_type == "success":
|
151 |
+
st.markdown(f"<div style='padding:5px; background-color:#d4edda; color:#155724;'><strong>{possibility}</strong></div>", unsafe_allow_html=True)
|
152 |
+
elif possibility_type == "info":
|
153 |
+
st.markdown(f"<div style='padding:5px; background-color:#cce5ff; color:#004085;'><strong>{possibility}</strong></div>", unsafe_allow_html=True)
|
154 |
+
elif possibility_type == "warning":
|
155 |
+
st.markdown(f"<div style='padding:5px; background-color:#fff3cd; color:#856404;'><strong>{possibility}</strong></div>", unsafe_allow_html=True)
|
156 |
+
elif possibility_type == "error":
|
157 |
+
st.markdown(f"<div style='padding:5px; background-color:#f8d7da; color:#721c24;'><strong>{possibility}</strong></div>", unsafe_allow_html=True)
|
158 |
+
|
159 |
+
with col2:
|
160 |
+
fig = create_tree_diagram(i)
|
161 |
+
st.plotly_chart(fig, use_container_width=True)
|
162 |
+
|
163 |
+
time.sleep(5)
|