Spaces:
Sleeping
Sleeping
Update app.py
Browse fileschỉnh lại với Z.ai, tăng cường nhận diện keyword, giảm phụ thuộc vào transformer
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 |
-
#
|
| 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 |
-
# ---
|
| 27 |
-
#
|
| 28 |
-
#
|
| 29 |
-
self.
|
| 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 |
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
|
| 71 |
-
|
| 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 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 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(
|
| 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
|
| 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():
|