AAPASSIGNM / app.py
imdarrin's picture
Update app.py
244f9d0 verified
import gradio as gr
import numpy as np
import joblib
from datetime import datetime, timedelta
# Load model and label encoders
model = joblib.load("best_model.pkl")
le_operation_type = joblib.load("le_operation_type.pkl")
le_operator_id = joblib.load("le_operator_id.pkl")
def predict_end_date(start_date_str, quantity, operation_type, operator_id, day_of_week):
try:
start_date = datetime.strptime(start_date_str, "%d-%m-%Y")
except ValueError:
return "Error: Start Date must be in DD-MM-YYYY format"
# Encode categorical inputs
try:
operation_code = le_operation_type.transform([operation_type])[0]
except ValueError:
return f"Unknown Operation Type: {operation_type}"
try:
operator_code = le_operator_id.transform([operator_id])[0]
except ValueError:
return f"Unknown Operator ID: {operator_id}"
# Feature engineering
quantity_log = np.log1p(quantity)
quantity_log_squared = quantity_log ** 2
quantity_operation_interaction = quantity_log * operation_code
quantity_operator_interaction = quantity_log * operator_code
# Create feature vector for prediction
X = np.array([[
quantity_log,
quantity_log_squared,
quantity_operation_interaction,
quantity_operator_interaction,
operation_code,
operator_code,
day_of_week
]])
pred_days = model.predict(X)[0]
whole_days = int(pred_days)
fractional_day = pred_days - whole_days
total_seconds = fractional_day * 24 * 3600
hours = int(total_seconds // 3600)
minutes = int((total_seconds % 3600) // 60)
est_end_datetime = start_date + timedelta(days=whole_days, hours=hours, minutes=minutes)
est_end_date_str = est_end_datetime.strftime("%d-%m-%Y %H:%M")
duration_str = f"{whole_days} days, {hours} hours, {minutes} minutes"
return f"Estimated End Date & Time: {est_end_date_str}\nEstimated Duration: {duration_str}"
# Build Gradio interface
def gradio_interface(start_date, quantity, operation_type, operator_id, day_of_week):
return predict_end_date(start_date, quantity, operation_type, operator_id, day_of_week)
operation_options = list(le_operation_type.classes_)
operator_options = list(le_operator_id.classes_)
iface = gr.Interface(
fn=gradio_interface,
inputs=[
gr.Textbox(label="Start Date (DD-MM-YYYY)", value="21-07-2025"),
gr.Number(label="Quantity", value=6000),
gr.Dropdown(choices=operation_options, label="Operation Type", value=operation_options[0]),
gr.Dropdown(choices=operator_options, label="Operator ID", value=operator_options[0]),
gr.Slider(minimum=0, maximum=6, step=1, label="Day of Week (0=Monday, 6=Sunday)", value=1)
],
outputs="text",
title="Manufacturing Scheduler Prediction",
description="Predict the estimated end date and duration of a manufacturing job based on input parameters."
)
if __name__ == "__main__":
iface.launch()