Spaces:
Sleeping
Sleeping
| import json | |
| import pandas as pd | |
| import gradio as gr | |
| # Load datasets from JSON files in the same folder | |
| with open('workers.json', 'r') as f: | |
| workers = json.load(f) | |
| with open('tasks.json', 'r') as f: | |
| tasks = json.load(f) | |
| #------ Core algorithm ------# | |
| class SkillMatchPro: | |
| def __init__(self, workers, tasks): | |
| self.workers = workers | |
| self.tasks = tasks | |
| def calculate_match_score(self, worker, task): | |
| score = 0 | |
| req = set(task['required_skills']) | |
| if req: | |
| score += (len(set(worker['skills']) & req) / len(req)) * 40 | |
| score += min(worker['experience_years'] / 10, 1) * 30 | |
| score += 20 if worker['availability'] == "Available" else 5 if worker['availability'] == "Busy" else 0 | |
| score += 10 if worker['shift'] == task['shift_required'] else 0 | |
| score += (worker['performance_rating'] - 3) * 2 | |
| return round(score, 2) | |
| def get_best_matches(self, task_id, top_n=5): | |
| task = next((x for x in self.tasks if x['task_id'] == task_id), None) | |
| if not task: | |
| return [] | |
| matches = [] | |
| for w in self.workers: | |
| matches.append({ | |
| 'worker_id': w['worker_id'], | |
| 'worker_name': w['name'], | |
| 'score': self.calculate_match_score(w, task), | |
| 'skills': w['skills'], | |
| 'experience': w['experience_years'], | |
| 'availability': w['availability'], | |
| 'shift': w['shift'], | |
| 'performance_rating': w['performance_rating'] | |
| }) | |
| matches.sort(key=lambda x: x['score'], reverse=True) | |
| return matches[:top_n] | |
| def assign_all_tasks(self): | |
| assignments = [] | |
| assigned_workers = set() | |
| priority_order = {'High': 3, 'Medium': 2, 'Low': 1} | |
| for task in sorted(self.tasks, key=lambda x: priority_order[x['priority']], reverse=True): | |
| best_matches = self.get_best_matches(task['task_id']) | |
| for m in best_matches: | |
| if m['worker_id'] not in assigned_workers and m['availability'] == "Available": | |
| assignments.append({ | |
| 'task_id': task['task_id'], | |
| 'task_name': task['task_name'], | |
| 'assigned_worker': m['worker_name'], | |
| 'worker_id': m['worker_id'], | |
| 'match_score': m['score'], | |
| 'priority': task['priority'] | |
| }) | |
| assigned_workers.add(m['worker_id']) | |
| break | |
| else: | |
| if best_matches: | |
| top = best_matches[0] | |
| assignments.append({ | |
| 'task_id': task['task_id'], | |
| 'task_name': task['task_name'], | |
| 'assigned_worker': f"{top['worker_name']} (Override)", | |
| 'worker_id': top['worker_id'], | |
| 'match_score': top['score'], | |
| 'priority': task['priority'] | |
| }) | |
| return assignments | |
| matcher = SkillMatchPro(workers, tasks) | |
| # ------ Gradio UI ------# | |
| # Create mapping from task_id to task_name | |
| task_id_to_name = {t['task_id']: t['task_name'] for t in tasks} | |
| # Prepare dropdown choices as (label, value) for nice display | |
| dropdown_choices = [(t['task_name'], t['task_id']) for t in tasks] | |
| def find_matches(task_id): | |
| if not task_id: | |
| return "Please select a task.", [] | |
| task_name = task_id_to_name.get(task_id, task_id) | |
| matches = matcher.get_best_matches(task_id, top_n=5) | |
| table = [[m['worker_name'], m['score'], ", ".join(m['skills']), | |
| m['experience'], m['availability'], m['shift']] for m in matches] | |
| return f"Best matches for \"{task_name}\":", table | |
| def auto_assign(): | |
| assignments = matcher.assign_all_tasks() | |
| table = [[a['task_id'], a['task_name'], a['assigned_worker'], a['match_score'], a['priority']] for a in assignments] | |
| return f"Auto-assignment completed! ({len(assignments)} tasks assigned)", table | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🏭 SkillMatch Pro - Workforce Assignment") | |
| with gr.Row(): | |
| with gr.Column(): | |
| dropdown = gr.Dropdown(choices=dropdown_choices, label="Select Task") | |
| find_button = gr.Button("Find Best Workers") | |
| with gr.Column(): | |
| result_text1 = gr.Textbox(label="Result") | |
| result_table1 = gr.Dataframe( | |
| headers=["Worker", "Score", "Skills", "Experience", "Availability", "Shift"] | |
| ) | |
| find_button.click(find_matches, inputs=dropdown, outputs=[result_text1, result_table1]) | |
| gr.Markdown("---") | |
| with gr.Row(): | |
| with gr.Column(): | |
| auto_assign_button = gr.Button("Auto-Assign All Tasks") | |
| with gr.Column(): | |
| result_text2 = gr.Textbox(label="Result") | |
| result_table2 = gr.Dataframe( | |
| headers=["Task ID", "Task Name", "Assigned Worker", "Score", "Priority"] | |
| ) | |
| auto_assign_button.click(auto_assign, outputs=[result_text2, result_table2]) | |
| demo.launch() | |