Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,99 @@
|
|
| 1 |
-
import json
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
st.write(f"Interacting with {agent_info['name']}")
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
# Submit the interaction
|
| 28 |
-
if st.button("Submit"):
|
| 29 |
-
st.write(f"Interactions for {agent_type} have been submitted.")
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import random
|
| 4 |
|
| 5 |
+
# Initialize agents (doctors, nurses, clinicians, patients)
|
| 6 |
+
agents = {
|
| 7 |
+
"Doctors": 5,
|
| 8 |
+
"Nurses": 10,
|
| 9 |
+
"Clinicians": 8,
|
| 10 |
+
"Patients": 200
|
| 11 |
+
}
|
| 12 |
|
| 13 |
+
# Define states for patients (health conditions)
|
| 14 |
+
patient_conditions = ["Healthy", "Mild Illness", "Chronic Illness", "Emergency"]
|
| 15 |
|
| 16 |
+
# Define the actions doctors can take
|
| 17 |
+
doctor_actions = ["Prescribe Medication", "Recommend Tests", "Consult Clinician", "Schedule Surgery"]
|
| 18 |
|
| 19 |
+
# Define the actions nurses can take
|
| 20 |
+
nurse_actions = ["Monitor Vitals", "Administer Medication", "Report to Doctor", "Assist Surgery"]
|
|
|
|
| 21 |
|
| 22 |
+
# Define the actions clinicians can take
|
| 23 |
+
clinician_actions = ["Review Diagnostic Test", "Consult with Doctor", "Recommend Additional Tests"]
|
| 24 |
|
| 25 |
+
# Reward and penalty system (simplified for Reinforcement Learning concept)
|
| 26 |
+
rewards = {
|
| 27 |
+
"Prescribe Medication": 10,
|
| 28 |
+
"Recommend Tests": 5,
|
| 29 |
+
"Consult Clinician": 7,
|
| 30 |
+
"Schedule Surgery": 15,
|
| 31 |
+
"Monitor Vitals": 3,
|
| 32 |
+
"Administer Medication": 10,
|
| 33 |
+
"Review Diagnostic Test": 6,
|
| 34 |
+
"Recommend Additional Tests": 4
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
penalties = {
|
| 38 |
+
"Wrong Medication": -10,
|
| 39 |
+
"Missed Diagnosis": -20,
|
| 40 |
+
"Incorrect Test Recommendation": -5
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# Define Reinforcement Learning algorithm (basic Q-learning approach)
|
| 44 |
+
class Agent:
|
| 45 |
+
def __init__(self, agent_type):
|
| 46 |
+
self.agent_type = agent_type
|
| 47 |
+
self.q_table = np.zeros((len(patient_conditions), len(doctor_actions if agent_type == "Doctor" else nurse_actions)))
|
| 48 |
+
self.state = random.choice(patient_conditions)
|
| 49 |
+
|
| 50 |
+
def choose_action(self, exploration_rate=0.1):
|
| 51 |
+
if random.uniform(0, 1) < exploration_rate: # Explore
|
| 52 |
+
return random.randint(0, len(doctor_actions)-1 if self.agent_type == "Doctor" else len(nurse_actions)-1)
|
| 53 |
+
else: # Exploit (choose the best action based on Q-values)
|
| 54 |
+
return np.argmax(self.q_table)
|
| 55 |
+
|
| 56 |
+
def update_q_value(self, state, action, reward, learning_rate=0.1, discount_factor=0.95):
|
| 57 |
+
old_q_value = self.q_table[state, action]
|
| 58 |
+
best_future_q_value = np.max(self.q_table)
|
| 59 |
+
new_q_value = old_q_value + learning_rate * (reward + discount_factor * best_future_q_value - old_q_value)
|
| 60 |
+
self.q_table[state, action] = new_q_value
|
| 61 |
+
|
| 62 |
+
# Instantiate agents
|
| 63 |
+
doctor_agent = Agent("Doctor")
|
| 64 |
+
nurse_agent = Agent("Nurse")
|
| 65 |
+
|
| 66 |
+
# Streamlit UI
|
| 67 |
+
st.title("Healthcare Civilization - Multi-Agent Simulation")
|
| 68 |
+
|
| 69 |
+
st.write(f"Current number of agents in the system:")
|
| 70 |
+
for agent, count in agents.items():
|
| 71 |
+
st.write(f"{agent}: {count}")
|
| 72 |
+
|
| 73 |
+
# Patient Condition Simulation
|
| 74 |
+
patient_state = random.choice(patient_conditions)
|
| 75 |
+
st.write(f"Simulated Patient Condition: {patient_state}")
|
| 76 |
+
|
| 77 |
+
# Doctor Action Simulation
|
| 78 |
+
doctor_action = doctor_agent.choose_action()
|
| 79 |
+
st.write(f"Doctor's Chosen Action: {doctor_actions[doctor_action]}")
|
| 80 |
+
|
| 81 |
+
# Nurse Action Simulation
|
| 82 |
+
nurse_action = nurse_agent.choose_action()
|
| 83 |
+
st.write(f"Nurse's Chosen Action: {nurse_actions[nurse_action]}")
|
| 84 |
+
|
| 85 |
+
# Reward or penalty based on action
|
| 86 |
+
reward = rewards.get(doctor_actions[doctor_action], 0)
|
| 87 |
+
penalty = penalties.get("Wrong Medication" if doctor_action == 0 and patient_state == "Healthy" else "", 0)
|
| 88 |
+
|
| 89 |
+
# Update Q-values
|
| 90 |
+
doctor_agent.update_q_value(patient_conditions.index(patient_state), doctor_action, reward if penalty == 0 else penalty)
|
| 91 |
+
|
| 92 |
+
st.write(f"Doctor's Reward: {reward if penalty == 0 else penalty}")
|
| 93 |
+
|
| 94 |
+
# Simulated patient feedback and next steps
|
| 95 |
+
next_step = random.choice(["Recovery", "Further Treatment Needed", "Complication"])
|
| 96 |
+
st.write(f"Patient Status after treatment: {next_step}")
|
| 97 |
+
|
| 98 |
+
st.write("Simulation completed! Run again to simulate different actions and outcomes.")
|
| 99 |
|
|
|
|
|
|
|
|
|