Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,137 +1,157 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 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 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 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 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
st.
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 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
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
# Initialize constants
|
| 6 |
+
DOCTOR_ACTIONS = ["Prescribe Medication", "Recommend Tests", "Consult Clinician", "Schedule Surgery"]
|
| 7 |
+
NURSE_ACTIONS = ["Monitor Vitals", "Administer Medication", "Report to Doctor", "Assist Surgery"]
|
| 8 |
+
PATIENT_CONDITIONS = ["Healthy", "Mild Illness", "Chronic Illness", "Emergency"]
|
| 9 |
+
DOCTOR_EMOTIONS = ["Calm", "Stressed", "Overwhelmed"]
|
| 10 |
+
NURSE_EMOTIONS = ["Focused", "Fatigued", "Panicked"]
|
| 11 |
+
|
| 12 |
+
# Rewards and Penalties
|
| 13 |
+
REWARDS = {
|
| 14 |
+
"Prescribe Medication": 12,
|
| 15 |
+
"Recommend Tests": 7,
|
| 16 |
+
"Consult Clinician": 9,
|
| 17 |
+
"Schedule Surgery": 17,
|
| 18 |
+
"Monitor Vitals": 4,
|
| 19 |
+
"Administer Medication": 12,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
}
|
| 21 |
+
PENALTIES = {
|
| 22 |
+
"Wrong Medication": -5,
|
| 23 |
+
"Missed Diagnosis": -10,
|
| 24 |
+
"Stress-Induced Mistake": -7,
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# Initialize session state for metrics and satisfaction
|
| 28 |
+
if "performance_metrics" not in st.session_state:
|
| 29 |
+
st.session_state.performance_metrics = {
|
| 30 |
+
"Doctor": {"successful_treatments": 0, "failed_treatments": 0},
|
| 31 |
+
"Nurse": {"successful_assists": 0, "failed_assists": 0}
|
| 32 |
+
}
|
| 33 |
+
if "patient_satisfaction" not in st.session_state:
|
| 34 |
+
st.session_state.patient_satisfaction = 100
|
| 35 |
|
| 36 |
+
|
| 37 |
+
# Define the Agent class for Q-learning
|
| 38 |
+
class Agent:
|
| 39 |
+
def __init__(self, agent_type):
|
| 40 |
+
self.agent_type = agent_type
|
| 41 |
+
self.actions = DOCTOR_ACTIONS if agent_type == "Doctor" else NURSE_ACTIONS
|
| 42 |
+
self.q_table = np.zeros((len(PATIENT_CONDITIONS), len(self.actions)))
|
| 43 |
|
| 44 |
+
def choose_action(self, state, exploration_rate=0.05):
|
| 45 |
+
if random.uniform(0, 1) < exploration_rate: # Explore
|
| 46 |
+
return random.randint(0, len(self.actions)-1)
|
| 47 |
+
else: # Exploit (choose best action)
|
| 48 |
+
return np.argmax(self.q_table[state])
|
| 49 |
+
|
| 50 |
+
def update_q_value(self, state, action, reward, learning_rate=0.1, discount_factor=0.9):
|
| 51 |
+
old_q_value = self.q_table[state, action]
|
| 52 |
+
best_future_q_value = np.max(self.q_table)
|
| 53 |
+
new_q_value = old_q_value + learning_rate * (reward + discount_factor * best_future_q_value - old_q_value)
|
| 54 |
+
self.q_table[state, action] = new_q_value
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# Instantiate agents
|
| 58 |
+
doctor_agent = Agent("Doctor")
|
| 59 |
+
nurse_agent = Agent("Nurse")
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# Function to simulate a special event
|
| 63 |
+
def simulate_special_event():
|
| 64 |
+
event = random.choice([None, "Disease Outbreak", "Resource Shortage"])
|
| 65 |
+
if event == "Disease Outbreak":
|
| 66 |
+
st.subheader("Special Event: Disease Outbreak")
|
| 67 |
+
st.write("A sudden disease outbreak has flooded the hospital with new patients. Resources are limited!")
|
| 68 |
+
elif event == "Resource Shortage":
|
| 69 |
+
st.subheader("Special Event: Resource Shortage")
|
| 70 |
+
st.write("A medical supply shortage is impacting the hospital. Staff must prioritize high-risk patients.")
|
| 71 |
+
return event
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# Function to handle complications during treatment
|
| 75 |
+
def handle_complications():
|
| 76 |
+
complication = random.choices(
|
| 77 |
+
[None, "Allergic Reaction", "Unexpected Complication"],
|
| 78 |
+
weights=[0.6, 0.2, 0.2]
|
| 79 |
+
)[0]
|
| 80 |
|
| 81 |
+
penalty = 0
|
| 82 |
+
if complication:
|
| 83 |
+
st.subheader(f"Complication: {complication}")
|
| 84 |
+
if complication == "Allergic Reaction":
|
| 85 |
+
st.write("The patient has developed an allergic reaction to the prescribed medication!")
|
| 86 |
+
penalty = PENALTIES["Wrong Medication"]
|
| 87 |
+
st.session_state.patient_satisfaction -= 10
|
| 88 |
+
elif complication == "Unexpected Complication":
|
| 89 |
+
st.write("An unexpected complication occurred during surgery!")
|
| 90 |
+
penalty = PENALTIES["Stress-Induced Mistake"]
|
| 91 |
+
st.session_state.patient_satisfaction -= 15
|
| 92 |
+
return penalty
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# Main simulation button logic
|
| 96 |
+
if st.button("Run Simulation"):
|
| 97 |
+
# Simulate a special event
|
| 98 |
+
special_event = simulate_special_event()
|
| 99 |
+
|
| 100 |
+
# Patient condition simulation
|
| 101 |
+
patient_state = random.choice(PATIENT_CONDITIONS)
|
| 102 |
+
st.write(f"Simulated Patient Condition: {patient_state}")
|
| 103 |
+
patient_index = PATIENT_CONDITIONS.index(patient_state)
|
| 104 |
+
|
| 105 |
+
# Doctor and nurse emotions
|
| 106 |
+
doctor_emotion = random.choice(DOCTOR_EMOTIONS)
|
| 107 |
+
nurse_emotion = random.choice(NURSE_EMOTIONS)
|
| 108 |
+
st.write(f"Doctor's Emotional State: {doctor_emotion}")
|
| 109 |
+
st.write(f"Nurse's Emotional State: {nurse_emotion}")
|
| 110 |
+
|
| 111 |
+
# Doctor Action
|
| 112 |
+
doctor_action_index = doctor_agent.choose_action(patient_index)
|
| 113 |
+
doctor_action = DOCTOR_ACTIONS[doctor_action_index]
|
| 114 |
+
st.write(f"Doctor's Chosen Action: {doctor_action}")
|
| 115 |
+
|
| 116 |
+
# Nurse Action
|
| 117 |
+
nurse_action_index = nurse_agent.choose_action(patient_index)
|
| 118 |
+
nurse_action = NURSE_ACTIONS[nurse_action_index]
|
| 119 |
+
st.write(f"Nurse's Chosen Action: {nurse_action}")
|
| 120 |
+
|
| 121 |
+
# Handle potential complications
|
| 122 |
+
penalty = handle_complications()
|
| 123 |
|
| 124 |
+
# Reward or penalty
|
| 125 |
+
reward = REWARDS.get(doctor_action, 0) if penalty == 0 else penalty
|
| 126 |
+
if doctor_emotion in ["Stressed", "Overwhelmed"]:
|
| 127 |
+
penalty += PENALTIES["Stress-Induced Mistake"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
# Update Q-values
|
| 130 |
+
doctor_agent.update_q_value(patient_index, doctor_action_index, reward)
|
| 131 |
+
|
| 132 |
+
st.write(f"Doctor's Reward/Penalty: {reward}")
|
| 133 |
+
st.write(f"Patient Satisfaction: {st.session_state.patient_satisfaction}")
|
| 134 |
+
|
| 135 |
+
# Outcome and Performance Metrics Update
|
| 136 |
+
outcome = random.choices(
|
| 137 |
+
["Recovery", "Further Treatment Needed", "Complication"],
|
| 138 |
+
weights=[0.6, 0.3, 0.1]
|
| 139 |
+
)[0]
|
| 140 |
+
|
| 141 |
+
st.write(f"Patient Status after Treatment: {outcome}")
|
| 142 |
+
|
| 143 |
+
if outcome == "Recovery":
|
| 144 |
+
st.session_state.performance_metrics["Doctor"]["successful_treatments"] += 1
|
| 145 |
+
st.session_state.performance_metrics["Nurse"]["successful_assists"] += 1
|
| 146 |
+
else:
|
| 147 |
+
st.session_state.performance_metrics["Doctor"]["failed_treatments"] += 1
|
| 148 |
+
st.session_state.performance_metrics["Nurse"]["failed_assists"] += 1
|
| 149 |
+
|
| 150 |
+
st.write("Simulation completed! Run again for different outcomes.")
|
| 151 |
+
|
| 152 |
+
# Display performance metrics
|
| 153 |
+
st.subheader("Performance Metrics:")
|
| 154 |
+
st.write(f"Doctor's Successful Treatments: {st.session_state.performance_metrics['Doctor']['successful_treatments']}")
|
| 155 |
+
st.write(f"Doctor's Failed Treatments: {st.session_state.performance_metrics['Doctor']['failed_treatments']}")
|
| 156 |
+
st.write(f"Nurse's Successful Assists: {st.session_state.performance_metrics['Nurse']['successful_assists']}")
|
| 157 |
+
st.write(f"Nurse's Failed Assists: {st.session_state.performance_metrics['Nurse']['failed_assists']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|