Update app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,87 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pickle
|
| 3 |
-
import pandas as pd
|
| 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 |
-
<p
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from sklearn.preprocessing import StandardScaler
|
| 7 |
+
from sklearn.preprocessing import OneHotEncoder
|
| 8 |
+
|
| 9 |
+
# Load the model and preprocessor
|
| 10 |
+
model = pickle.load(open('model.pkl', 'rb'))
|
| 11 |
+
preprocessor = pickle.load(open('preprocess_pipeline.pkl', 'rb'))
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def call_gradio(Sex, Equipment, Age, BodyweightKg, BestSquatKg, Bestbenchkg):
|
| 16 |
+
# Convert inputs to appropriate data types
|
| 17 |
+
Age = float(Age)
|
| 18 |
+
BodyweightKg = float(BodyweightKg)
|
| 19 |
+
BestSquatKg = float(BestSquatKg)
|
| 20 |
+
Bestbenchkg = float(Bestbenchkg)
|
| 21 |
+
|
| 22 |
+
# Create a DataFrame with the input data
|
| 23 |
+
df_x = pd.DataFrame({
|
| 24 |
+
'Sex': [Sex],
|
| 25 |
+
'Equipment': [Equipment],
|
| 26 |
+
'Age': [Age],
|
| 27 |
+
'BodyweightKg': [BodyweightKg],
|
| 28 |
+
'BestSquatKg': [BestSquatKg],
|
| 29 |
+
'Bestbench(kg)': [Bestbenchkg],
|
| 30 |
+
})
|
| 31 |
+
|
| 32 |
+
# Define the categorical and numerical feature lists
|
| 33 |
+
categorical_features = ['Sex', 'Equipment']
|
| 34 |
+
numerical_features = df_x.drop(categorical_features, axis=1).columns.tolist()
|
| 35 |
+
|
| 36 |
+
# Ensure that the preprocessor is fitted
|
| 37 |
+
if not hasattr(preprocessor, 'transformers_'):
|
| 38 |
+
raise RuntimeError("The preprocessor is not fitted yet. Fit the preprocessor before calling this function.")
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Transform the data using the preprocessor
|
| 42 |
+
X_processed = preprocessor.transform(df_x)
|
| 43 |
+
|
| 44 |
+
# Access the OneHotEncoder directly to get feature names
|
| 45 |
+
ohe = preprocessor.named_transformers_['cat'].named_steps['onehot']
|
| 46 |
+
cat_feature_names = ohe.get_feature_names_out(categorical_features)
|
| 47 |
+
|
| 48 |
+
# Combine numerical features and one-hot encoded feature names
|
| 49 |
+
all_feature_names = numerical_features + list(cat_feature_names)
|
| 50 |
+
|
| 51 |
+
# Create a DataFrame with processed features
|
| 52 |
+
X_processed_df = pd.DataFrame(X_processed, columns=all_feature_names)
|
| 53 |
+
|
| 54 |
+
# Predict using the model
|
| 55 |
+
y_pred = model.predict(X_processed_df)
|
| 56 |
+
|
| 57 |
+
max_kg = int(y_pred[0])
|
| 58 |
+
return max_kg
|
| 59 |
+
|
| 60 |
+
# Define Gradio inputs and outputs
|
| 61 |
+
sex_dropdown = gr.Dropdown(choices=['M', 'F'], label="Sex", info="Select Male or Female")
|
| 62 |
+
equipment_dropdown = gr.Dropdown(choices=['Raw', 'Wraps', 'Single-ply', 'Multi-ply'], label="Equipment", info="Select the equipment used for the competition.")
|
| 63 |
+
age_textbox = gr.Textbox(lines=1, label="Age", info="Enter your Age")
|
| 64 |
+
bodyweight_kg_textbox = gr.Textbox(lines=1, label="BodyweightKg", info="Enter your Bodyweight in Kg")
|
| 65 |
+
best_squat_kg_textbox = gr.Textbox(lines=1, label="BestSquatKg", info="Enter your Best Squat in Kg")
|
| 66 |
+
best_bench_kg_textbox = gr.Textbox(lines=1, label="BestbenchKg", info="Enter your Best Bench in Kg")
|
| 67 |
+
|
| 68 |
+
# Custom description with image and footer
|
| 69 |
+
description = """
|
| 70 |
+
<div style='text-align: center;'>
|
| 71 |
+
<h1 style='font-size: 50px;'>PowerLift Muscle Map</h1> <!-- Title with increased font size -->
|
| 72 |
+
<p>Use this model to estimate your best Deadlift (kg) based on your selected features. Input your details and see the predicted weight (kg) you could lift.</p>
|
| 73 |
+
<p><strong>Output:</strong> Estimated Best Deadlift (kg)</p>
|
| 74 |
+
<br>
|
| 75 |
+
<p style='font-size: 10px; color: #555;'>❤️ PDS</p>
|
| 76 |
+
</div>
|
| 77 |
+
"""
|
| 78 |
+
|
| 79 |
+
# Create and launch Gradio interface
|
| 80 |
+
iface = gr.Interface(
|
| 81 |
+
fn=call_gradio,
|
| 82 |
+
inputs=[sex_dropdown, equipment_dropdown, age_textbox, bodyweight_kg_textbox, best_squat_kg_textbox, best_bench_kg_textbox],
|
| 83 |
+
outputs="number",
|
| 84 |
+
description=description,
|
| 85 |
+
)
|
| 86 |
+
|
|
|
|
|
|
|
| 87 |
iface.launch()
|