Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,97 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
-
import
|
| 6 |
import random
|
|
|
|
| 7 |
|
| 8 |
-
# Load model
|
| 9 |
-
model = joblib.load("model.joblib")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
teams = sorted(df['Team'].unique())
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
#
|
|
|
|
|
|
|
|
|
|
| 43 |
def predict():
|
| 44 |
-
task =
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
|
|
|
|
|
|
| 47 |
for team in teams:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
pred = model.predict(
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
# Plot
|
| 58 |
-
|
| 59 |
-
ax.
|
|
|
|
| 60 |
ax.set_xlabel("Predicted Productivity")
|
| 61 |
-
ax.set_title("
|
| 62 |
ax.invert_yaxis()
|
| 63 |
plt.tight_layout()
|
| 64 |
|
| 65 |
-
|
| 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 |
-
|
| 76 |
-
|
| 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()
|