Spaces:
Runtime error
Runtime error
import gradio as gr | |
import numpy as np | |
import random # Simulating real-time health data | |
from sklearn.linear_model import LogisticRegression # Example of advanced AI model | |
from sklearn.preprocessing import StandardScaler | |
# Simulated dataset and AI model training | |
def train_ai_model(): | |
""" | |
Trains a simple Logistic Regression model to classify fabric adjustments | |
based on heart rate, temperature, and activity level. | |
""" | |
# Example training data: [heart_rate, body_temp, activity_level (encoded)] | |
X = np.array([ | |
[60, 35.5, 0], # Low activity, low temperature | |
[90, 36.8, 1], # Medium activity, normal temperature | |
[110, 38.0, 2], # High activity, high temperature | |
[75, 37.0, 0], # Low activity, normal temperature | |
[100, 38.5, 2], # High activity, very high temperature | |
]) | |
# Fabric adjustment labels: 0 = "Maintaining", 1 = "Cooling", 2 = "Warming" | |
y = np.array([2, 0, 1, 0, 1]) | |
# Scale the features for better model performance | |
scaler = StandardScaler() | |
X_scaled = scaler.fit_transform(X) | |
# Train the Logistic Regression model | |
model = LogisticRegression() | |
model.fit(X_scaled, y) | |
return model, scaler | |
# Train the AI model | |
ai_model, feature_scaler = train_ai_model() | |
# Function to simulate live health data from wearable sensors | |
def fetch_real_health_data(): | |
""" | |
Simulates real-time health data. Replace this with actual hardware integration | |
for wearable devices using libraries like pyserial or APIs from wearables. | |
""" | |
heart_rate = random.randint(60, 120) # Random BPM | |
body_temp = round(random.uniform(35.5, 38.5), 1) # Random temperature in °C | |
activity_level = random.choice(["Low", "Medium", "High"]) # Simulated activity level | |
return heart_rate, body_temp, activity_level | |
# Function to adjust fabric based on health metrics and user preferences | |
def adjust_fabric(real_time, heart_rate_pref, temp_pref, cooling_pref, warming_pref): | |
if real_time: | |
# Fetch live health data | |
heart_rate, body_temp, activity_level = fetch_real_health_data() | |
else: | |
# Default values for demo purposes | |
heart_rate, body_temp, activity_level = 70, 36.5, "Medium" | |
# Map activity level to numeric values for AI model | |
activity_map = {"Low": 0, "Medium": 1, "High": 2} | |
activity_level_num = activity_map[activity_level] | |
# Prepare input for AI model | |
features = np.array([[heart_rate, body_temp, activity_level_num]]) | |
features_scaled = feature_scaler.transform(features) | |
# Get fabric adjustment prediction from AI model | |
fabric_adjustment_pred = ai_model.predict(features_scaled)[0] | |
adjustment_map = {0: "Maintaining temperature", 1: cooling_pref, 2: warming_pref} | |
fabric_adjustment = adjustment_map[fabric_adjustment_pred] | |
# Heart rate feedback based on user preference | |
if heart_rate > heart_rate_pref: | |
heart_rate_feedback = f"Warning: High Heart Rate ({heart_rate} BPM)! Adjusting fabric for {cooling_pref}." | |
else: | |
heart_rate_feedback = f"Heart rate is normal ({heart_rate} BPM)." | |
# Body temperature feedback based on user preference | |
if body_temp > temp_pref: | |
temp_feedback = f"High body temperature ({body_temp}°C)! Activating {cooling_pref} mechanism." | |
else: | |
temp_feedback = f"Body temperature is normal ({body_temp}°C)." | |
# Activity level feedback | |
if activity_level == "High": | |
activity_feedback = f"High activity level detected. Increasing {cooling_pref}." | |
elif activity_level == "Medium": | |
activity_feedback = "Moderate activity level detected. Maintaining fabric temperature." | |
else: | |
activity_feedback = f"Low activity level detected. Activating {warming_pref} if needed." | |
# Return the feedback and fabric adjustment details | |
return (f"Heart Rate: {heart_rate} BPM\n{heart_rate_feedback}", | |
f"Body Temperature: {body_temp}°C\n{temp_feedback}", | |
f"Activity Level: {activity_level}\n{activity_feedback}", | |
fabric_adjustment) | |
# Create Gradio Interface | |
iface = gr.Interface( | |
fn=lambda real_time, heart_rate_pref, temp_pref, cooling_pref, warming_pref: | |
adjust_fabric(real_time, heart_rate_pref, temp_pref, cooling_pref, warming_pref), | |
inputs=[ | |
gr.Checkbox(label="Enable Real-Time Data", value=False), | |
gr.Slider(minimum=60, maximum=120, step=1, label="Preferred Max Heart Rate (BPM)", value=100), | |
gr.Slider(minimum=35, maximum=40, step=0.1, label="Preferred Max Body Temperature (°C)", value=37.5), | |
gr.Radio(choices=["Cooling", "Strong Cooling"], label="Cooling Mechanism Preference", value="Cooling"), | |
gr.Radio(choices=["Warming", "Strong Warming"], label="Warming Mechanism Preference", value="Warming"), | |
], | |
outputs=[ | |
gr.Textbox(label="Heart Rate Feedback"), | |
gr.Textbox(label="Temperature Feedback"), | |
gr.Textbox(label="Activity Feedback"), | |
gr.Textbox(label="Fabric Adjustment") | |
], | |
live=True | |
) | |
# Launch the Gradio interface | |
iface.launch() | |