Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
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")
|
@@ -54,29 +55,11 @@ possibilities = {
|
|
54 |
]
|
55 |
}
|
56 |
|
57 |
-
# Initialize
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
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
|
@@ -84,18 +67,18 @@ def create_tree_diagram(stage):
|
|
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 |
|
@@ -116,48 +99,61 @@ def create_tree_diagram(stage):
|
|
116 |
|
117 |
return fig
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
# Streamlit app layout
|
120 |
st.title("Precision Medicine AI Agents - Treatment Decision Tree")
|
121 |
|
122 |
-
#
|
123 |
-
st.
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
with
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
|
|
|
1 |
import streamlit as st
|
2 |
import time
|
3 |
import plotly.graph_objects as go
|
4 |
+
import random
|
5 |
|
6 |
# Set page layout to wide mode
|
7 |
st.set_page_config(layout="wide")
|
|
|
55 |
]
|
56 |
}
|
57 |
|
58 |
+
# Initialize a dictionary to keep track of patient counts for each possibility
|
59 |
+
patient_counts = {i: [0] * len(possibilities[i]) for i in possibilities}
|
60 |
+
|
61 |
+
# Create a tree diagram using Plotly
|
62 |
+
def create_tree_diagram(stage, selected_box=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
labels = [f"Stage {stage}: {outcomes[stage - 1][0]}"]
|
64 |
parents = [""]
|
65 |
values = [1] # Root node value
|
|
|
67 |
|
68 |
stage_possibilities = possibilities.get(stage, [("No specific possibilities defined", "info")])
|
69 |
|
70 |
+
for idx, (possibility, outcome_type) in enumerate(stage_possibilities):
|
71 |
+
labels.append(f"{possibility} - Patients: {patient_counts[stage][idx]}")
|
72 |
parents.append(f"Stage {stage}: {outcomes[stage - 1][0]}")
|
73 |
values.append(1) # Equal weight for all possibilities
|
74 |
if outcome_type == "success":
|
75 |
+
colors.append('#d4edda' if idx != selected_box else '#28a745')
|
76 |
elif outcome_type == "info":
|
77 |
+
colors.append('#cce5ff' if idx != selected_box else '#007bff')
|
78 |
elif outcome_type == "warning":
|
79 |
+
colors.append('#fff3cd' if idx != selected_box else '#ffc107')
|
80 |
elif outcome_type == "error":
|
81 |
+
colors.append('#f8d7da' if idx != selected_box else '#dc3545')
|
82 |
else:
|
83 |
colors.append('lightgrey')
|
84 |
|
|
|
99 |
|
100 |
return fig
|
101 |
|
102 |
+
# Function to increment patient counts in random boxes for each stage
|
103 |
+
def run_simulation():
|
104 |
+
for stage in possibilities:
|
105 |
+
selected_box = random.randint(0, len(possibilities[stage]) - 1)
|
106 |
+
patient_counts[stage][selected_box] += 1
|
107 |
+
st.session_state[f'selected_box_{stage}'] = selected_box
|
108 |
+
|
109 |
+
# Initialize the selected box in session state if it doesn't exist
|
110 |
+
for stage in possibilities:
|
111 |
+
if f'selected_box_{stage}' not in st.session_state:
|
112 |
+
st.session_state[f'selected_box_{stage}'] = None
|
113 |
+
|
114 |
# Streamlit app layout
|
115 |
st.title("Precision Medicine AI Agents - Treatment Decision Tree")
|
116 |
|
117 |
+
# Add a "Run Simulation" button
|
118 |
+
run_button = st.button("Run Simulation")
|
119 |
+
|
120 |
+
# If the button is pressed, run the simulation
|
121 |
+
if run_button:
|
122 |
+
run_simulation()
|
123 |
+
|
124 |
+
# Render the animation with the updated patient counts
|
125 |
+
for i, (outcome, outcome_type) in enumerate(outcomes, 1):
|
126 |
+
with st.container():
|
127 |
+
col1, col2 = st.columns([1, 2])
|
128 |
+
|
129 |
+
with col1:
|
130 |
+
if outcome_type == "success":
|
131 |
+
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)
|
132 |
+
elif outcome_type == "info":
|
133 |
+
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)
|
134 |
+
elif outcome_type == "warning":
|
135 |
+
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)
|
136 |
+
elif outcome_type == "error":
|
137 |
+
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)
|
138 |
+
|
139 |
+
st.markdown("### Possibilities:")
|
140 |
+
stage_possibilities = possibilities.get(i, [("No specific possibilities defined", "info")])
|
141 |
+
for idx, (possibility, possibility_type) in enumerate(stage_possibilities):
|
142 |
+
color = 'lightgrey'
|
143 |
+
if possibility_type == "success":
|
144 |
+
color = '#d4edda'
|
145 |
+
elif possibility_type == "info":
|
146 |
+
color = '#cce5ff'
|
147 |
+
elif possibility_type == "warning":
|
148 |
+
color = '#fff3cd'
|
149 |
+
elif possibility_type == "error":
|
150 |
+
color = '#f8d7da'
|
151 |
+
|
152 |
+
# Display the possibility and its patient count
|
153 |
+
st.markdown(f"<div style='padding:5px; background-color:{color};'><strong>{possibility} - Patients: {patient_counts[i][idx]}</strong></div>", unsafe_allow_html=True)
|
154 |
+
|
155 |
+
with col2:
|
156 |
+
selected_box = st.session_state[f'selected_box_{i}']
|
157 |
+
fig = create_tree_diagram(i, selected_box)
|
158 |
+
st.plotly_chart(fig, use_container_width=True)
|
159 |
+
|