Update app.py
Browse files
app.py
CHANGED
|
@@ -3,44 +3,47 @@ import matplotlib.pyplot as plt
|
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
import joblib
|
|
|
|
| 6 |
|
| 7 |
# Load the model and dataset
|
| 8 |
-
model = joblib.load("
|
| 9 |
-
df = pd.read_csv("worker_productivity.csv") #
|
| 10 |
|
| 11 |
# Get unique teams
|
| 12 |
teams = sorted(df['team'].unique())
|
| 13 |
|
| 14 |
-
# Define
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
'
|
| 32 |
-
'
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Prediction function
|
| 36 |
-
|
| 37 |
def predict():
|
|
|
|
| 38 |
team_scores = []
|
| 39 |
|
| 40 |
for team in teams:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
task_df = pd.DataFrame([
|
| 44 |
pred = model.predict(task_df)[0]
|
| 45 |
team_scores.append((team, pred))
|
| 46 |
|
|
@@ -50,22 +53,31 @@ def predict():
|
|
| 50 |
|
| 51 |
# Plot results
|
| 52 |
fig, ax = plt.subplots(figsize=(10, 6))
|
| 53 |
-
ax.barh(team_scores_df["Team"].astype(str), team_scores_df["Predicted Productivity"], color='skyblue')
|
| 54 |
ax.set_xlabel("Predicted Productivity")
|
| 55 |
ax.set_title("Predicted Productivity by Team for Custom Task")
|
| 56 |
ax.invert_yaxis()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
plt.tight_layout()
|
| 58 |
|
| 59 |
-
|
|
|
|
| 60 |
|
| 61 |
# Gradio UI
|
| 62 |
demo = gr.Interface(
|
| 63 |
fn=predict,
|
| 64 |
inputs=[],
|
| 65 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
| 66 |
live=False,
|
| 67 |
title="Worker Productivity Predictor",
|
| 68 |
-
description="Generates predicted productivity scores for each team on a
|
| 69 |
)
|
| 70 |
|
| 71 |
demo.launch()
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
import joblib
|
| 6 |
+
import random
|
| 7 |
|
| 8 |
# Load the model and dataset
|
| 9 |
+
model = joblib.load("model.joblib")
|
| 10 |
+
df = pd.read_csv("worker_productivity.csv") # Ensure this is uploaded to Hugging Face Space
|
| 11 |
|
| 12 |
# Get unique teams
|
| 13 |
teams = sorted(df['team'].unique())
|
| 14 |
|
| 15 |
+
# Define task generation function
|
| 16 |
+
def generate_task():
|
| 17 |
+
task = {
|
| 18 |
+
'quarter': random.choice(['Q1', 'Q2', 'Q3', 'Q4']),
|
| 19 |
+
'department': random.choice(['sewing', 'finishing']),
|
| 20 |
+
'day': random.choice(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']),
|
| 21 |
+
'no_of_workers': random.randint(30, 60),
|
| 22 |
+
'incentive': round(random.uniform(1.0, 4.0), 2),
|
| 23 |
+
'idle_time': round(random.uniform(0.0, 1.0), 2),
|
| 24 |
+
'idle_men': random.randint(0, 10),
|
| 25 |
+
'smv': round(random.uniform(20.0, 40.0), 2),
|
| 26 |
+
'month': random.randint(1, 12),
|
| 27 |
+
'day_of_week': random.randint(0, 6),
|
| 28 |
+
'is_weekend': random.choice([0, 1]),
|
| 29 |
+
'no_of_style_change': random.randint(0, 2),
|
| 30 |
+
'targeted_productivity': round(random.uniform(0.5, 0.95), 2)
|
| 31 |
+
}
|
| 32 |
+
task['smv_per_worker'] = task['smv'] / task['no_of_workers']
|
| 33 |
+
task['effort_index'] = task['smv'] + task['incentive'] + 1.0 - task['idle_time']
|
| 34 |
+
task['log_wip'] = np.log1p(random.randint(20, 100))
|
| 35 |
+
task['log_overtime'] = np.log1p(random.uniform(0.5, 3.0))
|
| 36 |
+
return task
|
| 37 |
|
| 38 |
# Prediction function
|
|
|
|
| 39 |
def predict():
|
| 40 |
+
task = generate_task()
|
| 41 |
team_scores = []
|
| 42 |
|
| 43 |
for team in teams:
|
| 44 |
+
t = task.copy()
|
| 45 |
+
t['team'] = team
|
| 46 |
+
task_df = pd.DataFrame([t])
|
| 47 |
pred = model.predict(task_df)[0]
|
| 48 |
team_scores.append((team, pred))
|
| 49 |
|
|
|
|
| 53 |
|
| 54 |
# Plot results
|
| 55 |
fig, ax = plt.subplots(figsize=(10, 6))
|
| 56 |
+
bars = ax.barh(team_scores_df["Team"].astype(str), team_scores_df["Predicted Productivity"], color='skyblue')
|
| 57 |
ax.set_xlabel("Predicted Productivity")
|
| 58 |
ax.set_title("Predicted Productivity by Team for Custom Task")
|
| 59 |
ax.invert_yaxis()
|
| 60 |
+
|
| 61 |
+
for bar in bars:
|
| 62 |
+
width = bar.get_width()
|
| 63 |
+
ax.text(width + 0.01, bar.get_y() + bar.get_height()/2, f"{width:.2f}", va='center')
|
| 64 |
+
|
| 65 |
plt.tight_layout()
|
| 66 |
|
| 67 |
+
task_info = "\n".join([f"{k}: {v}" for k, v in task.items()])
|
| 68 |
+
return fig, task_info
|
| 69 |
|
| 70 |
# Gradio UI
|
| 71 |
demo = gr.Interface(
|
| 72 |
fn=predict,
|
| 73 |
inputs=[],
|
| 74 |
+
outputs=[
|
| 75 |
+
gr.Plot(label="Team Productivity Rankings"),
|
| 76 |
+
gr.Textbox(label="Task Details", lines=20)
|
| 77 |
+
],
|
| 78 |
live=False,
|
| 79 |
title="Worker Productivity Predictor",
|
| 80 |
+
description="Generates predicted productivity scores for each team on a random custom task."
|
| 81 |
)
|
| 82 |
|
| 83 |
demo.launch()
|