m-rishabh commited on
Commit
adbddd2
·
verified ·
1 Parent(s): 908f354

Deploying format_4_array_wrapper

Browse files
Files changed (1) hide show
  1. app.py +24 -3
app.py CHANGED
@@ -11,14 +11,35 @@ import numpy as np
11
  import joblib
12
  import pandas as pd
13
  import os
 
14
 
15
  # Load the real model
16
  MODEL_PATH = os.path.join(os.path.dirname(__file__), "iris_knn_pipeline.pkl")
17
- model = joblib.load(MODEL_PATH)
 
 
 
 
 
 
 
 
 
18
 
19
  def predict_iris(features: List[float]) -> tuple:
20
- # Convert to DataFrame as expected by the pipeline
21
- df = pd.DataFrame([features], columns=["sepal_length", "sepal_width", "petal_length", "petal_width"])
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  pred = int(model.predict(df)[0])
24
  probs = model.predict_proba(df)[0].tolist()
 
11
  import joblib
12
  import pandas as pd
13
  import os
14
+ import traceback
15
 
16
  # Load the real model
17
  MODEL_PATH = os.path.join(os.path.dirname(__file__), "iris_knn_pipeline.pkl")
18
+ model = None
19
+ load_error = None
20
+
21
+ try:
22
+ if os.path.exists(MODEL_PATH):
23
+ model = joblib.load(MODEL_PATH)
24
+ else:
25
+ load_error = f"Model file not found at {MODEL_PATH}"
26
+ except Exception as e:
27
+ load_error = f"Error loading model: {str(e)}\n{traceback.format_exc()}"
28
 
29
  def predict_iris(features: List[float]) -> tuple:
30
+ if load_error:
31
+ raise Exception(f"Model not loaded: {load_error}")
32
+
33
+ # Feature names expected by the scikit-learn pipeline
34
+ FEATURE_NAMES = [
35
+ "sepal length (cm)",
36
+ "sepal width (cm)",
37
+ "petal length (cm)",
38
+ "petal width (cm)"
39
+ ]
40
+
41
+ # Convert to DataFrame with correct column names
42
+ df = pd.DataFrame([features], columns=FEATURE_NAMES)
43
 
44
  pred = int(model.predict(df)[0])
45
  probs = model.predict_proba(df)[0].tolist()