Spaces:
Sleeping
Sleeping
Update components/animation_engineer.py
Browse files- components/animation_engineer.py +80 -99
components/animation_engineer.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
-
import
|
| 5 |
-
import numpy as np
|
| 6 |
|
| 7 |
class AnimationEngineer:
|
| 8 |
def __init__(self):
|
|
@@ -14,105 +13,87 @@ class AnimationEngineer:
|
|
| 14 |
'physiotherapy': '#FFD700', # Gold
|
| 15 |
'cognitive therapy': '#DDA0DD' # Plum
|
| 16 |
}
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
def display_care_plan_animation(self, final_plan
|
| 19 |
st.write("Visualizing Care Plan...")
|
| 20 |
steps = final_plan.split(", ")
|
| 21 |
|
| 22 |
-
# Create
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
# Care Plan Progress Indicator
|
| 38 |
-
fig.add_trace(go.Indicator(
|
| 39 |
-
mode="gauge+number",
|
| 40 |
-
value=0,
|
| 41 |
-
title={'text': "Care Plan Progress"},
|
| 42 |
-
gauge={'axis': {'range': [None, len(steps)]},
|
| 43 |
-
'steps': [{'range': [0, len(steps)], 'color': "lightgray"}],
|
| 44 |
-
'threshold': {'line': {'color': "red", 'width': 4}, 'thickness': 0.75, 'value': len(steps)}},
|
| 45 |
-
domain={'row': 0, 'column': 0}
|
| 46 |
-
))
|
| 47 |
-
|
| 48 |
-
# Patient Vitals Line Chart
|
| 49 |
-
vitals = np.random.rand(10) * 10 + 90 # Simulated vital signs data
|
| 50 |
-
fig.add_trace(go.Scatter(
|
| 51 |
-
x=list(range(10)), y=vitals, mode='lines+markers', name='Vitals',
|
| 52 |
-
line=dict(color='royalblue', width=2)
|
| 53 |
-
), row=1, col=2)
|
| 54 |
-
|
| 55 |
-
# Treatment Effectiveness Bar Chart
|
| 56 |
-
fig.add_trace(go.Bar(
|
| 57 |
-
x=df['Step'], y=df['Value'], name='Effectiveness',
|
| 58 |
-
marker_color=[self.colors.get(step.split()[0], '#FFFFFF') for step in df['Description']]
|
| 59 |
-
), row=2, col=1)
|
| 60 |
-
|
| 61 |
-
# Overall Health Score Indicator
|
| 62 |
-
fig.add_trace(go.Indicator(
|
| 63 |
-
mode="gauge+number",
|
| 64 |
-
value=75, # Starting value
|
| 65 |
-
title={'text': "Overall Health Score"},
|
| 66 |
-
gauge={'axis': {'range': [None, 100]},
|
| 67 |
-
'steps': [{'range': [0, 50], 'color': "lightgray"},
|
| 68 |
-
{'range': [50, 75], 'color': "gray"}],
|
| 69 |
-
'threshold': {'line': {'color': "red", 'width': 4}, 'thickness': 0.75, 'value': 90}},
|
| 70 |
-
domain={'row': 1, 'column': 1}
|
| 71 |
-
))
|
| 72 |
-
|
| 73 |
-
# Update layout
|
| 74 |
-
fig.update_layout(height=800, showlegend=False)
|
| 75 |
-
|
| 76 |
-
# Display the initial plot
|
| 77 |
-
plot_placeholder = st.empty()
|
| 78 |
-
plot_placeholder.plotly_chart(fig, use_container_width=True)
|
| 79 |
-
|
| 80 |
-
# Animate the care plan steps
|
| 81 |
-
for i in range(len(steps)):
|
| 82 |
-
# Update Care Plan Progress
|
| 83 |
-
fig.data[0].value = i + 1
|
| 84 |
-
|
| 85 |
-
# Update Patient Vitals
|
| 86 |
-
new_vital = np.random.rand() * 10 + 90
|
| 87 |
-
fig.data[1].y = list(fig.data[1].y) + [new_vital]
|
| 88 |
-
fig.data[1].x = list(range(len(fig.data[1].y)))
|
| 89 |
-
|
| 90 |
-
# Update Treatment Effectiveness
|
| 91 |
-
fig.data[2].marker.color = [self.colors.get(step.split()[0], '#FFFFFF') if idx <= i else 'lightgray'
|
| 92 |
-
for idx, step in enumerate(df['Description'])]
|
| 93 |
-
|
| 94 |
-
# Update Overall Health Score
|
| 95 |
-
fig.data[3].value = min(100, fig.data[3].value + np.random.randint(-5, 10))
|
| 96 |
-
|
| 97 |
-
# Update the plot
|
| 98 |
-
plot_placeholder.plotly_chart(fig, use_container_width=True)
|
| 99 |
-
|
| 100 |
-
# Add a short pause between updates
|
| 101 |
-
st.empty().text(f"Step {i+1}: {steps[i]}")
|
| 102 |
-
st.empty().text(f"Processing...")
|
| 103 |
-
|
| 104 |
st.success("Care Plan Visualization Complete!")
|
| 105 |
st.balloons()
|
| 106 |
|
| 107 |
-
def
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import time
|
| 3 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 4 |
+
import io
|
|
|
|
| 5 |
|
| 6 |
class AnimationEngineer:
|
| 7 |
def __init__(self):
|
|
|
|
| 13 |
'physiotherapy': '#FFD700', # Gold
|
| 14 |
'cognitive therapy': '#DDA0DD' # Plum
|
| 15 |
}
|
| 16 |
+
self.frame_width = 600
|
| 17 |
+
self.frame_height = 400
|
| 18 |
|
| 19 |
+
def display_care_plan_animation(self, final_plan):
|
| 20 |
st.write("Visualizing Care Plan...")
|
| 21 |
steps = final_plan.split(", ")
|
| 22 |
|
| 23 |
+
# Create all frames
|
| 24 |
+
frames = self.create_all_frames(steps)
|
| 25 |
+
|
| 26 |
+
# Create a placeholder for our animation
|
| 27 |
+
animation_placeholder = st.empty()
|
| 28 |
+
|
| 29 |
+
# Display frames
|
| 30 |
+
for i, frame in enumerate(frames):
|
| 31 |
+
animation_placeholder.image(frame)
|
| 32 |
+
progress = int((i + 1) / len(frames) * 100)
|
| 33 |
+
st.progress(progress)
|
| 34 |
+
time.sleep(0.5) # Adjust speed of animation
|
| 35 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
st.success("Care Plan Visualization Complete!")
|
| 37 |
st.balloons()
|
| 38 |
|
| 39 |
+
def create_all_frames(self, steps):
|
| 40 |
+
frames = []
|
| 41 |
+
|
| 42 |
+
# Create intro frame
|
| 43 |
+
intro_frame = self.create_intro_frame()
|
| 44 |
+
frames.append(intro_frame)
|
| 45 |
+
|
| 46 |
+
# Create step frames
|
| 47 |
+
for i, step in enumerate(steps):
|
| 48 |
+
step_frame = self.create_step_frame(step, i+1, len(steps))
|
| 49 |
+
frames.append(step_frame)
|
| 50 |
+
|
| 51 |
+
# Create transition frames
|
| 52 |
+
transition_frames = self.create_transition_frames()
|
| 53 |
+
frames.extend(transition_frames)
|
| 54 |
+
|
| 55 |
+
# Create final frame
|
| 56 |
+
final_frame = self.create_final_frame()
|
| 57 |
+
frames.append(final_frame)
|
| 58 |
+
|
| 59 |
+
return frames
|
| 60 |
+
|
| 61 |
+
def create_intro_frame(self):
|
| 62 |
+
img = Image.new('RGB', (self.frame_width, self.frame_height), color='white')
|
| 63 |
+
d = ImageDraw.Draw(img)
|
| 64 |
+
font = ImageFont.load_default()
|
| 65 |
+
d.text((self.frame_width//2, self.frame_height//2), "Care Plan Animation", fill='black', font=font, anchor="mm")
|
| 66 |
+
return img
|
| 67 |
+
|
| 68 |
+
def create_step_frame(self, step, step_num, total_steps):
|
| 69 |
+
img = Image.new('RGB', (self.frame_width, self.frame_height), color='white')
|
| 70 |
+
d = ImageDraw.Draw(img)
|
| 71 |
+
|
| 72 |
+
color = next((self.colors[key] for key in self.colors if key.lower() in step.lower()), '#FFFFFF')
|
| 73 |
+
|
| 74 |
+
d.rectangle([50, 50, self.frame_width-50, self.frame_height-50], fill=color, outline='black')
|
| 75 |
+
|
| 76 |
+
font = ImageFont.load_default()
|
| 77 |
+
d.text((self.frame_width//2, 70), f"Step {step_num}/{total_steps}", fill='black', font=font, anchor="mm")
|
| 78 |
+
d.text((self.frame_width//2, self.frame_height//2), step, fill='black', font=font, anchor="mm")
|
| 79 |
+
|
| 80 |
+
return img
|
| 81 |
+
|
| 82 |
+
def create_transition_frames(self):
|
| 83 |
+
frames = []
|
| 84 |
+
for i in range(10): # 10 transition frames
|
| 85 |
+
img = Image.new('RGB', (self.frame_width, self.frame_height), color='white')
|
| 86 |
+
d = ImageDraw.Draw(img)
|
| 87 |
+
size = 20 + i * 5 # Growing circle
|
| 88 |
+
d.ellipse([self.frame_width//2 - size, self.frame_height//2 - size,
|
| 89 |
+
self.frame_width//2 + size, self.frame_height//2 + size],
|
| 90 |
+
fill='red')
|
| 91 |
+
frames.append(img)
|
| 92 |
+
return frames
|
| 93 |
+
|
| 94 |
+
def create_final_frame(self):
|
| 95 |
+
img = Image.new('RGB', (self.frame_width, self.frame_height), color='white')
|
| 96 |
+
d = ImageDraw.Draw(img)
|
| 97 |
+
font = ImageFont.load_default()
|
| 98 |
+
d.text((self.frame_width//2, self.frame_height//2), "Care Plan Complete!", fill='black', font=font, anchor="mm")
|
| 99 |
+
return img
|