Spaces:
Runtime error
Runtime error
Thiwanka01
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random # Simulating real-time health data
|
4 |
+
from sklearn.linear_model import LogisticRegression # Example of advanced AI model
|
5 |
+
from sklearn.preprocessing import StandardScaler
|
6 |
+
|
7 |
+
# Simulated dataset and AI model training
|
8 |
+
def train_ai_model():
|
9 |
+
"""
|
10 |
+
Trains a simple Logistic Regression model to classify fabric adjustments
|
11 |
+
based on heart rate, temperature, and activity level.
|
12 |
+
"""
|
13 |
+
# Example training data: [heart_rate, body_temp, activity_level (encoded)]
|
14 |
+
X = np.array([
|
15 |
+
[60, 35.5, 0], # Low activity, low temperature
|
16 |
+
[90, 36.8, 1], # Medium activity, normal temperature
|
17 |
+
[110, 38.0, 2], # High activity, high temperature
|
18 |
+
[75, 37.0, 0], # Low activity, normal temperature
|
19 |
+
[100, 38.5, 2], # High activity, very high temperature
|
20 |
+
])
|
21 |
+
# Fabric adjustment labels: 0 = "Maintaining", 1 = "Cooling", 2 = "Warming"
|
22 |
+
y = np.array([2, 0, 1, 0, 1])
|
23 |
+
|
24 |
+
# Scale the features for better model performance
|
25 |
+
scaler = StandardScaler()
|
26 |
+
X_scaled = scaler.fit_transform(X)
|
27 |
+
|
28 |
+
# Train the Logistic Regression model
|
29 |
+
model = LogisticRegression()
|
30 |
+
model.fit(X_scaled, y)
|
31 |
+
|
32 |
+
return model, scaler
|
33 |
+
|
34 |
+
# Train the AI model
|
35 |
+
ai_model, feature_scaler = train_ai_model()
|
36 |
+
|
37 |
+
# Function to simulate live health data from wearable sensors
|
38 |
+
def fetch_real_health_data():
|
39 |
+
"""
|
40 |
+
Simulates real-time health data. Replace this with actual hardware integration
|
41 |
+
for wearable devices using libraries like pyserial or APIs from wearables.
|
42 |
+
"""
|
43 |
+
heart_rate = random.randint(60, 120) # Random BPM
|
44 |
+
body_temp = round(random.uniform(35.5, 38.5), 1) # Random temperature in °C
|
45 |
+
activity_level = random.choice(["Low", "Medium", "High"]) # Simulated activity level
|
46 |
+
return heart_rate, body_temp, activity_level
|
47 |
+
|
48 |
+
# Function to adjust fabric based on health metrics and user preferences
|
49 |
+
def adjust_fabric(real_time, heart_rate_pref, temp_pref, cooling_pref, warming_pref):
|
50 |
+
if real_time:
|
51 |
+
# Fetch live health data
|
52 |
+
heart_rate, body_temp, activity_level = fetch_real_health_data()
|
53 |
+
else:
|
54 |
+
# Default values for demo purposes
|
55 |
+
heart_rate, body_temp, activity_level = 70, 36.5, "Medium"
|
56 |
+
|
57 |
+
# Map activity level to numeric values for AI model
|
58 |
+
activity_map = {"Low": 0, "Medium": 1, "High": 2}
|
59 |
+
activity_level_num = activity_map[activity_level]
|
60 |
+
|
61 |
+
# Prepare input for AI model
|
62 |
+
features = np.array([[heart_rate, body_temp, activity_level_num]])
|
63 |
+
features_scaled = feature_scaler.transform(features)
|
64 |
+
|
65 |
+
# Get fabric adjustment prediction from AI model
|
66 |
+
fabric_adjustment_pred = ai_model.predict(features_scaled)[0]
|
67 |
+
adjustment_map = {0: "Maintaining temperature", 1: cooling_pref, 2: warming_pref}
|
68 |
+
fabric_adjustment = adjustment_map[fabric_adjustment_pred]
|
69 |
+
|
70 |
+
# Heart rate feedback based on user preference
|
71 |
+
if heart_rate > heart_rate_pref:
|
72 |
+
heart_rate_feedback = f"Warning: High Heart Rate ({heart_rate} BPM)! Adjusting fabric for {cooling_pref}."
|
73 |
+
else:
|
74 |
+
heart_rate_feedback = f"Heart rate is normal ({heart_rate} BPM)."
|
75 |
+
|
76 |
+
# Body temperature feedback based on user preference
|
77 |
+
if body_temp > temp_pref:
|
78 |
+
temp_feedback = f"High body temperature ({body_temp}°C)! Activating {cooling_pref} mechanism."
|
79 |
+
else:
|
80 |
+
temp_feedback = f"Body temperature is normal ({body_temp}°C)."
|
81 |
+
|
82 |
+
# Activity level feedback
|
83 |
+
if activity_level == "High":
|
84 |
+
activity_feedback = f"High activity level detected. Increasing {cooling_pref}."
|
85 |
+
elif activity_level == "Medium":
|
86 |
+
activity_feedback = "Moderate activity level detected. Maintaining fabric temperature."
|
87 |
+
else:
|
88 |
+
activity_feedback = f"Low activity level detected. Activating {warming_pref} if needed."
|
89 |
+
|
90 |
+
# Return the feedback and fabric adjustment details
|
91 |
+
return (f"Heart Rate: {heart_rate} BPM\n{heart_rate_feedback}",
|
92 |
+
f"Body Temperature: {body_temp}°C\n{temp_feedback}",
|
93 |
+
f"Activity Level: {activity_level}\n{activity_feedback}",
|
94 |
+
fabric_adjustment)
|
95 |
+
|
96 |
+
# Create Gradio Interface
|
97 |
+
iface = gr.Interface(
|
98 |
+
fn=lambda real_time, heart_rate_pref, temp_pref, cooling_pref, warming_pref:
|
99 |
+
adjust_fabric(real_time, heart_rate_pref, temp_pref, cooling_pref, warming_pref),
|
100 |
+
inputs=[
|
101 |
+
gr.Checkbox(label="Enable Real-Time Data", value=False),
|
102 |
+
gr.Slider(minimum=60, maximum=120, step=1, label="Preferred Max Heart Rate (BPM)", value=100),
|
103 |
+
gr.Slider(minimum=35, maximum=40, step=0.1, label="Preferred Max Body Temperature (°C)", value=37.5),
|
104 |
+
gr.Radio(choices=["Cooling", "Strong Cooling"], label="Cooling Mechanism Preference", value="Cooling"),
|
105 |
+
gr.Radio(choices=["Warming", "Strong Warming"], label="Warming Mechanism Preference", value="Warming"),
|
106 |
+
],
|
107 |
+
outputs=[
|
108 |
+
gr.Textbox(label="Heart Rate Feedback"),
|
109 |
+
gr.Textbox(label="Temperature Feedback"),
|
110 |
+
gr.Textbox(label="Activity Feedback"),
|
111 |
+
gr.Textbox(label="Fabric Adjustment")
|
112 |
+
],
|
113 |
+
live=True
|
114 |
+
)
|
115 |
+
|
116 |
+
# Launch the Gradio interface
|
117 |
+
iface.launch()
|