Spaces:
Build error
Build error
File size: 26,505 Bytes
13c3f2c | 1 2 3 4 5 6 7 8 9 10 11 12 13 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 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 | """
Root Cause Classification Service for Gapura AI
Auto-classifies root causes into standard categories
"""
import os
import logging
import pickle
import re
from typing import List, Dict, Any, Optional, Tuple
from collections import Counter, defaultdict
import numpy as np
logger = logging.getLogger(__name__)
ROOT_CAUSE_CATEGORIES = {
"Equipment Failure": {
"keywords": [
"equipment",
"mesin",
"alat",
"rusak",
"broken",
"damage",
"malfunction",
"scanner",
"printer",
"computer",
"komputer",
"conveyor",
"conveyor belt",
"cbl",
"belt",
"forklift",
"pallet",
"scale",
"timbangan",
"x-ray",
"scanner",
"system down",
"hardware",
"software",
"error system",
"gangguan sistem",
"kerusakan",
"breakdown",
"tidak berfungsi",
"tidak bisa digunakan",
"trouble",
"hose",
"hll",
"leak",
"bocor",
"oil",
"fuel",
"solenoid",
"selenoid",
],
"severity_multiplier": 1.3,
"description": "Issues caused by equipment malfunction or failure",
},
"Staff Competency": {
"keywords": [
"staff",
"karyawan",
"pegawai",
"operator",
"kurang",
"tidak paham",
"tidak mengerti",
"salah",
"mistake",
"human error",
"kelalaian",
"incompetence",
"skill",
"pengalaman",
"baru",
"training",
"pelatihan",
"tidak fokus",
"tidak teliti",
"teledor",
"lupa",
"inexperienced",
"inappropriate handling",
"careless",
"negligence",
"tanpa supervisor",
],
"severity_multiplier": 1.2,
"description": "Issues caused by staff knowledge or skill gaps",
},
"Process/Procedure": {
"keywords": [
"procedure",
"prosedur",
"process",
"proses",
"sop",
"standard",
"tidak sesuai",
"non-compliance",
"pelanggaran",
"violation",
"bypass",
"skip",
"langkah",
"step",
"urutan",
"sequence",
"tidak patuh",
"melanggar",
"aturan",
"rules",
"policy",
"inconsistency",
"tidak konsisten",
"deviasi",
"deviation",
"flow",
"workflow",
"ketidaksesuaian",
],
"severity_multiplier": 1.1,
"description": "Issues caused by procedure violations or process failures",
},
"Communication": {
"keywords": [
"communication",
"komunikasi",
"informasi",
"koordinasi",
"coordination",
"miscommunication",
"misunderstanding",
"tidak jelas",
"unclear",
"konfirmasi",
"confirmation",
"notif",
"notification",
"pemberitahuan",
"sosialisasi",
"tidak terima",
"tidak sampai",
"lost in translation",
"bahasa",
"language",
"interpretasi",
"interpretation",
"beda informasi",
"informasi berbeda",
"salah paham",
"miskomunikasi",
"koordinasi kurang",
],
"severity_multiplier": 1.0,
"description": "Issues caused by communication breakdowns",
},
"External Factors": {
"keywords": [
"weather",
"cuaca",
"rain",
"hujan",
"storm",
"badai",
"lightning",
"flight delay",
"delay",
"airport",
"bandara",
"customs",
"bea cukai",
"airline",
"maskapai",
"vendor",
"third party",
"pihak ketiga",
"schedule",
"jadwal",
"traffic",
"macet",
"force majeure",
"bencana",
"disaster",
"pandemic",
"covid",
"external",
"di luar kendali",
"beyond control",
"unforeseen",
"unexpected",
"konflik",
"demo",
"strikes",
"pemogokan",
],
"severity_multiplier": 0.8,
"description": "Issues caused by external events or parties",
},
"Documentation": {
"keywords": [
"document",
"dokumen",
"paperwork",
"awb",
"air waybill",
"manifest",
"label",
"tag",
"sticker",
"barcode",
"missing",
"hilang",
"lost",
"incomplete",
"tidak lengkap",
"wrong",
"salah",
"error",
"typo",
"incorrect",
"tidak sesuai",
"mismatch",
"data entry",
"input",
"recording",
"pencatatan",
"reporting",
"pelaporan",
"faktur",
"invoice",
"packing list",
"dokumen tidak lengkap",
"admin error",
],
"severity_multiplier": 1.0,
"description": "Issues caused by documentation errors or missing documents",
},
"Training Gap": {
"keywords": [
"training",
"pelatihan",
"education",
"edukasi",
"briefing",
"arahan",
"new employee",
"karyawan baru",
"orientation",
"orientasi",
"tidak dilatih",
"untrained",
"refresher",
"update",
"perbaruan",
"knowledge gap",
"kesenjangan pengetahuan",
"competency",
"kompetensi",
"sertifikasi",
"certification",
"qualification",
"kualifikasi",
"belum pernah",
"never done",
"jarak training terakhir",
],
"severity_multiplier": 1.1,
"description": "Issues caused by lack of training or refresher",
},
"Resource/Manpower": {
"keywords": [
"manpower",
"manpower shortage",
"kekurangan",
"shortage",
"understaffed",
"kurang staf",
"overwhelmed",
"kewalahan",
"peak hour",
"jam sibuk",
"high volume",
"volume tinggi",
"many flights",
"banyak penerbangan",
"resource",
"sumber daya",
"allocation",
"alokasi",
"overtime",
"lembur",
"shift",
"jadwal",
"schedule conflict",
"konflik jadwal",
"double task",
"multitasking",
"terlalu banyak",
"overload",
],
"severity_multiplier": 1.0,
"description": "Issues caused by resource or manpower constraints",
},
}
class RootCauseService:
"""
Root cause classification service
Uses keyword matching + TF-IDF similarity for classification
"""
def __init__(self):
self.classifier = None
self.vectorizer = None
self.patterns = {}
self._classification_cache = {} # In-memory cache for speed
self._load_model()
def _load_model(self):
"""Load trained classifier if available"""
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
model_dir = os.path.join(base_dir, "models", "root_cause")
clf_path = os.path.join(model_dir, "classifier.pkl")
vec_path = os.path.join(model_dir, "vectorizer.pkl")
if os.path.exists(clf_path) and os.path.exists(vec_path):
try:
with open(clf_path, "rb") as f:
self.classifier = pickle.load(f)
with open(vec_path, "rb") as f:
self.vectorizer = pickle.load(f)
logger.info("✓ Root cause ML classifier loaded")
except Exception as e:
logger.warning(f"Failed to load root cause ML model: {e}")
def _classify_ml(self, root_cause_text: str, report_text: str) -> Optional[Dict[str, Any]]:
"""
Classify using trained TF-IDF + classifier if available
"""
if self.classifier is None or self.vectorizer is None:
return None
combined = f"{root_cause_text} {report_text}".strip()
if not combined or len(combined) < 6:
return None
try:
X = self.vectorizer.transform([combined])
if hasattr(self.classifier, "predict_proba"):
proba = self.classifier.predict_proba(X)[0]
idx = int(np.argmax(proba))
label = self.classifier.classes_[idx]
conf = float(proba[idx])
else:
label = self.classifier.predict(X)[0]
conf = 0.7
if conf < 0.4:
return None
return {
"primary_category": str(label),
"confidence": round(conf, 2),
"all_scores": {str(label): round(conf, 2)},
"keywords_matched": [],
"secondary_categories": [],
"description": ROOT_CAUSE_CATEGORIES.get(str(label), {}).get("description", ""),
"method": "tfidf-ml",
}
except Exception as e:
logger.warning(f"ML RC classification failed: {e}")
return None
def _classify_keyword(self, root_cause_text: str, report_text: str) -> Dict[str, Any]:
"""
Classify using keyword matching (Fast fallback)
"""
combined_text = f"{root_cause_text} {report_text}".lower()
scores = {}
matched_keywords = defaultdict(list)
for category, config in ROOT_CAUSE_CATEGORIES.items():
keywords = config["keywords"]
score = 0
matched = []
for keyword in keywords:
keyword_lower = keyword.lower()
if keyword_lower in combined_text:
score += 1
matched.append(keyword)
if score > 0:
score *= config["severity_multiplier"]
scores[category] = round(score, 2)
matched_keywords[category] = matched
if not scores:
return {
"primary_category": "Unknown",
"confidence": 0.0,
"all_scores": {},
"keywords_matched": [],
"secondary_categories": [],
}
sorted_scores = sorted(scores.items(), key=lambda x: -x[1])
primary_category = sorted_scores[0][0]
primary_score = sorted_scores[0][1]
total_score = sum(scores.values())
confidence = min(0.95, primary_score / max(total_score, 1))
secondary = [
{"category": cat, "score": score}
for cat, score in sorted_scores[1:3]
if score > 0
]
return {
"primary_category": primary_category,
"confidence": round(confidence, 2),
"all_scores": scores,
"keywords_matched": matched_keywords.get(primary_category, []),
"secondary_categories": secondary,
"description": ROOT_CAUSE_CATEGORIES.get(primary_category, {}).get(
"description", ""
),
"method": "keyword"
}
def classify(
self,
root_cause_text: str,
report_text: str = "",
context: Optional[Dict] = None,
) -> Dict[str, Any]:
"""
Classify a root cause text into categories using Transformer + Rule-based hybrid
"""
if not root_cause_text and not report_text:
return {
"primary_category": "Unknown",
"confidence": 0.0,
"all_scores": {},
"keywords_matched": [],
"secondary_categories": [],
}
# Check Cache
cache_key = f"{report_text}|{root_cause_text}"
if cache_key in self._classification_cache:
return self._classification_cache[cache_key].copy()
result = None
# 1. Try Transformer (High Accuracy)
try:
from data.nlp_service import get_nlp_service
nlp = get_nlp_service()
if nlp.models_loaded:
combined = f"{report_text} [SEP] {root_cause_text}"
rc_pred = nlp.classify_root_cause([combined])[0]
if rc_pred["label"] != "Unknown" and rc_pred["confidence"] > 0.4:
result = {
"primary_category": rc_pred["label"],
"confidence": rc_pred["confidence"],
"all_scores": {rc_pred["label"]: rc_pred["confidence"]},
"keywords_matched": [],
"secondary_categories": [],
"description": ROOT_CAUSE_CATEGORIES.get(rc_pred["label"], {}).get("description", ""),
"method": "transformer"
}
except Exception as e:
logger.warning(f"Transformer RC classification failed: {e}")
# 2. Try ML classifier if available
if result is None:
result = self._classify_ml(root_cause_text, report_text)
# 3. Fallback to Keyword Matching (Legacy)
if result is None:
result = self._classify_keyword(root_cause_text, report_text)
# Update Cache
self._classification_cache[cache_key] = result.copy()
return result
def classify_batch(
self, records: List[Dict], text_field: str = "Root_Caused"
) -> List[Dict[str, Any]]:
"""
Classify multiple records using optimized batch inference
"""
results = []
texts_for_transformer = []
valid_indices = []
for i, record in enumerate(records):
root_cause = record.get(text_field, "")
report = record.get("Report", "")
# 0. Check Cache First (O(1) vs heavy O(Transformer))
cache_key = f"{report}|{root_cause}"
if cache_key in self._classification_cache:
results.append({**self._classification_cache[cache_key], "row_id": record.get("_row_id")})
continue
# Prepare context for transformer
if root_cause or report:
combined = f"{report} [SEP] {root_cause}"
texts_for_transformer.append(combined)
valid_indices.append(i)
# Initialize results with placeholders
results.append({
"primary_category": "Unknown",
"confidence": 0.0,
"all_scores": {},
"keywords_matched": [],
"secondary_categories": [],
"row_id": record.get("_row_id")
})
# 1. Batch Transformer Inference (High Efficiency)
if texts_for_transformer:
try:
from data.nlp_service import get_nlp_service
nlp = get_nlp_service()
if nlp.models_loaded:
mt_results = nlp.classify_root_cause(texts_for_transformer)
for idx, mt_res in zip(valid_indices, mt_results):
if mt_res["label"] != "Unknown" and mt_res["confidence"] > 0.4:
results[idx].update({
"primary_category": mt_res["label"],
"confidence": mt_res["confidence"],
"all_scores": {mt_res["label"]: mt_res["confidence"]},
"description": ROOT_CAUSE_CATEGORIES.get(mt_res["label"], {}).get("description", ""),
"method": "transformer"
})
# Update cache
record = records[idx]
cache_key = f"{record.get('Report', '')}|{record.get(text_field, '')}"
self._classification_cache[cache_key] = results[idx].copy()
del self._classification_cache[cache_key]["row_id"] # Don't cache row_id
except Exception as e:
logger.warning(f"Batch transformer classification failed, falling back: {e}")
# 2. ML classification for remaining Unknowns
if self.classifier is not None and self.vectorizer is not None:
unknown_indices = [i for i, r in enumerate(results) if r["primary_category"] == "Unknown"]
if unknown_indices:
texts = []
for i in unknown_indices:
rc = records[i].get(text_field, "")
rep = records[i].get("Report", "")
texts.append(f"{rc} {rep}".strip())
try:
X = self.vectorizer.transform(texts)
if hasattr(self.classifier, "predict_proba"):
proba = self.classifier.predict_proba(X)
preds = np.argmax(proba, axis=1)
labels = self.classifier.classes_[preds]
confs = proba[np.arange(len(preds)), preds]
else:
labels = self.classifier.predict(X)
confs = np.full(len(labels), 0.7)
for j, i in enumerate(unknown_indices):
label = str(labels[j])
conf = float(confs[j])
if conf >= 0.4 and label:
results[i].update({
"primary_category": label,
"confidence": round(conf, 2),
"all_scores": {label: round(conf, 2)},
"description": ROOT_CAUSE_CATEGORIES.get(label, {}).get("description", ""),
"method": "tfidf-ml"
})
except Exception as e:
logger.warning(f"Batch ML classification failed: {e}")
# 3. Fallback to Keyword Matching for any remaining Unknowns
for i, record in enumerate(records):
if results[i]["primary_category"] == "Unknown":
root_cause = record.get(text_field, "")
report = record.get("Report", "")
if root_cause or report:
res = self._classify_keyword(root_cause, report)
if res["primary_category"] != "Unknown":
row_id = results[i]["row_id"]
results[i] = res
results[i]["row_id"] = row_id
return results
def train_from_data(self, records: List[Dict]) -> Dict[str, Any]:
"""
Train/improve classifier from labeled data using batch processing
"""
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.utils.class_weight import compute_class_weight
from sklearn.metrics import classification_report
logger.info(f"Training root cause classifier from {len(records)} records using batch labeling...")
# Bulk classify to get labels
classifications = self.classify_batch(records)
texts = []
labels = []
for record, classification in zip(records, classifications):
root_cause = record.get("Root_Caused", "")
report = record.get("Report", "")
combined = f"{root_cause} {report}".strip()
if not combined or len(combined) < 10:
continue
category = classification["primary_category"]
if category != "Unknown":
texts.append(combined)
labels.append(category)
if len(set(labels)) < 2:
logger.warning("Not enough variety in labels for training")
return {
"status": "insufficient_data",
"records_processed": len(records),
"usable_records": len(texts),
}
X_train, X_test, y_train, y_test = train_test_split(
texts, labels, test_size=0.2, random_state=42
)
vectorizer = TfidfVectorizer(
max_features=20000, ngram_range=(1, 2), sublinear_tf=True, lowercase=True, min_df=2
)
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)
classes = np.unique(y_train)
try:
class_weights = compute_class_weight(class_weight="balanced", classes=classes, y=y_train)
cw = {c: w for c, w in zip(classes, class_weights)}
except Exception:
cw = None
classifier = LogisticRegression(max_iter=2000)
if cw:
try:
classifier.set_params(class_weight=cw)
except Exception:
pass
classifier.fit(X_train_vec, y_train)
accuracy = classifier.score(X_test_vec, y_test)
logger.info(f"Root cause classifier trained with accuracy: {accuracy:.2%}")
try:
y_pred = classifier.predict(X_test_vec)
logger.info("Root cause classification report:\n" + classification_report(y_test, y_pred))
except Exception:
pass
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
model_dir = os.path.join(base_dir, "models", "root_cause")
os.makedirs(model_dir, exist_ok=True)
with open(os.path.join(model_dir, "classifier.pkl"), "wb") as f:
pickle.dump(classifier, f)
with open(os.path.join(model_dir, "vectorizer.pkl"), "wb") as f:
pickle.dump(vectorizer, f)
self.classifier = classifier
self.vectorizer = vectorizer
self._classification_cache = {} # Clear cache as models changed
category_dist = Counter(labels)
return {
"status": "success",
"records_processed": len(records),
"usable_records": len(texts),
"accuracy": round(float(accuracy), 3),
"category_distribution": dict(category_dist),
}
def get_statistics(self, records: List[Dict]) -> Dict[str, Any]:
"""
Get root cause statistics from records
Args:
records: List of records to analyze
Returns:
Dict with statistics by category
"""
stats = defaultdict(
lambda: {
"count": 0,
"categories": Counter(),
"areas": Counter(),
"airlines": Counter(),
}
)
total_classified = 0
total_unknown = 0
# Use batch classification for performance (O(N/batch_size) instead of O(N))
classifications = self.classify_batch(records)
for record, classification in zip(records, classifications):
category = classification["primary_category"]
if category != "Unknown":
total_classified += 1
stats[category]["count"] += 1
stats[category]["categories"][
record.get("Irregularity_Complain_Category", "Unknown")
] += 1
stats[category]["areas"][record.get("Area", "Unknown")] += 1
stats[category]["airlines"][record.get("Airlines", "Unknown")] += 1
else:
total_unknown += 1
result = {}
for category, data in stats.items():
result[category] = {
"count": data["count"],
"percentage": round(data["count"] / max(total_classified, 1) * 100, 1),
"top_issue_categories": dict(data["categories"].most_common(3)),
"top_areas": dict(data["areas"].most_common(3)),
"top_airlines": dict(data["airlines"].most_common(3)),
"description": ROOT_CAUSE_CATEGORIES.get(category, {}).get(
"description", ""
),
}
return {
"total_records": len(records),
"classified": total_classified,
"unknown": total_unknown,
"classification_rate": round(
total_classified / max(len(records), 1) * 100, 1
),
"by_category": result,
"top_categories": sorted(result.items(), key=lambda x: -x[1]["count"])[:5],
}
def get_categories(self) -> Dict[str, Dict]:
"""Get all available root cause categories"""
return {
cat: {
"name": cat,
"description": config["description"],
"keyword_count": len(config["keywords"]),
"severity_multiplier": config["severity_multiplier"],
}
for cat, config in ROOT_CAUSE_CATEGORIES.items()
}
_root_cause_service: Optional[RootCauseService] = None
def get_root_cause_service() -> RootCauseService:
"""Get singleton instance"""
global _root_cause_service
if _root_cause_service is None:
_root_cause_service = RootCauseService()
return _root_cause_service
|