nodronm commited on
Commit
34b6aad
Β·
verified Β·
1 Parent(s): 28cb06f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -65
app.py CHANGED
@@ -1,88 +1,82 @@
1
  import gradio as gr
2
- import pandas as pd
3
  import numpy as np
 
4
  import matplotlib.pyplot as plt
5
- import joblib
6
 
7
  # Load model
8
- model = joblib.load("model.joblib")
9
 
10
- # Define example tasks
11
- example_tasks = {
12
- # 🟒 High Productivity Task
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
- # 🟑 Medium Productivity Task
24
- "Soap β†’ Molding (Moderate Productivity)": {
25
- 'quarter': 'Q2', 'department': 'sewing', 'day': 'Wednesday',
26
- 'no_of_workers': 48, 'incentive': 2.5, 'idle_time': 0.3,
27
- 'idle_men': 4, 'smv': 30.0, 'month': 5, 'day_of_week': 2,
28
- 'is_weekend': 0, 'smv_per_worker': 30.0 / 48,
29
- 'effort_index': 30.0 + 2.5 + 1.0 - 0.3,
30
- 'log_wip': np.log1p(50), 'log_overtime': np.log1p(1.0),
31
- 'no_of_style_change': 0, 'targeted_productivity': 0.75
32
- },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- # πŸ”΄ Low Productivity Task
35
- "Mothball β†’ Packaging (Low Productivity)": {
36
- 'quarter': 'Q4', 'department': 'finishing', 'day': 'Saturday',
37
- 'no_of_workers': 38, 'incentive': 1.0, 'idle_time': 1.5,
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
- row = base.copy()
57
- row['Team'] = team
58
- df_row = pd.DataFrame([row])
59
- pred = model.predict(df_row)[0]
60
- predictions.append((team, round(pred, 5)))
61
 
62
- result_df = pd.DataFrame(predictions, columns=["Team", "Predicted Productivity"])
63
- result_df = result_df.sort_values(by="Predicted Productivity", ascending=False)
64
 
65
- # Plot bar chart
66
- fig, ax = plt.subplots(figsize=(10, 6))
67
- ax.barh(result_df["Team"], result_df["Predicted Productivity"], color="skyblue")
68
  ax.set_xlabel("Predicted Productivity")
69
- ax.set_title(f"Team Ranking for: {task_name}")
70
  ax.invert_yaxis()
71
  plt.tight_layout()
72
 
73
- return result_df, fig
 
 
 
74
 
75
  # Gradio UI
76
  demo = gr.Interface(
77
- fn=predict_productivity,
78
- inputs=gr.Dropdown(choices=list(example_tasks.keys()), label="Select Task Scenario"),
79
  outputs=[
80
- gr.Dataframe(label="πŸ† Team Productivity Rankings:"),
81
- gr.Plot(label="πŸ“ˆ Team Productivity Graph")
82
  ],
83
- title="πŸ‘· Team Assignment Predictor",
84
- description="Select a task to predict productivity scores for all teams. The best team will be at the top!"
85
  )
86
 
87
- if __name__ == "__main__":
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()