Spaces:
Sleeping
Sleeping
Create file_loader.py
Browse files- file_loader.py +33 -0
file_loader.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from tqdm import tqdm
|
3 |
+
from langchain_community.vectorstores import FAISS
|
4 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
5 |
+
|
6 |
+
# Import từ helpers
|
7 |
+
from helpers import (
|
8 |
+
list_docx_files, # Lấy danh sách file .docx
|
9 |
+
get_splits, # Xử lý file docx thành splits
|
10 |
+
get_json_splits_only, # Xử lý file JSON (FAQ)
|
11 |
+
get_urls_splits, # Xử lý dữ liệu từ web
|
12 |
+
)
|
13 |
+
def get_splits_and_vectorstore()
|
14 |
+
### Xử lý tất cả các tài liệu và nhét vào database
|
15 |
+
folder_path = "syllabus_nct_word_format/"
|
16 |
+
docx_files = list_docx_files(folder_path)
|
17 |
+
|
18 |
+
all_splits = [] # Khởi tạo danh sách lưu kết quả
|
19 |
+
for i, file_path in enumerate(tqdm(docx_files, desc="Đang xử lý", unit="file")):
|
20 |
+
output_json_path = f"output_{i}.json"
|
21 |
+
splits = get_splits(file_path, output_json_path)
|
22 |
+
all_splits += splits
|
23 |
+
|
24 |
+
# Xử lý FAQ
|
25 |
+
FAQ_path = "syllabus_nct_word_format/FAQ.json"
|
26 |
+
FAQ_splits = get_json_splits_only(FAQ_path)
|
27 |
+
all_splits += FAQ_splits
|
28 |
+
|
29 |
+
# Lưu vào vectorstore với nhúng từ Google GenAI
|
30 |
+
embedding = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004")
|
31 |
+
vectorstore = FAISS.from_documents(documents=all_splits, embedding=embedding)
|
32 |
+
|
33 |
+
return all_splits, vectorstore
|