Update app.py
Browse files
app.py
CHANGED
|
@@ -1,221 +1,99 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import joblib
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
import random
|
| 7 |
-
|
| 8 |
-
# Set page config
|
| 9 |
-
st.set_page_config(
|
| 10 |
-
page_title="Team Productivity Predictor",
|
| 11 |
-
page_icon="🏆",
|
| 12 |
-
layout="wide"
|
| 13 |
-
)
|
| 14 |
|
| 15 |
# Load the model
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
return None
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
'
|
| 52 |
-
'
|
| 53 |
-
'
|
| 54 |
-
'
|
| 55 |
-
'
|
| 56 |
-
'
|
| 57 |
-
'
|
| 58 |
-
'
|
| 59 |
-
'
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
}
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
'
|
| 78 |
-
'
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
st.metric("⏰ Deadline Days", sample_task['DeadlineDays'])
|
| 107 |
-
|
| 108 |
-
with col2:
|
| 109 |
-
st.metric("👨💼 Experience Years", f"{sample_task['ExperienceYears']}")
|
| 110 |
-
st.metric("⏱️ Avg Task Time (min)", f"{sample_task['AvgTaskTime_Minutes']}")
|
| 111 |
-
st.metric("❌ Error Rate", f"{sample_task['ErrorRate']:.3f}")
|
| 112 |
-
st.metric("📚 Training Hours", f"{sample_task['TrainingHours']}")
|
| 113 |
-
|
| 114 |
-
with col3:
|
| 115 |
-
st.metric("📅 Day Number", sample_task['DayNumber'])
|
| 116 |
-
st.metric("⭐ Priority Level", sample_task['PriorityLevel'])
|
| 117 |
-
st.metric("🚀 Throughput Rate", f"{sample_task['ThroughputRate']:.2f}")
|
| 118 |
-
st.metric("⚡ Time Pressure", f"{sample_task['TimePressure']:.3f}")
|
| 119 |
-
|
| 120 |
-
try:
|
| 121 |
-
# Build DataFrame with one row per team
|
| 122 |
-
rows = []
|
| 123 |
-
for team, specialty in teams_specialties.items():
|
| 124 |
-
r = sample_task.copy()
|
| 125 |
-
r['Team'] = team
|
| 126 |
-
r['Specialty'] = specialty
|
| 127 |
-
rows.append(r)
|
| 128 |
-
|
| 129 |
-
test_df = pd.DataFrame(rows)
|
| 130 |
-
|
| 131 |
-
# Make predictions
|
| 132 |
-
test_df['PredictedProductivity'] = model.predict(test_df)
|
| 133 |
-
ranked = test_df.sort_values('PredictedProductivity', ascending=False).reset_index(drop=True)
|
| 134 |
-
ranked['Rank'] = range(1, len(ranked) + 1)
|
| 135 |
-
|
| 136 |
-
st.header("🏆 Team Productivity Rankings")
|
| 137 |
-
|
| 138 |
-
# Display results in two columns
|
| 139 |
-
col1, col2 = st.columns([1, 1])
|
| 140 |
-
|
| 141 |
-
with col1:
|
| 142 |
-
st.subheader("📊 Rankings Table")
|
| 143 |
-
|
| 144 |
-
# Style the dataframe
|
| 145 |
-
display_df = ranked[['Rank', 'Team', 'Specialty', 'PredictedProductivity']].copy()
|
| 146 |
-
display_df['PredictedProductivity'] = display_df['PredictedProductivity'].round(4)
|
| 147 |
-
|
| 148 |
-
# Add medal emojis for top 3
|
| 149 |
-
medals = {1: "🥇", 2: "🥈", 3: "🥉"}
|
| 150 |
-
display_df['Medal'] = display_df['Rank'].map(lambda x: medals.get(x, ""))
|
| 151 |
-
|
| 152 |
-
# Reorder columns
|
| 153 |
-
display_df = display_df[['Medal', 'Rank', 'Team', 'Specialty', 'PredictedProductivity']]
|
| 154 |
-
|
| 155 |
-
st.dataframe(
|
| 156 |
-
display_df,
|
| 157 |
-
use_container_width=True,
|
| 158 |
-
hide_index=True
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
# Show top 3 teams with special formatting
|
| 162 |
-
st.subheader("🎖️ Top 3 Teams")
|
| 163 |
-
for i in range(min(3, len(ranked))):
|
| 164 |
-
team_data = ranked.iloc[i]
|
| 165 |
-
medal = ["🥇", "🥈", "🥉"][i]
|
| 166 |
-
st.success(f"{medal} **{team_data['Team']}** ({team_data['Specialty']}) - Score: {team_data['PredictedProductivity']:.4f}")
|
| 167 |
-
|
| 168 |
-
with col2:
|
| 169 |
-
st.subheader("📈 Productivity Chart")
|
| 170 |
-
|
| 171 |
-
# Create horizontal bar chart
|
| 172 |
-
fig, ax = plt.subplots(figsize=(8, 6))
|
| 173 |
-
|
| 174 |
-
# Color gradient for bars
|
| 175 |
-
colors = plt.cm.viridis(np.linspace(0, 1, len(ranked)))
|
| 176 |
-
|
| 177 |
-
bars = ax.barh(ranked['Team'], ranked['PredictedProductivity'], color=colors)
|
| 178 |
-
|
| 179 |
-
# Customize chart
|
| 180 |
-
ax.invert_yaxis()
|
| 181 |
-
ax.set_xlabel('Predicted Productivity Score', fontsize=12)
|
| 182 |
-
ax.set_title('Team Productivity Rankings', fontsize=14, fontweight='bold')
|
| 183 |
-
ax.grid(axis='x', linestyle='--', alpha=0.3)
|
| 184 |
-
|
| 185 |
-
# Add value labels on bars
|
| 186 |
-
for i, (bar, value) in enumerate(zip(bars, ranked['PredictedProductivity'])):
|
| 187 |
-
ax.text(value + 0.001, bar.get_y() + bar.get_height()/2,
|
| 188 |
-
f'{value:.3f}', ha='left', va='center', fontsize=9)
|
| 189 |
-
|
| 190 |
-
plt.tight_layout()
|
| 191 |
-
st.pyplot(fig)
|
| 192 |
-
plt.close()
|
| 193 |
-
|
| 194 |
-
except Exception as e:
|
| 195 |
-
st.error(f"Error making predictions: {str(e)}")
|
| 196 |
-
st.write("Please check that your model file is compatible with the expected features.")
|
| 197 |
-
|
| 198 |
-
else:
|
| 199 |
-
# Show instructions when no button is clicked
|
| 200 |
-
st.info("👆 Click the button above to generate a random task and see team productivity rankings!")
|
| 201 |
-
|
| 202 |
-
# Show example of what teams are available
|
| 203 |
-
st.subheader("👥 Available Teams")
|
| 204 |
-
teams_df = pd.DataFrame([
|
| 205 |
-
{"Team": team, "Specialty": specialty}
|
| 206 |
-
for team, specialty in {
|
| 207 |
-
'Team_1': 'Molding',
|
| 208 |
-
'Team_2': 'Packaging',
|
| 209 |
-
'Team_3': 'Quality Check',
|
| 210 |
-
'Team_4': 'Molding',
|
| 211 |
-
'Team_5': 'Boxing',
|
| 212 |
-
'Team_6': 'Shipping',
|
| 213 |
-
'Team_7': 'Boxing',
|
| 214 |
-
'Team_8': 'Packaging'
|
| 215 |
-
}.items()
|
| 216 |
-
])
|
| 217 |
-
|
| 218 |
-
st.dataframe(teams_df, use_container_width=True, hide_index=True)
|
| 219 |
|
| 220 |
-
|
| 221 |
-
main()
|
|
|
|
| 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 |
+
# Load the original dataset to extract real specialty mappings
|
| 13 |
+
df = pd.read_csv("dataset.csv") # <-- Make sure this file is uploaded to Hugging Face
|
| 14 |
+
|
| 15 |
+
# Get actual teams and specialties
|
| 16 |
+
teams = sorted(df['Team'].unique())
|
| 17 |
+
specialty_map = dict(zip(df['Team'], df['Specialty']))
|
|
|
|
| 18 |
|
| 19 |
+
# Sample task examples (all feature-engineered already)
|
| 20 |
+
sample_tasks = [
|
| 21 |
+
{
|
| 22 |
+
'ProductType': 'Mothball',
|
| 23 |
+
'TaskType': 'Packaging',
|
| 24 |
+
'OrderQuantity': 120,
|
| 25 |
+
'DeadlineDays': 4,
|
| 26 |
+
'ExperienceYears': 6,
|
| 27 |
+
'AvgTaskTime_Minutes': 28.0,
|
| 28 |
+
'ErrorRate': 0.05,
|
| 29 |
+
'TrainingHours': 20.0,
|
| 30 |
+
'DayNumber': 2
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
'ProductType': 'Perfume',
|
| 34 |
+
'TaskType': 'Boxing',
|
| 35 |
+
'OrderQuantity': 80,
|
| 36 |
+
'DeadlineDays': 2,
|
| 37 |
+
'ExperienceYears': 2,
|
| 38 |
+
'AvgTaskTime_Minutes': 45.0,
|
| 39 |
+
'ErrorRate': 0.12,
|
| 40 |
+
'TrainingHours': 10.0,
|
| 41 |
+
'DayNumber': 3
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
'ProductType': 'Soap',
|
| 45 |
+
'TaskType': 'Molding',
|
| 46 |
+
'OrderQuantity': 200,
|
| 47 |
+
'DeadlineDays': 6,
|
| 48 |
+
'ExperienceYears': 4,
|
| 49 |
+
'AvgTaskTime_Minutes': 32.0,
|
| 50 |
+
'ErrorRate': 0.07,
|
| 51 |
+
'TrainingHours': 15.0,
|
| 52 |
+
'DayNumber': 4
|
| 53 |
+
},
|
| 54 |
+
]
|
|
|
|
| 55 |
|
| 56 |
+
# Prediction function
|
| 57 |
+
def predict():
|
| 58 |
+
task = random.choice(sample_tasks)
|
| 59 |
+
|
| 60 |
+
# Feature engineering
|
| 61 |
+
task['ThroughputRate'] = task['OrderQuantity'] / task['AvgTaskTime_Minutes']
|
| 62 |
+
task['TimePressure'] = task['OrderQuantity'] / ((task['DeadlineDays'] or 1) * task['AvgTaskTime_Minutes'])
|
| 63 |
+
task['PriorityLevel'] = 3 # high priority
|
| 64 |
+
|
| 65 |
+
# Predict for each team
|
| 66 |
+
results = []
|
| 67 |
+
for team in teams:
|
| 68 |
+
row = task.copy()
|
| 69 |
+
row['Team'] = team
|
| 70 |
+
row['Specialty'] = specialty_map.get(team, "Unknown")
|
| 71 |
+
input_df = pd.DataFrame([row])
|
| 72 |
+
pred = model.predict(input_df)[0]
|
| 73 |
+
results.append((team, round(pred, 4)))
|
| 74 |
+
|
| 75 |
+
# Sort results
|
| 76 |
+
results.sort(key=lambda x: x[1], reverse=True)
|
| 77 |
+
|
| 78 |
+
# Plot
|
| 79 |
+
teams_sorted, scores_sorted = zip(*results)
|
| 80 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 81 |
+
ax.barh(teams_sorted, scores_sorted, color="skyblue")
|
| 82 |
+
ax.set_xlabel("Predicted Productivity")
|
| 83 |
+
ax.set_title("Team Ranking for Random Task")
|
| 84 |
+
ax.invert_yaxis()
|
| 85 |
+
plt.tight_layout()
|
| 86 |
+
|
| 87 |
+
return fig, json.dumps(task, indent=2)
|
| 88 |
+
|
| 89 |
+
# Gradio UI
|
| 90 |
+
demo = gr.Interface(
|
| 91 |
+
fn=predict,
|
| 92 |
+
inputs=[],
|
| 93 |
+
outputs=[gr.Plot(label="Team Productivity Rankings"), gr.Textbox(label="Task Details")],
|
| 94 |
+
live=False,
|
| 95 |
+
title="Team Productivity Predictor",
|
| 96 |
+
description="Click Generate to predict the best team for a randomly selected task."
|
| 97 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
demo.launch()
|
|
|