nodronm commited on
Commit
a50721f
·
verified ·
1 Parent(s): 34b6aad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -57
app.py CHANGED
@@ -1,82 +1,97 @@
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()
 
1
  import gradio as gr
2
+ import matplotlib.pyplot as plt
3
  import numpy as np
4
  import pandas as pd
5
+ import joblib
6
  import random
7
+ import json
8
 
9
+ # Load the model
10
+ model = joblib.load("model.joblib")
11
 
12
+ # Team list (based on your dataset)
13
+ teams = [f"team_{i}" for i in range(1, 13)]
 
14
 
15
+ # Sample task examples (all feature-engineered already)
16
+ sample_tasks = [
17
+ {
18
+ 'ProductType': 'Mothball',
19
+ 'TaskType': 'Packaging',
20
+ 'OrderQuantity': 120,
21
+ 'DeadlineDays': 4,
22
+ 'ExperienceYears': 6,
23
+ 'AvgTaskTime_Minutes': 28.0,
24
+ 'ErrorRate': 0.05,
25
+ 'TrainingHours': 20.0,
26
+ 'DayNumber': 2
27
+ },
28
+ {
29
+ 'ProductType': 'Insecticide',
30
+ 'TaskType': 'Labeling',
31
+ 'OrderQuantity': 80,
32
+ 'DeadlineDays': 2,
33
+ 'ExperienceYears': 2,
34
+ 'AvgTaskTime_Minutes': 45.0,
35
+ 'ErrorRate': 0.12,
36
+ 'TrainingHours': 10.0,
37
+ 'DayNumber': 3
38
+ },
39
+ {
40
+ 'ProductType': 'Soap',
41
+ 'TaskType': 'Mixing',
42
+ 'OrderQuantity': 200,
43
+ 'DeadlineDays': 6,
44
+ 'ExperienceYears': 4,
45
+ 'AvgTaskTime_Minutes': 32.0,
46
+ 'ErrorRate': 0.07,
47
+ 'TrainingHours': 15.0,
48
+ 'DayNumber': 4
49
+ },
50
+ ]
51
 
52
+ # Priority map
53
+ priority_map = {'High': 3, 'Medium': 2, 'Low': 1}
54
+
55
+ # Prediction function
56
  def predict():
57
+ task = random.choice(sample_tasks)
58
+
59
+ # Feature engineering
60
+ task['ThroughputRate'] = task['OrderQuantity'] / task['AvgTaskTime_Minutes']
61
+ task['TimePressure'] = task['OrderQuantity'] / ((task['DeadlineDays'] or 1) * task['AvgTaskTime_Minutes'])
62
+ task['PriorityLevel'] = 3 # all samples are high priority
63
 
64
+ # Predict for each team
65
+ results = []
66
  for team in teams:
67
+ row = task.copy()
68
+ row['Team'] = team
69
+ input_df = pd.DataFrame([row])
70
+ pred = model.predict(input_df)[0]
71
+ results.append((team, round(pred, 4)))
72
 
73
+ # Sort results
74
+ results.sort(key=lambda x: x[1], reverse=True)
75
 
76
  # Plot
77
+ teams_sorted, scores_sorted = zip(*results)
78
+ fig, ax = plt.subplots(figsize=(10, 6))
79
+ ax.barh(teams_sorted, scores_sorted, color="skyblue")
80
  ax.set_xlabel("Predicted Productivity")
81
+ ax.set_title("Team Ranking for Random Task")
82
  ax.invert_yaxis()
83
  plt.tight_layout()
84
 
85
+ return fig, json.dumps(task, indent=2)
 
 
 
86
 
87
  # Gradio UI
88
  demo = gr.Interface(
89
  fn=predict,
90
  inputs=[],
91
+ outputs=[gr.Plot(label="Team Productivity Rankings"), gr.Textbox(label="Task Details")],
92
+ live=False,
93
+ title="Team Productivity Predictor",
94
+ description="Click Generate to predict the best team for a randomly selected task."
 
 
95
  )
96
 
97
+ demo.launch()