imdarrin commited on
Commit
932f1a8
·
verified ·
1 Parent(s): ac23dee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -9,7 +9,13 @@ le_operation_type = joblib.load("le_operation_type.pkl")
9
  le_operator_id = joblib.load("le_operator_id.pkl")
10
 
11
  def predict_end_date(start_date, quantity, operation_code, operator_code, day_of_week):
12
- # Recreate engineered features
 
 
 
 
 
 
13
  quantity_log = np.log1p(quantity)
14
  quantity_log_squared = quantity_log ** 2
15
  quantity_log_cubed = quantity_log ** 3
@@ -18,7 +24,7 @@ def predict_end_date(start_date, quantity, operation_code, operator_code, day_of
18
  quantity_operator_interaction_squared = quantity_operator_interaction ** 2
19
  operation_operator_interaction = operation_code * operator_code
20
 
21
- # Reconstruct feature vector with same structure as training
22
  X = pd.DataFrame([[
23
  quantity_log,
24
  quantity_log_squared,
@@ -46,11 +52,11 @@ def predict_end_date(start_date, quantity, operation_code, operator_code, day_of
46
  # Predict
47
  pred_days = model.predict(X)[0]
48
 
49
- # Convert predicted days to end date
 
50
  start_dt = datetime.strptime(start_date, "%d-%m-%Y")
51
  end_dt = start_dt + timedelta(days=pred_days)
52
 
53
- # Split days into days + hours + minutes
54
  total_minutes = pred_days * 24 * 60
55
  days = int(total_minutes // (24 * 60))
56
  hours = int((total_minutes % (24 * 60)) // 60)
 
9
  le_operator_id = joblib.load("le_operator_id.pkl")
10
 
11
  def predict_end_date(start_date, quantity, operation_code, operator_code, day_of_week):
12
+ # Convert inputs to correct types
13
+ quantity = float(quantity)
14
+ operation_code = int(operation_code)
15
+ operator_code = int(operator_code)
16
+ day_of_week = int(day_of_week)
17
+
18
+ # Log transform and feature engineering
19
  quantity_log = np.log1p(quantity)
20
  quantity_log_squared = quantity_log ** 2
21
  quantity_log_cubed = quantity_log ** 3
 
24
  quantity_operator_interaction_squared = quantity_operator_interaction ** 2
25
  operation_operator_interaction = operation_code * operator_code
26
 
27
+ # Match the model's training feature order
28
  X = pd.DataFrame([[
29
  quantity_log,
30
  quantity_log_squared,
 
52
  # Predict
53
  pred_days = model.predict(X)[0]
54
 
55
+ # Convert to date and readable duration
56
+ from datetime import datetime, timedelta
57
  start_dt = datetime.strptime(start_date, "%d-%m-%Y")
58
  end_dt = start_dt + timedelta(days=pred_days)
59
 
 
60
  total_minutes = pred_days * 24 * 60
61
  days = int(total_minutes // (24 * 60))
62
  hours = int((total_minutes % (24 * 60)) // 60)