Spaces:
Sleeping
Sleeping
Update components/animation_engineer.py
Browse files- components/animation_engineer.py +91 -10
components/animation_engineer.py
CHANGED
|
@@ -1,16 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
class AnimationEngineer:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
def display_care_plan_animation(self, final_plan):
|
| 3 |
st.write("Visualizing Care Plan...")
|
| 4 |
-
progress_bar = st.progress(0)
|
| 5 |
-
status_text = st.empty()
|
| 6 |
-
|
| 7 |
steps = final_plan.split(", ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
for i, step in enumerate(steps):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import time
|
| 3 |
+
import random
|
| 4 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
class AnimationEngineer:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.colors = {
|
| 10 |
+
'Parkinson': '#FFB3BA', # Light Pink
|
| 11 |
+
'Alzheimer': '#BAFFC9', # Light Green
|
| 12 |
+
'Treatment A': '#BAE1FF', # Light Blue
|
| 13 |
+
'Treatment B': '#FFFFBA', # Light Yellow
|
| 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 a placeholder for our animation
|
| 23 |
+
animation_placeholder = st.empty()
|
| 24 |
+
|
| 25 |
+
# Progress bar
|
| 26 |
+
progress_bar = st.progress(0)
|
| 27 |
+
|
| 28 |
for i, step in enumerate(steps):
|
| 29 |
+
# Update progress
|
| 30 |
+
progress = int((i + 1) / len(steps) * 100)
|
| 31 |
+
progress_bar.progress(progress)
|
| 32 |
+
|
| 33 |
+
# Create and display step visualization
|
| 34 |
+
img = self.create_step_visualization(step, i+1, len(steps))
|
| 35 |
+
animation_placeholder.image(img)
|
| 36 |
+
|
| 37 |
+
time.sleep(1.5) # Pause between steps
|
| 38 |
+
|
| 39 |
+
# Final animation
|
| 40 |
+
self.display_final_animation(animation_placeholder)
|
| 41 |
+
|
| 42 |
+
st.success("Care Plan Visualization Complete!")
|
| 43 |
+
st.balloons()
|
| 44 |
+
|
| 45 |
+
def create_step_visualization(self, step, step_num, total_steps):
|
| 46 |
+
# Create a new image with a white background
|
| 47 |
+
img = Image.new('RGB', (400, 200), color='white')
|
| 48 |
+
d = ImageDraw.Draw(img)
|
| 49 |
+
|
| 50 |
+
# Choose color based on step content
|
| 51 |
+
color = next((self.colors[key] for key in self.colors if key.lower() in step.lower()), '#FFFFFF')
|
| 52 |
+
|
| 53 |
+
# Draw a colored rectangle
|
| 54 |
+
d.rectangle([20, 20, 380, 180], fill=color, outline='black')
|
| 55 |
+
|
| 56 |
+
# Add text
|
| 57 |
+
font = ImageFont.load_default()
|
| 58 |
+
d.text((30, 30), f"Step {step_num}/{total_steps}", fill='black', font=font)
|
| 59 |
+
d.text((30, 60), step, fill='black', font=font)
|
| 60 |
+
|
| 61 |
+
# Convert PIL Image to bytes
|
| 62 |
+
img_byte_arr = io.BytesIO()
|
| 63 |
+
img.save(img_byte_arr, format='PNG')
|
| 64 |
+
|
| 65 |
+
return img_byte_arr.getvalue()
|
| 66 |
+
|
| 67 |
+
def display_final_animation(self, placeholder):
|
| 68 |
+
# Create a simple animation of a pulsing heart
|
| 69 |
+
for _ in range(5): # Pulse 5 times
|
| 70 |
+
for size in range(50, 100, 10): # Grow
|
| 71 |
+
self.display_heart(placeholder, size)
|
| 72 |
+
time.sleep(0.1)
|
| 73 |
+
for size in range(100, 50, -10): # Shrink
|
| 74 |
+
self.display_heart(placeholder, size)
|
| 75 |
+
time.sleep(0.1)
|
| 76 |
|
| 77 |
+
def display_heart(self, placeholder, size):
|
| 78 |
+
# Create a new image with a white background
|
| 79 |
+
img = Image.new('RGB', (200, 200), color='white')
|
| 80 |
+
d = ImageDraw.Draw(img)
|
| 81 |
+
|
| 82 |
+
# Draw a red heart
|
| 83 |
+
d.polygon([
|
| 84 |
+
(100, 100 - size//2),
|
| 85 |
+
(100 - size//2, 100 - size//4),
|
| 86 |
+
(100 - size//2, 100 + size//4),
|
| 87 |
+
(100, 100 + size//2),
|
| 88 |
+
(100 + size//2, 100 + size//4),
|
| 89 |
+
(100 + size//2, 100 - size//4)
|
| 90 |
+
], fill='red')
|
| 91 |
+
|
| 92 |
+
# Convert PIL Image to bytes
|
| 93 |
+
img_byte_arr = io.BytesIO()
|
| 94 |
+
img.save(img_byte_arr, format='PNG')
|
| 95 |
+
|
| 96 |
+
# Display the image
|
| 97 |
+
placeholder.image(img_byte_arr.getvalue())
|