fix cache
Browse files- Dockerfile +0 -2
- rag_core/chunker.py +76 -12
Dockerfile
CHANGED
|
@@ -13,8 +13,6 @@ RUN apt-get update && apt-get install -y \
|
|
| 13 |
|
| 14 |
# ✅ Tạo thư mục FAISS index và gán quyền đầy đủ
|
| 15 |
RUN mkdir -p faiss_index && chmod -R 777 faiss_index
|
| 16 |
-
# Tạo thư mục FAISS index cache
|
| 17 |
-
RUN mkdir -p /data && chmod -R 777 /data
|
| 18 |
|
| 19 |
# Cài đặt dependencies
|
| 20 |
RUN pip install --upgrade pip
|
|
|
|
| 13 |
|
| 14 |
# ✅ Tạo thư mục FAISS index và gán quyền đầy đủ
|
| 15 |
RUN mkdir -p faiss_index && chmod -R 777 faiss_index
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Cài đặt dependencies
|
| 18 |
RUN pip install --upgrade pip
|
rag_core/chunker.py
CHANGED
|
@@ -1,22 +1,86 @@
|
|
| 1 |
import re
|
|
|
|
|
|
|
|
|
|
| 2 |
from typing import List
|
| 3 |
from rag_core.utils import log_timed
|
| 4 |
-
import logging
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
def chunk_legal_text(text: str) -> List[str]:
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
logging.info(f"Tổng số chunk sau khi xử lý: {len(
|
| 19 |
-
for i, c in enumerate(
|
| 20 |
logging.info(f"Mẫu chunk {i+1}:\n{c[:300]}...\n")
|
| 21 |
|
| 22 |
-
return
|
|
|
|
| 1 |
import re
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import logging
|
| 5 |
from typing import List
|
| 6 |
from rag_core.utils import log_timed
|
|
|
|
| 7 |
|
| 8 |
+
NESTED_LOG_PATH = "faiss_index/chunk_structure.json"
|
| 9 |
+
|
| 10 |
+
@log_timed("chunking văn bản luật nâng cao")
|
| 11 |
def chunk_legal_text(text: str) -> List[str]:
|
| 12 |
+
nested_chunks = []
|
| 13 |
+
current_chapter = ""
|
| 14 |
+
current_article = ""
|
| 15 |
+
current_clause = ""
|
| 16 |
+
current_points = []
|
| 17 |
+
|
| 18 |
+
for line in text.splitlines():
|
| 19 |
+
line = line.strip()
|
| 20 |
+
if re.match(r"^Chương\s+[IVXLC]+\s+", line):
|
| 21 |
+
current_chapter = line
|
| 22 |
+
elif re.match(r"^Điều\s+\d+\.", line):
|
| 23 |
+
if current_article:
|
| 24 |
+
nested_chunks.append({
|
| 25 |
+
"chương": current_chapter,
|
| 26 |
+
"điều": current_article,
|
| 27 |
+
"khoản": current_clause,
|
| 28 |
+
"điểm": current_points
|
| 29 |
+
})
|
| 30 |
+
current_article = line
|
| 31 |
+
current_clause = ""
|
| 32 |
+
current_points = []
|
| 33 |
+
elif re.match(r"^\d+\.\s", line):
|
| 34 |
+
if current_clause:
|
| 35 |
+
nested_chunks.append({
|
| 36 |
+
"chương": current_chapter,
|
| 37 |
+
"điều": current_article,
|
| 38 |
+
"khoản": current_clause,
|
| 39 |
+
"điểm": current_points
|
| 40 |
+
})
|
| 41 |
+
current_clause = line
|
| 42 |
+
current_points = []
|
| 43 |
+
elif re.match(r"^[a-zA-Z]\)|^[a-zA-Z]\.", line):
|
| 44 |
+
current_points.append(line)
|
| 45 |
+
else:
|
| 46 |
+
if current_points:
|
| 47 |
+
current_points[-1] += " " + line
|
| 48 |
+
elif current_clause:
|
| 49 |
+
current_clause += " " + line
|
| 50 |
+
elif current_article:
|
| 51 |
+
current_article += " " + line
|
| 52 |
+
|
| 53 |
+
if current_article:
|
| 54 |
+
nested_chunks.append({
|
| 55 |
+
"chương": current_chapter,
|
| 56 |
+
"điều": current_article,
|
| 57 |
+
"khoản": current_clause,
|
| 58 |
+
"điểm": current_points
|
| 59 |
+
})
|
| 60 |
+
|
| 61 |
+
# Ghi ra file JSON để debug / kiểm tra
|
| 62 |
+
os.makedirs(os.path.dirname(NESTED_LOG_PATH), exist_ok=True)
|
| 63 |
+
with open(NESTED_LOG_PATH, "w", encoding="utf-8") as f:
|
| 64 |
+
json.dump(nested_chunks, f, ensure_ascii=False, indent=2)
|
| 65 |
|
| 66 |
+
logging.info(f"✅ Đã ghi cấu trúc nested vào {NESTED_LOG_PATH}")
|
| 67 |
+
logging.info(f"📎 Tải file tại: https://<YOUR_SPACE>.hf.space/file/{NESTED_LOG_PATH}")
|
| 68 |
|
| 69 |
+
# Chuyển về dạng list[str] để dùng với FAISS
|
| 70 |
+
flat_chunks = []
|
| 71 |
+
for item in nested_chunks:
|
| 72 |
+
parts = [
|
| 73 |
+
item.get("chương"),
|
| 74 |
+
item.get("điều"),
|
| 75 |
+
item.get("khoản"),
|
| 76 |
+
*item.get("điểm", [])
|
| 77 |
+
]
|
| 78 |
+
flat_text = "\n".join(filter(None, parts)).strip()
|
| 79 |
+
if len(flat_text) > 30:
|
| 80 |
+
flat_chunks.append(flat_text)
|
| 81 |
|
| 82 |
+
logging.info(f"Tổng số chunk sau khi xử lý: {len(flat_chunks)}")
|
| 83 |
+
for i, c in enumerate(flat_chunks[:2]):
|
| 84 |
logging.info(f"Mẫu chunk {i+1}:\n{c[:300]}...\n")
|
| 85 |
|
| 86 |
+
return flat_chunks
|