File size: 3,220 Bytes
b89cf70
29ce14d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b89cf70
29ce14d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import numpy as np
import joblib
import xgboost as xgb

# Page Configuration
st.set_page_config(
    page_title="Calorie Burn Predictor",
    page_icon="🔥",
    layout="centered"
)

# Load Model and Columns
@st.cache_resource
def load_assets():
    try:
        model = joblib.load('src/xgboost_model.pkl')
        model_columns = joblib.load('src/model_columns.pkl')
        return model, model_columns
    except FileNotFoundError:
        return None, None

model, model_columns = load_assets()

# App Header
st.title("🔥 Calorie Burn Predictor")
st.write("Enter your exercise details below to estimate the calories burned.")

if model is None:
    st.error("Error: Model files not found. Please upload 'xgboost_model.pkl' and 'model_columns.pkl' to the directory.")
    st.stop()

# User Inputs 
col1, col2 = st.columns(2)

with col1:
    gender = st.selectbox("Gender", ["Male", "Female"])
    age = st.number_input("Age", min_value=10, max_value=100, value=25)
    height = st.number_input("Height (cm)", min_value=100, max_value=250, value=175)
    weight = st.number_input("Weight (kg)", min_value=30, max_value=200, value=70)

with col2:
    duration = st.slider("Duration (minutes)", min_value=1, max_value=60, value=30)
    heart_rate = st.slider("Heart Rate (bpm)", min_value=60, max_value=200, value=100)
    body_temp = st.slider("Body Temperature (°C)", min_value=35.0, max_value=42.0, value=38.0, step=0.1)

# Prediction 
if st.button("Calculate Calories", type="primary"):
    
    input_data = pd.DataFrame({
        'Sex': [gender], 
        'Age': [age],
        'Height': [height],
        'Weight': [weight],
        'Duration': [duration],
        'Heart_Rate': [heart_rate],
        'Body_Temp': [body_temp]})

    # Feature Engineering 
    
    # BMI
    input_data['BMI'] = input_data['Weight'] / ((input_data['Height'] / 100) ** 2)

    # BMR 
    input_data['BMR'] = (10 * input_data['Weight']) + (6.25 * input_data['Height']) - (5 * input_data['Age'])

    # Effort 
    input_data['Effort'] = input_data['Duration'] * input_data['Heart_Rate']

    # Temperature Difference
    input_data['Temp_Diff'] = input_data['Body_Temp'] - 37.0

    # Intensity
    input_data['Intensity'] = input_data['Heart_Rate'] * input_data['Body_Temp']

    # Temp per Minute
    input_data['Temp_per_Minute'] = input_data['Body_Temp'] / input_data['Duration']

    # Encoding
    input_data['Sex'] = input_data['Sex'].map({'Male': 0, 'Female': 1})

    input_data = input_data.reindex(columns=model_columns, fill_value=0)

    # Prediction
    try:
        prediction = model.predict(input_data)[0]
        
        st.success(f"Estimated Calories Burned: **{prediction:.1f} kcal**")
        
        if prediction < 100:
            st.info("Nice warm-up! Keep moving! 🚶")
        elif prediction < 300:
            st.info("Great workout! 💪")
        else:
            st.balloons()
            st.info("Incredible effort! You are on fire! 🔥")
            
        with st.expander("See calculated features"):
            st.write(input_data)
            
    except Exception as e:
        st.error(f"An error occurred during prediction: {str(e)}")