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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import re
|
| 5 |
+
from collections import Counter
|
| 6 |
+
|
| 7 |
+
# ── CONFIG ───────────────────────────────────────────────────────────────────
|
| 8 |
+
|
| 9 |
+
KB_PATH = "knowledge_base_v4.json"
|
| 10 |
+
|
| 11 |
+
# Load knowledge base
|
| 12 |
+
with open(KB_PATH, "r", encoding="utf-8") as f:
|
| 13 |
+
KB = json.load(f)
|
| 14 |
+
|
| 15 |
+
PATTERNS = KB.get("by_pattern", {})
|
| 16 |
+
FAILURE_KEYS = KB.get("by_failure_key", {})
|
| 17 |
+
LOOKUP = KB.get("lookup", {})
|
| 18 |
+
|
| 19 |
+
# ── FUZZY SEARCH ───────────────────────────────────────────────────────────────
|
| 20 |
+
|
| 21 |
+
def find_best_pattern(query):
|
| 22 |
+
"""Tìm pattern phù hợp nhất với query của user"""
|
| 23 |
+
query = query.upper()
|
| 24 |
+
|
| 25 |
+
# 1. Exact match
|
| 26 |
+
for pattern in PATTERNS:
|
| 27 |
+
if pattern.upper() == query:
|
| 28 |
+
return pattern, 1.0
|
| 29 |
+
|
| 30 |
+
# 2. Substring match
|
| 31 |
+
for pattern in PATTERNS:
|
| 32 |
+
if pattern.upper() in query or query in pattern.upper():
|
| 33 |
+
return pattern, 0.9
|
| 34 |
+
|
| 35 |
+
# 3. Keyword matching
|
| 36 |
+
query_words = set(re.findall(r'\b[A-Z]{2,}\b', query))
|
| 37 |
+
best_match = None
|
| 38 |
+
best_score = 0
|
| 39 |
+
|
| 40 |
+
for pattern in PATTERNS:
|
| 41 |
+
pattern_words = set(re.findall(r'\b[A-Z]{2,}\b', pattern.upper()))
|
| 42 |
+
if not pattern_words:
|
| 43 |
+
continue
|
| 44 |
+
common = query_words & pattern_words
|
| 45 |
+
score = len(common) / max(len(query_words), len(pattern_words))
|
| 46 |
+
if score > best_score:
|
| 47 |
+
best_score = score
|
| 48 |
+
best_match = pattern
|
| 49 |
+
|
| 50 |
+
if best_score >= 0.3:
|
| 51 |
+
return best_match, best_score
|
| 52 |
+
|
| 53 |
+
# 4. Failure key matching
|
| 54 |
+
for fk in FAILURE_KEYS:
|
| 55 |
+
if fk.upper() in query:
|
| 56 |
+
return fk, 0.5 # Return failure key for broad matching
|
| 57 |
+
|
| 58 |
+
return None, 0
|
| 59 |
+
|
| 60 |
+
# ── RESPONSE BUILDER ─────────────────────────────────────────────────────────
|
| 61 |
+
|
| 62 |
+
def build_response(query):
|
| 63 |
+
"""Xây dựng phản hồi từ knowledge base"""
|
| 64 |
+
|
| 65 |
+
pattern, score = find_best_pattern(query)
|
| 66 |
+
|
| 67 |
+
if not pattern:
|
| 68 |
+
return "Không tìm thấy thông tin phù hợp. Vui lòng mô tả lỗi chi tiết hơn (ví dụ: 'lỗi channel 53', 'TRIP PE TEM', 'DDR failed')."
|
| 69 |
+
|
| 70 |
+
# Get entry
|
| 71 |
+
if pattern in PATTERNS:
|
| 72 |
+
entry = PATTERNS[pattern]
|
| 73 |
+
elif pattern in FAILURE_KEYS:
|
| 74 |
+
entry = FAILURE_KEYS[pattern]
|
| 75 |
+
else:
|
| 76 |
+
return "Lỗi: Không tìm thấy dữ liệu."
|
| 77 |
+
|
| 78 |
+
# Build response
|
| 79 |
+
lines = []
|
| 80 |
+
lines.append(f"## Phân tích lỗi: {pattern}")
|
| 81 |
+
lines.append(f"")
|
| 82 |
+
lines.append(f"- **Tổng cases:** {entry['total_cases']}")
|
| 83 |
+
lines.append(f"- **Pass rate:** {entry['pass_rate']}%")
|
| 84 |
+
lines.append(f"- **Pass:** {entry['pass_count']} | **Fail:** {entry['fail_count']}")
|
| 85 |
+
lines.append(f"- **Board types:** {', '.join(entry.get('board_types', []))}")
|
| 86 |
+
lines.append(f"")
|
| 87 |
+
|
| 88 |
+
# BKM Procedure
|
| 89 |
+
bkm_procs = entry.get('bkm_procedures', [])
|
| 90 |
+
if bkm_procs:
|
| 91 |
+
lines.append(f"## Quy trình BKM")
|
| 92 |
+
for proc in bkm_procs[:3]:
|
| 93 |
+
lines.append(f"- {proc}")
|
| 94 |
+
lines.append(f"")
|
| 95 |
+
|
| 96 |
+
# BKM Components
|
| 97 |
+
bkm_comps = entry.get('bkm_components', [])
|
| 98 |
+
if bkm_comps:
|
| 99 |
+
lines.append(f"## Linh kiện theo BKM")
|
| 100 |
+
lines.append(f"- {', '.join(bkm_comps[:10])}")
|
| 101 |
+
lines.append(f"")
|
| 102 |
+
|
| 103 |
+
# Priority Replace
|
| 104 |
+
priority = entry.get('priority_replace', [])
|
| 105 |
+
if priority:
|
| 106 |
+
lines.append(f"## Thống kê - Linh kiện thay nhiều nhất")
|
| 107 |
+
lines.append(f"| Rank | Linh kiện | Pass Rate | Số lần thay | Pass | Fail |")
|
| 108 |
+
lines.append(f"|------|-----------|-----------|-------------|------|------|")
|
| 109 |
+
for i, p in enumerate(priority[:10], 1):
|
| 110 |
+
lines.append(f"| {i} | {p['component']} | {p['pass_rate']}% | {p['count']} | {p['pass']} | {p['fail']} |")
|
| 111 |
+
lines.append(f"")
|
| 112 |
+
|
| 113 |
+
# Best Actions
|
| 114 |
+
best_actions = entry.get('best_actions', [])
|
| 115 |
+
if best_actions:
|
| 116 |
+
lines.append(f"## Hành động hiệu quả nhất")
|
| 117 |
+
lines.append(f"| Rank | Hành động | Linh kiện | Pass Rate | Số lần | Pass | Fail |")
|
| 118 |
+
lines.append(f"|------|-----------|-----------|-----------|--------|------|------|")
|
| 119 |
+
for i, a in enumerate(best_actions[:10], 1):
|
| 120 |
+
lines.append(f"| {i} | {a['action']} | {a['component']} | {a['pass_rate']}% | {a['count']} | {a['pass']} | {a['fail']} |")
|
| 121 |
+
lines.append(f"")
|
| 122 |
+
|
| 123 |
+
# Top Technicians
|
| 124 |
+
techs = entry.get('top_technicians', [])
|
| 125 |
+
if techs:
|
| 126 |
+
lines.append(f"## Kỹ thuật viên tốt nhất")
|
| 127 |
+
lines.append(f"| Rank | Tên | Pass Rate | Tổng cases | Pass |")
|
| 128 |
+
lines.append(f"|------|-----|-----------|------------|------|")
|
| 129 |
+
for i, t in enumerate(techs[:5], 1):
|
| 130 |
+
lines.append(f"| {i} | {t['name']} | {t['pass_rate']}% | {t['total']} | {t['pass']} |")
|
| 131 |
+
lines.append(f"")
|
| 132 |
+
|
| 133 |
+
# Sample descriptions
|
| 134 |
+
samples = entry.get('sample_original_descriptions', [])
|
| 135 |
+
if samples:
|
| 136 |
+
lines.append(f"## Ví dụ mô tả lỗi tương tự")
|
| 137 |
+
for s in samples:
|
| 138 |
+
lines.append(f"- {s}")
|
| 139 |
+
lines.append(f"")
|
| 140 |
+
|
| 141 |
+
lines.append(f"---")
|
| 142 |
+
lines.append(f"*Độ chính xác tìm kiếm: {int(score*100)}%*")
|
| 143 |
+
|
| 144 |
+
return "\n".join(lines)
|
| 145 |
+
|
| 146 |
+
# ── GRADIO UI ────────────────────────────────────────────────────────────────
|
| 147 |
+
|
| 148 |
+
def chat(message, history):
|
| 149 |
+
"""Chat function for Gradio"""
|
| 150 |
+
response = build_response(message)
|
| 151 |
+
return response
|
| 152 |
+
|
| 153 |
+
# Create interface
|
| 154 |
+
demo = gr.ChatInterface(
|
| 155 |
+
fn=chat,
|
| 156 |
+
title="HDMT Debug Assistant v4",
|
| 157 |
+
description="Chatbot hỗ trợ debug HDMT boards. Mô tả lỗi để nhận phân tích và hướng dẫn.",
|
| 158 |
+
examples=[
|
| 159 |
+
"lỗi channel 53",
|
| 160 |
+
"TRIP PE TEM",
|
| 161 |
+
"DDR failed",
|
| 162 |
+
"blue screen",
|
| 163 |
+
"ADATE error",
|
| 164 |
+
"FPGA calibration",
|
| 165 |
+
"MISSING J18",
|
| 166 |
+
],
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
if __name__ == "__main__":
|
| 170 |
+
demo.launch()
|