Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import random
|
|
|
|
| 4 |
|
| 5 |
# Initialize agents (doctors, nurses, clinicians, patients)
|
| 6 |
agents = {
|
|
@@ -25,8 +26,8 @@ nurse_actions = ["Monitor Vitals", "Administer Medication", "Report to Doctor",
|
|
| 25 |
|
| 26 |
# Reward and penalty system
|
| 27 |
rewards = {
|
| 28 |
-
"Prescribe Medication": 12,
|
| 29 |
-
"Recommend Tests": 7,
|
| 30 |
"Consult Clinician": 9,
|
| 31 |
"Schedule Surgery": 17,
|
| 32 |
"Monitor Vitals": 4,
|
|
@@ -36,7 +37,7 @@ rewards = {
|
|
| 36 |
}
|
| 37 |
|
| 38 |
penalties = {
|
| 39 |
-
"Wrong Medication": -5,
|
| 40 |
"Missed Diagnosis": -10,
|
| 41 |
"Incorrect Test Recommendation": -3,
|
| 42 |
"Stress-Induced Mistake": -7
|
|
@@ -58,13 +59,13 @@ class Agent:
|
|
| 58 |
self.q_table = np.zeros((len(patient_conditions), len(doctor_actions if agent_type == "Doctor" else nurse_actions)))
|
| 59 |
self.state = random.choice(patient_conditions)
|
| 60 |
|
| 61 |
-
def choose_action(self, exploration_rate=0.03):
|
| 62 |
-
if random.uniform(0, 1) < exploration_rate:
|
| 63 |
return random.randint(0, len(doctor_actions)-1 if self.agent_type == "Doctor" else len(nurse_actions)-1)
|
| 64 |
-
else:
|
| 65 |
return np.argmax(self.q_table)
|
| 66 |
|
| 67 |
-
def update_q_value(self, state, action, reward, learning_rate=0.25, discount_factor=0.95):
|
| 68 |
old_q_value = self.q_table[state, action]
|
| 69 |
best_future_q_value = np.max(self.q_table)
|
| 70 |
new_q_value = old_q_value + learning_rate * (reward + discount_factor * best_future_q_value - old_q_value)
|
|
@@ -74,60 +75,68 @@ class Agent:
|
|
| 74 |
doctor_agent = Agent("Doctor")
|
| 75 |
nurse_agent = Agent("Nurse")
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# Button to run the simulation
|
| 78 |
if st.button("Run Simulation"):
|
|
|
|
|
|
|
| 79 |
# Simulate special event (disease outbreak or resource shortage)
|
| 80 |
special_event = random.choice([None, "Disease Outbreak", "Resource Shortage"])
|
| 81 |
if special_event:
|
| 82 |
-
|
| 83 |
if special_event == "Disease Outbreak":
|
| 84 |
-
|
| 85 |
elif special_event == "Resource Shortage":
|
| 86 |
-
|
| 87 |
|
| 88 |
# Patient Condition Simulation
|
| 89 |
patient_state = random.choice(patient_conditions)
|
| 90 |
-
|
| 91 |
|
| 92 |
# Random doctor and nurse emotions (affects performance)
|
| 93 |
doctor_emotion = random.choice(doctor_emotions)
|
| 94 |
nurse_emotion = random.choice(nurse_emotions)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
# Doctor Action Simulation
|
| 99 |
doctor_action = doctor_agent.choose_action()
|
| 100 |
-
|
| 101 |
|
| 102 |
# Nurse Action Simulation
|
| 103 |
nurse_action = nurse_agent.choose_action()
|
| 104 |
-
|
| 105 |
|
| 106 |
# Complications that can arise during treatment
|
| 107 |
complication = random.choices(
|
| 108 |
[None, "Allergic Reaction", "Unexpected Complication"],
|
| 109 |
-
weights=[0.6, 0.2, 0.2]
|
| 110 |
)[0]
|
| 111 |
|
| 112 |
if complication:
|
| 113 |
-
|
| 114 |
if complication == "Allergic Reaction":
|
| 115 |
-
|
| 116 |
penalty = penalties.get("Wrong Medication", 0)
|
| 117 |
st.session_state.patient_satisfaction -= 10
|
| 118 |
elif complication == "Unexpected Complication":
|
| 119 |
-
|
| 120 |
penalty = penalties.get("Stress-Induced Mistake", 0)
|
| 121 |
st.session_state.patient_satisfaction -= 15
|
| 122 |
else:
|
| 123 |
penalty = 0
|
| 124 |
|
| 125 |
# Ensure action constraints for healthy patients
|
| 126 |
-
if patient_state == "Healthy" and doctor_action == 0:
|
| 127 |
penalty = penalties.get("Wrong Medication", 0)
|
| 128 |
-
|
| 129 |
st.session_state.patient_satisfaction -= 20
|
| 130 |
-
next_step = "Complication" # Force a complication due to unnecessary treatment
|
| 131 |
|
| 132 |
# Reward or penalty based on action and emotional state
|
| 133 |
reward = rewards.get(doctor_actions[doctor_action], 0)
|
|
@@ -137,15 +146,15 @@ if st.button("Run Simulation"):
|
|
| 137 |
# Update Q-values
|
| 138 |
doctor_agent.update_q_value(patient_conditions.index(patient_state), doctor_action, reward if penalty == 0 else penalty)
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
|
| 143 |
# Simulated patient feedback and next steps
|
| 144 |
next_step = random.choices(
|
| 145 |
["Recovery", "Further Treatment Needed", "Complication"],
|
| 146 |
-
weights=[0.6, 0.3, 0.1]
|
| 147 |
)[0]
|
| 148 |
-
|
| 149 |
|
| 150 |
# Increment performance based on outcome
|
| 151 |
if next_step == "Recovery":
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
+
import time # For animation delays
|
| 5 |
|
| 6 |
# Initialize agents (doctors, nurses, clinicians, patients)
|
| 7 |
agents = {
|
|
|
|
| 26 |
|
| 27 |
# Reward and penalty system
|
| 28 |
rewards = {
|
| 29 |
+
"Prescribe Medication": 12,
|
| 30 |
+
"Recommend Tests": 7,
|
| 31 |
"Consult Clinician": 9,
|
| 32 |
"Schedule Surgery": 17,
|
| 33 |
"Monitor Vitals": 4,
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
penalties = {
|
| 40 |
+
"Wrong Medication": -5,
|
| 41 |
"Missed Diagnosis": -10,
|
| 42 |
"Incorrect Test Recommendation": -3,
|
| 43 |
"Stress-Induced Mistake": -7
|
|
|
|
| 59 |
self.q_table = np.zeros((len(patient_conditions), len(doctor_actions if agent_type == "Doctor" else nurse_actions)))
|
| 60 |
self.state = random.choice(patient_conditions)
|
| 61 |
|
| 62 |
+
def choose_action(self, exploration_rate=0.03):
|
| 63 |
+
if random.uniform(0, 1) < exploration_rate:
|
| 64 |
return random.randint(0, len(doctor_actions)-1 if self.agent_type == "Doctor" else len(nurse_actions)-1)
|
| 65 |
+
else:
|
| 66 |
return np.argmax(self.q_table)
|
| 67 |
|
| 68 |
+
def update_q_value(self, state, action, reward, learning_rate=0.25, discount_factor=0.95):
|
| 69 |
old_q_value = self.q_table[state, action]
|
| 70 |
best_future_q_value = np.max(self.q_table)
|
| 71 |
new_q_value = old_q_value + learning_rate * (reward + discount_factor * best_future_q_value - old_q_value)
|
|
|
|
| 75 |
doctor_agent = Agent("Doctor")
|
| 76 |
nurse_agent = Agent("Nurse")
|
| 77 |
|
| 78 |
+
# Animation function for smoother transitions
|
| 79 |
+
def animate_text(text, delay=0.1):
|
| 80 |
+
for char in text:
|
| 81 |
+
st.write(char, end="")
|
| 82 |
+
time.sleep(delay)
|
| 83 |
+
st.write("\n")
|
| 84 |
+
|
| 85 |
# Button to run the simulation
|
| 86 |
if st.button("Run Simulation"):
|
| 87 |
+
st.subheader("Simulation Running...")
|
| 88 |
+
|
| 89 |
# Simulate special event (disease outbreak or resource shortage)
|
| 90 |
special_event = random.choice([None, "Disease Outbreak", "Resource Shortage"])
|
| 91 |
if special_event:
|
| 92 |
+
animate_text(f"Special Event: {special_event}")
|
| 93 |
if special_event == "Disease Outbreak":
|
| 94 |
+
animate_text("A sudden disease outbreak has flooded the hospital with new patients. Resources are limited!")
|
| 95 |
elif special_event == "Resource Shortage":
|
| 96 |
+
animate_text("A medical supply shortage is impacting the hospital. Staff must prioritize high-risk patients.")
|
| 97 |
|
| 98 |
# Patient Condition Simulation
|
| 99 |
patient_state = random.choice(patient_conditions)
|
| 100 |
+
animate_text(f"Simulated Patient Condition: {patient_state}")
|
| 101 |
|
| 102 |
# Random doctor and nurse emotions (affects performance)
|
| 103 |
doctor_emotion = random.choice(doctor_emotions)
|
| 104 |
nurse_emotion = random.choice(nurse_emotions)
|
| 105 |
+
animate_text(f"Doctor's Emotional State: {doctor_emotion}")
|
| 106 |
+
animate_text(f"Nurse's Emotional State: {nurse_emotion}")
|
| 107 |
|
| 108 |
# Doctor Action Simulation
|
| 109 |
doctor_action = doctor_agent.choose_action()
|
| 110 |
+
animate_text(f"Doctor's Chosen Action: {doctor_actions[doctor_action]}")
|
| 111 |
|
| 112 |
# Nurse Action Simulation
|
| 113 |
nurse_action = nurse_agent.choose_action()
|
| 114 |
+
animate_text(f"Nurse's Chosen Action: {nurse_actions[nurse_action]}")
|
| 115 |
|
| 116 |
# Complications that can arise during treatment
|
| 117 |
complication = random.choices(
|
| 118 |
[None, "Allergic Reaction", "Unexpected Complication"],
|
| 119 |
+
weights=[0.6, 0.2, 0.2]
|
| 120 |
)[0]
|
| 121 |
|
| 122 |
if complication:
|
| 123 |
+
animate_text(f"Complication: {complication}")
|
| 124 |
if complication == "Allergic Reaction":
|
| 125 |
+
animate_text("The patient has developed an allergic reaction to the prescribed medication!")
|
| 126 |
penalty = penalties.get("Wrong Medication", 0)
|
| 127 |
st.session_state.patient_satisfaction -= 10
|
| 128 |
elif complication == "Unexpected Complication":
|
| 129 |
+
animate_text("An unexpected complication occurred during surgery!")
|
| 130 |
penalty = penalties.get("Stress-Induced Mistake", 0)
|
| 131 |
st.session_state.patient_satisfaction -= 15
|
| 132 |
else:
|
| 133 |
penalty = 0
|
| 134 |
|
| 135 |
# Ensure action constraints for healthy patients
|
| 136 |
+
if patient_state == "Healthy" and doctor_action == 0:
|
| 137 |
penalty = penalties.get("Wrong Medication", 0)
|
| 138 |
+
animate_text("Prescribing medication to a healthy patient is unnecessary! Immediate penalty applied.")
|
| 139 |
st.session_state.patient_satisfaction -= 20
|
|
|
|
| 140 |
|
| 141 |
# Reward or penalty based on action and emotional state
|
| 142 |
reward = rewards.get(doctor_actions[doctor_action], 0)
|
|
|
|
| 146 |
# Update Q-values
|
| 147 |
doctor_agent.update_q_value(patient_conditions.index(patient_state), doctor_action, reward if penalty == 0 else penalty)
|
| 148 |
|
| 149 |
+
animate_text(f"Doctor's Reward: {reward if penalty == 0 else penalty}")
|
| 150 |
+
animate_text(f"Patient Satisfaction: {st.session_state.patient_satisfaction}")
|
| 151 |
|
| 152 |
# Simulated patient feedback and next steps
|
| 153 |
next_step = random.choices(
|
| 154 |
["Recovery", "Further Treatment Needed", "Complication"],
|
| 155 |
+
weights=[0.6, 0.3, 0.1]
|
| 156 |
)[0]
|
| 157 |
+
animate_text(f"Patient Status after treatment: {next_step}")
|
| 158 |
|
| 159 |
# Increment performance based on outcome
|
| 160 |
if next_step == "Recovery":
|