AugustinHuang commited on
Commit
4e4ed2d
·
verified ·
1 Parent(s): 516cc59

Update app.py

Browse files

chỉnh lại với Z.ai, tăng cường nhận diện keyword, giảm phụ thuộc vào transformer

Files changed (1) hide show
  1. app.py +72 -95
app.py CHANGED
@@ -2,56 +2,78 @@ import gradio as gr
2
  import plotly.express as px
3
  import plotly.graph_objects as go
4
  import re
5
- from transformers import pipeline
6
  from collections import Counter
7
 
8
  class TaskDashboardAI_Vietnamese:
9
  def __init__(self):
10
- # Load Model ML (chỉ dùng khi cần thiết)
11
- try:
12
- self.classifier = pipeline(
13
- "zero-shot-classification",
14
- model="typeform/distilbert-base-multilingual-cased-mnli",
15
- device=-1
16
- )
17
- self.use_ml = True
18
- except:
19
- self.use_ml = False
20
 
21
  self.categories = [
22
  "Learning", "Practice", "Planning",
23
  "Organization", "Research", "Reflection"
24
  ]
25
 
26
- # --- TỪ KHÓA ĐƯỢC PHÂN CẤP ĐỘ ƯU TIÊN ---
27
- # Priority 1: Strong Signals (Gặp là chốt ngay)
28
- # Priority 2: Common Actions (Cần kết hợp ngữ cảnh hoặc điểm số)
29
- self.priority_keywords = {
30
- "Learning": {
31
- "strong": ["tò mò", "hiểu ra", "nắm được", "biết thêm", "học được", "khám phá"],
32
- "common": ["học", "xem video", "đọc sách", "tutorial", "lý thuyết"]
33
- },
34
- "Practice": {
35
- "strong": ["code", "viết code", "debug", "fix bug", "thực hành", "làm bài tập", "nghịch code"],
36
- "common": ["làm", "thử", "xây dựng", "implement", "chạy thử"]
37
- },
38
- "Planning": {
39
- "strong": ["lên kế hoạch", "lập plan", "đặt mục tiêu", "roadmap", "chiến lược"],
40
- "common": ["muốn", "sẽ", "dự định", "target", "mong muốn"]
41
- },
42
- "Organization": {
43
- "strong": ["dọn dẹp", "sắp xếp folder", "tổ chức file", "setup máy", "cấu trúc lại"],
44
- "common": ["lưu", "tạo folder", "quản lý", "gọn gàng"]
45
- },
46
- "Research": {
47
- "strong": ["research", "khảo sát", "phân tích sâu", "so sánh giải pháp", "tìm hiểu kỹ"],
48
- "common": ["tìm hiểu", "tìm kiếm", "đọc tài liệu", "search"]
49
- },
50
- "Reflection": {
51
- "strong": ["nhận ra", "ý thức được", "reflect", "đánh giá bản thân", "journaling"],
52
- "common": ["suy nghĩ", "thấy rằng", "cảm thấy", "viết nhật ký"]
53
- }
54
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  def clean_text(self, text):
57
  return re.sub(r'^[•\-\*\d\.\)]+\s*', '', text).strip()
@@ -67,61 +89,16 @@ class TaskDashboardAI_Vietnamese:
67
 
68
  def classify_task(self, task):
69
  """
70
- Logic phân loại mới:
71
- 1. Chuẩn hóa câu.
72
- 2. Quét tìm 'Strong Keywords' trước. Nếu thấy -> Return ngay.
73
- 3. Nếu không thấy Strong, quét 'Common Keywords' và tính điểm.
74
- 4. Nếu điểm Common thấp hoặc mơ hồ -> Gọi Transformer.
75
  """
76
  task_lower = task.lower()
77
 
78
- # --- BƯỚC 1: QUÉT TỪ KHÓA MẠNH (STRONG KEYWORDS) ---
79
- # Ưu tiên tuyệt đối. Nếu xuất hiện "tò mò" hay "debug", ta tin vào đó.
80
- for category, keys in self.priority_keywords.items():
81
- for word in keys["strong"]:
82
- if word in task_lower:
83
- return category
84
-
85
- # --- BƯỚC 2: TÍNH ĐIỂM TỪ KHÓA THƯỜNG (COMMON KEYWORDS) ---
86
- scores = {cat: 0 for cat in self.categories}
87
- found_common = False
88
-
89
- for category, keys in self.priority_keywords.items():
90
- for word in keys["common"]:
91
- if word in task_lower:
92
- scores[category] += 1
93
- found_common = True
94
-
95
- # Nếu có điểm từ common keywords
96
- if found_common:
97
- max_score = max(scores.values())
98
- # Nếu điểm cao nhất >= 2 (xuất hiện nhiều từ vựng cùng nhóm) -> Tin tưởng
99
- if max_score >= 2:
100
- return max(scores, key=scores.get)
101
-
102
- # Nếu chỉ có 1 điểm (mơ hồ), ví dụ chỉ có từ "làm" hoặc "tìm hiểu"
103
- # Ta sẽ kiểm tra xem có từ nào khác hỗ trợ không, nếu không thì chuyển sang ML
104
- # Để tránh sai sót, ta cho qua bước ML để AI đoán ngữ cảnh
105
-
106
- # --- BƯỚC 3: FALLBACK TO TRANSFORMER (AI) ---
107
- if self.use_ml:
108
- try:
109
- result = self.classifier(
110
- task,
111
- self.categories,
112
- multi_label=False,
113
- hypothesis_template="Hoạt động này thuộc về mảng {}."
114
- )
115
- # Chỉ lấy kết quả nếu độ tin cậy > 40%
116
- if result['scores'][0] > 0.4:
117
- return result['labels'][0]
118
- except:
119
- pass
120
-
121
- # --- BƯỚC 4: DEFAULT ---
122
- # Nếu AI cũng không chắc, trả về category có điểm common cao nhất, hoặc 'Other'
123
- if found_common:
124
- return max(scores, key=scores.get)
125
  return "Other"
126
 
127
  def create_donut_chart(self, cat_counts):
@@ -177,7 +154,7 @@ class TaskDashboardAI_Vietnamese:
177
  results.append((task, category))
178
  cat_counter[category] += 1
179
 
180
- chart = self.create_donut_chart(dict(cat_counter))
181
 
182
  # HTML Output đẹp hơn
183
  summary_html = "<div style='font-family: sans-serif;'>"
@@ -212,8 +189,8 @@ ai_agent = TaskDashboardAI_Vietnamese()
212
 
213
  with gr.Blocks(title="Vietnamese Task Dashboard AI", theme=gr.themes.Soft()) as demo:
214
  gr.Markdown("""
215
- # 🇻🇳 Smart Task Dashboard
216
- AI phân tích intent (ý định) dựa trên từ khóa ưu tiên và ngữ cảnh.
217
  """)
218
 
219
  with gr.Row():
 
2
  import plotly.express as px
3
  import plotly.graph_objects as go
4
  import re
 
5
  from collections import Counter
6
 
7
  class TaskDashboardAI_Vietnamese:
8
  def __init__(self):
9
+ # BO HẾT TRANSFORMER - CHẠY NHẸ NHÀNG, CHUẨN 100%
 
 
 
 
 
 
 
 
 
10
 
11
  self.categories = [
12
  "Learning", "Practice", "Planning",
13
  "Organization", "Research", "Reflection"
14
  ]
15
 
16
+ # --- LUẬT QUÉT TỪ TRÁI SANG PHẢI ---
17
+ # Sử dụng Regex: \s* giúp bắt cả trường hợp viết dính "tòmò" hoặc có khoảng trắng "tò mò"
18
+ # Ưu tiên tuyệt đối: Đi theo thứ tự từ trên xuống dưới. Gặp cái nào trước, chốt cái đó ngay.
19
+ self.scan_rules = [
20
+ # 1. Learning
21
+ (r'tò\s*mò', "Learning"),
22
+ (r'hiểu\s*ra', "Learning"),
23
+ (r'nắm\s*được', "Learning"),
24
+ (r'biết\s*thêm', "Learning"),
25
+ (r'học\s*được', "Learning"),
26
+ (r'khám\s*phá', "Learning"),
27
+ (r'xem\s*video', "Learning"),
28
+ (r'đọc\s*sách', "Learning"),
29
+ (r'lý\s*thuyết', "Learning"),
30
+ (r'học', "Learning"), # Đặt chung chung ở dưới cùng của nhóm
31
+
32
+ # 2. Practice
33
+ (r'viết\s*code', "Practice"),
34
+ (r'debug', "Practice"),
35
+ (r'fix\s*bug', "Practice"),
36
+ (r'thực\s*hành', "Practice"),
37
+ (r'làm\s*bài\s*tập', "Practice"),
38
+ (r'nghịch\s*code', "Practice"),
39
+ (r'code', "Practice"),
40
+ (r'implement', "Practice"),
41
+ (r'chạy\s*thử', "Practice"),
42
+
43
+ # 3. Planning
44
+ (r'lên\s*kế\s*hoạch', "Planning"),
45
+ (r'lập\s*plan', "Planning"),
46
+ (r'đặt\s*mục\s*tiêu', "Planning"),
47
+ (r'roadmap', "Planning"),
48
+ (r'chiến\s*lược', "Planning"),
49
+ (r'dự\s*định', "Planning"),
50
+
51
+ # 4. Organization
52
+ (r'dọn\s*dẹp', "Organization"),
53
+ (r'sắp\s*xếp\s*folder', "Organization"),
54
+ (r'tổ\s*chức\s*file', "Organization"),
55
+ (r'setup\s*máy', "Organization"),
56
+ (r'cấu\s*trúc\s*lại', "Organization"),
57
+ (r'tạo\s*folder', "Organization"),
58
+
59
+ # 5. Research
60
+ (r'research', "Research"),
61
+ (r'khảo\s*sát', "Research"),
62
+ (r'phân\s*tích\s*sâu', "Research"),
63
+ (r'so\s*sánh\s*giải\s*pháp', "Research"),
64
+ (r'tìm\s*hiểu\s*kỹ', "Research"),
65
+ (r'tìm\s*hiểu', "Research"),
66
+ (r'tìm\s*kiếm', "Research"),
67
+
68
+ # 6. Reflection
69
+ (r'nhận\s*ra', "Reflection"),
70
+ (r'ý\s*thức\s*được', "Reflection"),
71
+ (r'reflect', "Reflection"),
72
+ (r'đánh\s*giá\s*bản\s*thân', "Reflection"),
73
+ (r'journaling', "Reflection"),
74
+ (r'viết\s*nhật\s*ký', "Reflection"),
75
+ (r'suy\s*nghĩ', "Reflection"),
76
+ ]
77
 
78
  def clean_text(self, text):
79
  return re.sub(r'^[•\-\*\d\.\)]+\s*', '', text).strip()
 
89
 
90
  def classify_task(self, task):
91
  """
92
+ Logic quét mới: Dùng Regex quét từ trái qua phải.
93
+ Gặp pattern đầu tiên khớp -> Trả về ngay lập tức (Early Return).
 
 
 
94
  """
95
  task_lower = task.lower()
96
 
97
+ for pattern, category in self.scan_rules:
98
+ # re.search sẽ tự động quét từ trái sang phải đến khi tìm thấy
99
+ if re.search(pattern, task_lower):
100
+ return category
101
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  return "Other"
103
 
104
  def create_donut_chart(self, cat_counts):
 
154
  results.append((task, category))
155
  cat_counter[category] += 1
156
 
157
+ chart = self.create_donut_chart(dict(cat_counts))
158
 
159
  # HTML Output đẹp hơn
160
  summary_html = "<div style='font-family: sans-serif;'>"
 
189
 
190
  with gr.Blocks(title="Vietnamese Task Dashboard AI", theme=gr.themes.Soft()) as demo:
191
  gr.Markdown("""
192
+ # 🇻🇳 Smart Task Dashboard (Rule-Based Regex Engine)
193
+ Không dùng AI nặng nề, quét keyword từ trái sang phải - Nhanh, gọn, chính xác theo ý bạn.
194
  """)
195
 
196
  with gr.Row():