Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,81 @@
|
|
| 1 |
-
import joblib
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import numpy as np
|
| 4 |
-
from datetime import datetime, timedelta
|
| 5 |
-
|
| 6 |
-
# Load the saved model and encoders
|
| 7 |
-
model = joblib.load("best_model.pkl")
|
| 8 |
-
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(
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
|
| 6 |
+
# Load the saved model and encoders
|
| 7 |
+
model = joblib.load("best_model.pkl")
|
| 8 |
+
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
|
| 16 |
+
quantity_operation_interaction = quantity_log * operation_code
|
| 17 |
+
quantity_operator_interaction = quantity_log * operator_code
|
| 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,
|
| 25 |
+
quantity_log_cubed,
|
| 26 |
+
quantity_operation_interaction,
|
| 27 |
+
quantity_operator_interaction,
|
| 28 |
+
quantity_operator_interaction_squared,
|
| 29 |
+
operation_operator_interaction,
|
| 30 |
+
operation_code,
|
| 31 |
+
operator_code,
|
| 32 |
+
day_of_week
|
| 33 |
+
]], columns=[
|
| 34 |
+
'Quantity_log',
|
| 35 |
+
'Quantity_log_squared',
|
| 36 |
+
'Quantity_log_cubed',
|
| 37 |
+
'Quantity_Operation_interaction',
|
| 38 |
+
'Quantity_Operator_interaction',
|
| 39 |
+
'Quantity_Operator_interaction_squared',
|
| 40 |
+
'Operation_Operator_interaction',
|
| 41 |
+
'Operation_Code',
|
| 42 |
+
'Operator_Code',
|
| 43 |
+
'DayOfWeek'
|
| 44 |
+
])
|
| 45 |
+
|
| 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)
|
| 57 |
+
minutes = int(total_minutes % 60)
|
| 58 |
+
|
| 59 |
+
return f"Estimated end date: {end_dt.strftime('%d-%m-%Y')} ({days} days {hours} hours {minutes} minutes)"
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# Get all options for dropdowns from the encoders
|
| 63 |
+
operation_types = le_operation_type.classes_.tolist()
|
| 64 |
+
operator_ids = le_operator_id.classes_.tolist()
|
| 65 |
+
|
| 66 |
+
iface = gr.Interface(
|
| 67 |
+
fn=predict_end_date,
|
| 68 |
+
inputs=[
|
| 69 |
+
gr.Textbox(label="Start Date (DD-MM-YYYY)", value="01-01-2025"),
|
| 70 |
+
gr.Number(label="Quantity", value=1),
|
| 71 |
+
gr.Dropdown(operation_types, label="Operation Type"),
|
| 72 |
+
gr.Dropdown(operator_ids, label="Operator ID"),
|
| 73 |
+
gr.Slider(0, 6, step=1, label="Day of Week (0=Monday, 6=Sunday)", value=0),
|
| 74 |
+
],
|
| 75 |
+
outputs="text",
|
| 76 |
+
title="Manufacturing Production Scheduling",
|
| 77 |
+
description="Predict estimated end date and duration from production inputs."
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
iface.launch()
|