File size: 5,093 Bytes
caefa5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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()