Zen-4011 commited on
Commit
c52b8a8
·
verified ·
1 Parent(s): 0add3fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -22
app.py CHANGED
@@ -1,29 +1,25 @@
1
- from flask import Flask, render_template, request, jsonify
2
- import joblib
3
- import pandas as pd
4
- import numpy as np
5
-
6
- app = Flask(__name__)
7
-
8
- model = joblib.load("music_genre_classifier.pkl")
9
- scaler = joblib.load("scaler.pkl")
10
- le = joblib.load("label_encoder.pkl")
11
-
12
- @app.route('/')
13
- def home():
14
- return render_template('index.html')
15
-
16
- @app.route('/predict', methods = ['POST'])
17
  def predict():
18
  data = request.get_json()
19
 
 
20
  input_df = pd.DataFrame([data])
21
 
22
- # Scale and Predict
 
 
 
 
 
 
 
 
 
 
 
23
  scaled_data = scaler.transform(input_df)
24
  prediction_idx = model.predict(scaled_data)[0]
25
 
26
- # Get probability / confidence
27
  probs = model.predict_proba(scaled_data)[0]
28
  confidence = np.max(probs) * 100
29
 
@@ -32,7 +28,4 @@ def predict():
32
  return jsonify({
33
  'prediction': genre,
34
  'confidence': confidence
35
- })
36
-
37
- if __name__ == "__main__":
38
- app.run(host = "0.0.0.0", port = 7860)
 
1
+ @app.route('/predict', methods=['POST'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  def predict():
3
  data = request.get_json()
4
 
5
+ # 1. Create a DataFrame from the 8 features coming from your website
6
  input_df = pd.DataFrame([data])
7
 
8
+ # 2. Get the list of all 58 features the scaler expects
9
+ expected_features = scaler.feature_names_in_
10
+
11
+ # 3. Add the missing features and set them to 0
12
+ for col in expected_features:
13
+ if col not in input_df.columns:
14
+ input_df[col] = 0.0
15
+
16
+ # 4. Reorder columns to match the exact order seen during training
17
+ input_df = input_df[expected_features]
18
+
19
+ # 5. Now the scale and predict will work!
20
  scaled_data = scaler.transform(input_df)
21
  prediction_idx = model.predict(scaled_data)[0]
22
 
 
23
  probs = model.predict_proba(scaled_data)[0]
24
  confidence = np.max(probs) * 100
25
 
 
28
  return jsonify({
29
  'prediction': genre,
30
  'confidence': confidence
31
+ })