imdarrin commited on
Commit
ac23dee
·
verified ·
1 Parent(s): 31a5e02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -63
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(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 operation type and operator id using the saved encoders
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
- X = np.array([[quantity, operation_code, operator_code, day_of_week]])
29
- pred_days = model.predict(X)[0]
30
-
31
- whole_days = int(pred_days)
32
- fractional_day = pred_days - whole_days
33
-
34
- total_seconds = fractional_day * 24 * 3600
35
- hours = int(total_seconds // 3600)
36
- minutes = int((total_seconds % 3600) // 60)
37
-
38
- est_end_datetime = start_date + timedelta(days=whole_days, hours=hours, minutes=minutes)
39
- est_end_date_str = est_end_datetime.strftime("%d-%m-%Y %H:%M")
40
- duration_str = f"{whole_days} days, {hours} hours, {minutes} minutes"
41
-
42
- return f"Estimated End Date & Time: {est_end_date_str}\nEstimated Duration: {duration_str}"
43
-
44
- # Get all options for dropdowns from the encoders
45
- operation_types = le_operation_type.classes_.tolist()
46
- operator_ids = le_operator_id.classes_.tolist()
47
-
48
- iface = gr.Interface(
49
- fn=predict_end_date,
50
- inputs=[
51
- gr.Textbox(label="Start Date (DD-MM-YYYY)", value="01-01-2025"),
52
- gr.Number(label="Quantity", value=1),
53
- gr.Dropdown(operation_types, label="Operation Type"),
54
- gr.Dropdown(operator_ids, label="Operator ID"),
55
- gr.Slider(0, 6, step=1, label="Day of Week (0=Monday, 6=Sunday)", value=0),
56
- ],
57
- outputs="text",
58
- title="Manufacturing Production Scheduling",
59
- description="Predict estimated end date and duration from production inputs."
60
- )
61
-
62
- if __name__ == "__main__":
63
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()