Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,81 @@
|
|
| 1 |
-
import joblib
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
|
| 6 |
-
# Load
|
| 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 |
# Feature engineering
|
| 21 |
quantity_log = np.log1p(quantity)
|
| 22 |
quantity_log_squared = quantity_log ** 2
|
| 23 |
-
quantity_log_cubed = quantity_log ** 3
|
| 24 |
quantity_operation_interaction = quantity_log * operation_code
|
| 25 |
quantity_operator_interaction = quantity_log * operator_code
|
| 26 |
-
quantity_operator_interaction_squared = quantity_operator_interaction ** 2
|
| 27 |
-
operation_operator_interaction = operation_code * operator_code
|
| 28 |
|
| 29 |
-
|
|
|
|
| 30 |
quantity_log,
|
| 31 |
quantity_log_squared,
|
| 32 |
-
quantity_log_cubed,
|
| 33 |
quantity_operation_interaction,
|
| 34 |
quantity_operator_interaction,
|
| 35 |
-
quantity_operator_interaction_squared,
|
| 36 |
-
operation_operator_interaction,
|
| 37 |
operation_code,
|
| 38 |
operator_code,
|
| 39 |
day_of_week
|
| 40 |
-
]]
|
| 41 |
-
'Quantity_log',
|
| 42 |
-
'Quantity_log_squared',
|
| 43 |
-
'Quantity_log_cubed',
|
| 44 |
-
'Quantity_Operation_interaction',
|
| 45 |
-
'Quantity_Operator_interaction',
|
| 46 |
-
'Quantity_Operator_interaction_squared',
|
| 47 |
-
'Operation_Operator_interaction',
|
| 48 |
-
'Operation_Code',
|
| 49 |
-
'Operator_Code',
|
| 50 |
-
'DayOfWeek'
|
| 51 |
-
])
|
| 52 |
|
| 53 |
pred_days = model.predict(X)[0]
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
minutes = int(total_minutes % 60)
|
| 64 |
|
| 65 |
-
return f"Estimated
|
| 66 |
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
operator_ids = le_operator_id.classes_.tolist()
|
| 71 |
|
| 72 |
iface = gr.Interface(
|
| 73 |
-
fn=
|
| 74 |
inputs=[
|
| 75 |
-
gr.Textbox(label="Start Date (DD-MM-YYYY)", value="
|
| 76 |
-
gr.Number(label="Quantity", value=
|
| 77 |
-
gr.Dropdown(
|
| 78 |
-
gr.Dropdown(
|
| 79 |
-
gr.Slider(0, 6, step=1, label="Day of Week (0=Monday, 6=Sunday)", value=
|
| 80 |
],
|
| 81 |
outputs="text",
|
| 82 |
-
title="Manufacturing
|
| 83 |
-
description="Predict estimated end date and duration
|
| 84 |
)
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
+
import joblib
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
|
| 6 |
+
# Load model and label 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_str, quantity, operation_type, operator_id, day_of_week):
|
| 12 |
+
try:
|
| 13 |
+
start_date = datetime.strptime(start_date_str, "%d-%m-%Y")
|
| 14 |
+
except ValueError:
|
| 15 |
+
return "Error: Start Date must be in DD-MM-YYYY format"
|
| 16 |
|
| 17 |
+
# Encode categorical inputs
|
| 18 |
+
try:
|
| 19 |
+
operation_code = le_operation_type.transform([operation_type])[0]
|
| 20 |
+
except ValueError:
|
| 21 |
+
return f"Unknown Operation Type: {operation_type}"
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
operator_code = le_operator_id.transform([operator_id])[0]
|
| 25 |
+
except ValueError:
|
| 26 |
+
return f"Unknown Operator ID: {operator_id}"
|
| 27 |
|
| 28 |
# Feature engineering
|
| 29 |
quantity_log = np.log1p(quantity)
|
| 30 |
quantity_log_squared = quantity_log ** 2
|
|
|
|
| 31 |
quantity_operation_interaction = quantity_log * operation_code
|
| 32 |
quantity_operator_interaction = quantity_log * operator_code
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Create feature vector for prediction
|
| 35 |
+
X = np.array([[
|
| 36 |
quantity_log,
|
| 37 |
quantity_log_squared,
|
|
|
|
| 38 |
quantity_operation_interaction,
|
| 39 |
quantity_operator_interaction,
|
|
|
|
|
|
|
| 40 |
operation_code,
|
| 41 |
operator_code,
|
| 42 |
day_of_week
|
| 43 |
+
]])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
pred_days = model.predict(X)[0]
|
| 46 |
|
| 47 |
+
whole_days = int(pred_days)
|
| 48 |
+
fractional_day = pred_days - whole_days
|
| 49 |
+
|
| 50 |
+
total_seconds = fractional_day * 24 * 3600
|
| 51 |
+
hours = int(total_seconds // 3600)
|
| 52 |
+
minutes = int((total_seconds % 3600) // 60)
|
| 53 |
|
| 54 |
+
est_end_datetime = start_date + timedelta(days=whole_days, hours=hours, minutes=minutes)
|
| 55 |
+
est_end_date_str = est_end_datetime.strftime("%d-%m-%Y %H:%M")
|
| 56 |
+
duration_str = f"{whole_days} days, {hours} hours, {minutes} minutes"
|
|
|
|
| 57 |
|
| 58 |
+
return f"Estimated End Date & Time: {est_end_date_str}\nEstimated Duration: {duration_str}"
|
| 59 |
|
| 60 |
+
# Build Gradio interface
|
| 61 |
+
def gradio_interface(start_date, quantity, operation_type, operator_id, day_of_week):
|
| 62 |
+
return predict_end_date(start_date, quantity, operation_type, operator_id, day_of_week)
|
| 63 |
|
| 64 |
+
operation_options = list(le_operation_type.classes_)
|
| 65 |
+
operator_options = list(le_operator_id.classes_)
|
|
|
|
| 66 |
|
| 67 |
iface = gr.Interface(
|
| 68 |
+
fn=gradio_interface,
|
| 69 |
inputs=[
|
| 70 |
+
gr.Textbox(label="Start Date (DD-MM-YYYY)", value="21-07-2025"),
|
| 71 |
+
gr.Number(label="Quantity", value=6000),
|
| 72 |
+
gr.Dropdown(choices=operation_options, label="Operation Type", value=operation_options[0]),
|
| 73 |
+
gr.Dropdown(choices=operator_options, label="Operator ID", value=operator_options[0]),
|
| 74 |
+
gr.Slider(minimum=0, maximum=6, step=1, label="Day of Week (0=Monday, 6=Sunday)", value=1)
|
| 75 |
],
|
| 76 |
outputs="text",
|
| 77 |
+
title="Manufacturing Scheduler Prediction",
|
| 78 |
+
description="Predict the estimated end date and duration of a manufacturing job based on input parameters."
|
| 79 |
)
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|