PrinceDeepakSiddharth12 commited on
Commit
e7774b9
·
verified ·
1 Parent(s): 34d8e40

Update app.py

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