Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,82 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
-
import
|
| 6 |
|
| 7 |
# Load model
|
| 8 |
-
model = joblib.load("model.joblib")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
"Candle β QualityCheck (High Productivity)": {
|
| 14 |
-
'quarter': 'Q1', 'department': 'quality', 'day': 'Tuesday',
|
| 15 |
-
'no_of_workers': 55, 'incentive': 3.5, 'idle_time': 0.1,
|
| 16 |
-
'idle_men': 2, 'smv': 20.0, 'month': 2, 'day_of_week': 1,
|
| 17 |
-
'is_weekend': 0, 'smv_per_worker': 20.0 / 55,
|
| 18 |
-
'effort_index': 20.0 + 3.5 + 1.0 - 0.1,
|
| 19 |
-
'log_wip': np.log1p(30), 'log_overtime': np.log1p(1.0),
|
| 20 |
-
'no_of_style_change': 0, 'targeted_productivity': 0.9
|
| 21 |
-
},
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
'idle_men': 10, 'smv': 35.0, 'month': 12, 'day_of_week': 5,
|
| 39 |
-
'is_weekend': 1, 'smv_per_worker': 35.0 / 38,
|
| 40 |
-
'effort_index': 35.0 + 1.0 + 1.0 - 1.5,
|
| 41 |
-
'log_wip': np.log1p(90), 'log_overtime': np.log1p(0.5),
|
| 42 |
-
'no_of_style_change': 2, 'targeted_productivity': 0.60
|
| 43 |
-
}
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
# Define team list
|
| 48 |
-
teams = [f"team_{i}" for i in range(1, 13)]
|
| 49 |
-
|
| 50 |
-
# Prediction function
|
| 51 |
-
def predict_productivity(task_name):
|
| 52 |
-
base = example_tasks[task_name]
|
| 53 |
-
predictions = []
|
| 54 |
|
| 55 |
for team in teams:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
pred = model.predict(
|
| 60 |
-
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
# Plot
|
| 66 |
-
fig, ax = plt.subplots(figsize=(
|
| 67 |
-
ax.barh(
|
| 68 |
ax.set_xlabel("Predicted Productivity")
|
| 69 |
-
ax.set_title(
|
| 70 |
ax.invert_yaxis()
|
| 71 |
plt.tight_layout()
|
| 72 |
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
# Gradio UI
|
| 76 |
demo = gr.Interface(
|
| 77 |
-
fn=
|
| 78 |
-
inputs=
|
| 79 |
outputs=[
|
| 80 |
-
gr.
|
| 81 |
-
gr.Plot(label="π Team Productivity
|
| 82 |
],
|
| 83 |
-
title="
|
| 84 |
-
description="
|
| 85 |
)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
+
import random
|
| 7 |
|
| 8 |
# Load model
|
| 9 |
+
model = joblib.load("model.joblib") # Ensure your model was saved as a Pipeline
|
| 10 |
|
| 11 |
+
# Load base data for teams
|
| 12 |
+
df = pd.read_csv("data.csv") # Used to get team names
|
| 13 |
+
teams = sorted(df['Team'].unique())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Define possible randomized tasks
|
| 16 |
+
def get_random_task():
|
| 17 |
+
tasks = [
|
| 18 |
+
{
|
| 19 |
+
'quarter': 'Q1', 'department': 'molding', 'day': 'Monday', 'no_of_workers': 40,
|
| 20 |
+
'incentive': 2.0, 'idle_time': 0.3, 'idle_men': 3, 'smv': 25.0, 'month': 3,
|
| 21 |
+
'day_of_week': 0, 'is_weekend': 0, 'log_wip': np.log1p(35), 'log_overtime': np.log1p(0.8),
|
| 22 |
+
'no_of_style_change': 0, 'targeted_productivity': 0.75
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
'quarter': 'Q3', 'department': 'packaging', 'day': 'Wednesday', 'no_of_workers': 52,
|
| 26 |
+
'incentive': 1.5, 'idle_time': 0.6, 'idle_men': 6, 'smv': 28.0, 'month': 8,
|
| 27 |
+
'day_of_week': 2, 'is_weekend': 0, 'log_wip': np.log1p(60), 'log_overtime': np.log1p(0.5),
|
| 28 |
+
'no_of_style_change': 1, 'targeted_productivity': 0.7
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
'quarter': 'Q4', 'department': 'quality', 'day': 'Friday', 'no_of_workers': 35,
|
| 32 |
+
'incentive': 3.0, 'idle_time': 0.1, 'idle_men': 1, 'smv': 32.0, 'month': 11,
|
| 33 |
+
'day_of_week': 4, 'is_weekend': 0, 'log_wip': np.log1p(20), 'log_overtime': np.log1p(1.2),
|
| 34 |
+
'no_of_style_change': 0, 'targeted_productivity': 0.85
|
| 35 |
+
}
|
| 36 |
+
]
|
| 37 |
+
task = random.choice(tasks)
|
| 38 |
+
task['smv_per_worker'] = task['smv'] / task['no_of_workers']
|
| 39 |
+
task['effort_index'] = task['smv'] + task['incentive'] + 1.0 - task['idle_time']
|
| 40 |
+
return task
|
| 41 |
|
| 42 |
+
# Function to predict productivity and return sorted chart and summary
|
| 43 |
+
def predict():
|
| 44 |
+
task = get_random_task()
|
| 45 |
+
team_scores = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
for team in teams:
|
| 48 |
+
t = task.copy()
|
| 49 |
+
t['Team'] = team
|
| 50 |
+
task_df = pd.DataFrame([t])
|
| 51 |
+
pred = model.predict(task_df)[0]
|
| 52 |
+
team_scores.append((team, pred))
|
| 53 |
|
| 54 |
+
df_scores = pd.DataFrame(team_scores, columns=["Team", "Predicted Productivity"])
|
| 55 |
+
df_scores = df_scores.sort_values(by="Predicted Productivity", ascending=False)
|
| 56 |
|
| 57 |
+
# Plot
|
| 58 |
+
fig, ax = plt.subplots(figsize=(8, 6))
|
| 59 |
+
ax.barh(df_scores['Team'], df_scores['Predicted Productivity'], color='skyblue')
|
| 60 |
ax.set_xlabel("Predicted Productivity")
|
| 61 |
+
ax.set_title("π Team Ranking for Random Task")
|
| 62 |
ax.invert_yaxis()
|
| 63 |
plt.tight_layout()
|
| 64 |
|
| 65 |
+
# Convert task to readable summary
|
| 66 |
+
task_summary = "\n".join([f"{k}: {v}" for k, v in task.items()])
|
| 67 |
+
|
| 68 |
+
return task_summary, fig
|
| 69 |
|
| 70 |
# Gradio UI
|
| 71 |
demo = gr.Interface(
|
| 72 |
+
fn=predict,
|
| 73 |
+
inputs=[],
|
| 74 |
outputs=[
|
| 75 |
+
gr.Textbox(label="π Random Task Details"),
|
| 76 |
+
gr.Plot(label="π Team Productivity Rankings")
|
| 77 |
],
|
| 78 |
+
title="π§ Team Assignment Predictor",
|
| 79 |
+
description="Click Submit to generate a random task and rank all teams based on predicted productivity."
|
| 80 |
)
|
| 81 |
|
| 82 |
+
demo.launch()
|
|
|