Jarvis0525/ai-task-allocator
0
1from sentence_transformers import SentenceTransformer, util2import logging3import random4from database import load_data5 6# Setup Logging7logging.basicConfig(filename="logs/match_logs.txt", level=logging.INFO, format="%(asctime)s - %(message)s")8 9# Load pre-trained model10model = SentenceTransformer("all-MiniLM-L6-v2")11 12# Minimum similarity threshold to filter out incorrect matches13MIN_SIMILARITY = 0.514 15 16def match_task(task_desc, task_priority, location_preference=None):17 """Finds the best employee for a given task using AI-based ranking."""18 19 employees = load_data("data/dataset.json")20 task_embedding = model.encode(task_desc, convert_to_tensor=True)21 22 best_candidates = []23 24 for employee in employees:25 if employee["availability"] == "Busy":26 continue # Skip employees who are not available27 28 # Compute similarity between task and employee skills29 skills_embedding = model.encode(employee["skills"], convert_to_tensor=True)30 skills_score = util.pytorch_cos_sim(task_embedding, skills_embedding).max().item()31 32 # Adjust scoring weights33 task_load_score = 1 - (employee["current_tasks"] / 5) # Normalize (Assume max 5 tasks)34 location_score = 1 if location_preference and location_preference.lower() == employee[35 "location"].lower() else 0.536 37 final_score = (skills_score * 0.7) + (task_load_score * 0.2) + (location_score * 0.1)38 39 # Log results40 logging.info(f"Checked {employee['name']} - Score: {final_score:.4f}")41 42 # Select candidates who meet the similarity threshold43 if final_score >= MIN_SIMILARITY:44 best_candidates.append((final_score, employee))45 46 # If no suitable candidate found47 if not best_candidates:48 return {"name": "No Match Found", "skills": [], "location": "N/A"}49 50 # Sort by score (descending) and pick top candidates51 best_candidates.sort(reverse=True, key=lambda x: x[0])52 53 # If multiple candidates have the same best score, pick one randomly54 top_score = best_candidates[0][0]55 top_matches = [c[1] for c in best_candidates if c[0] == top_score]56 57 return random.choice(top_matches) if top_matches else best_candidates[0][1]58 