Pudding48 commited on
Commit
f383878
·
verified ·
1 Parent(s): fd4565a

Update prepare_vector_dp.py

Browse files
Files changed (1) hide show
  1. prepare_vector_dp.py +67 -53
prepare_vector_dp.py CHANGED
@@ -1,53 +1,67 @@
1
- from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
2
- from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader
3
- from langchain_community.vectorstores import FAISS
4
- from langchain_community.embeddings import GPT4AllEmbeddings
5
-
6
- from huggingface_hub import hf_hub_download
7
-
8
- # Khai bao bien
9
- pdf_data_path = "data"
10
- vector_dp_path = "vectorstores/db_faiss"
11
-
12
- model_file = hf_hub_download(
13
- repo_id="Pudding48/TinyLlamaTest", # Replace with your model repo
14
- filename="tinyllama-1.1b-chat-v1.0.Q8_0.gguf",
15
- cache_dir="model" # Will be created in the Space's environment
16
- )
17
-
18
- # Ham 1. Tao ra vector DB tu 1 doan text
19
- def create_db_from_text():
20
- raw_text = "Trường Đại học Khoa học – Đại học Huế là một trong những cơ sở đào tạo và nghiên cứu hàng đầu tại khu vực miền Trung và Tây Nguyên. Được thành lập từ năm 1957, trường có bề dày truyền thống trong giảng dạy các ngành khoa học tự nhiên, xã hội và nhân văn. Với đội ngũ giảng viên giàu kinh nghiệm, cơ sở vật chất hiện đại và môi trường học tập năng động, Trường Đại học Khoa học luôn là lựa chọn uy tín của sinh viên trong và ngoài nước. Trường hiện tọa lạc tại số 77 Nguyễn Huệ, thành phố Huế – trung tâm văn hóa, giáo dục lớn của cả nước."
21
-
22
- text_splitter = CharacterTextSplitter(
23
- separator="\n",
24
- chunk_size=512,
25
- chunk_overlap=50,
26
- length_function=len
27
- )
28
-
29
- chunks = text_splitter.split_text(raw_text)
30
-
31
- # Embeding
32
- embedding_model = GPT4AllEmbeddings(model_file= model_file)
33
-
34
- # Dua vao Faiss Vector DB
35
- db = FAISS.from_texts(texts=chunks, embedding=embedding_model)
36
- db.save_local(vector_dp_path)
37
- return db
38
-
39
- def create_dp_from_files():
40
- # Khai bao loader de quet toan bo thu muc data
41
- loader = DirectoryLoader(pdf_data_path, glob="*.pdf",loader_cls=PyPDFLoader)
42
- documents = loader.load()
43
-
44
- text_splitter = CharacterTextSplitter(chunk_size = 512, chunk_overlap = 50)
45
- chunks = text_splitter.split_documents(documents)
46
-
47
- embedding_model = GPT4AllEmbeddings(model_file = model_file)
48
- dp = FAISS.from_documents(chunks, embedding_model)
49
- dp.save_local(vector_dp_path)
50
- return dp
51
-
52
- # create_db_from_text()
53
- create_dp_from_files()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
2
+ from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader
3
+ from langchain_community.vectorstores import FAISS
4
+ from langchain_community.embeddings import GPT4AllEmbeddings
5
+
6
+ from huggingface_hub import hf_hub_download
7
+
8
+ # from llama_cpp import Llama
9
+ import os
10
+
11
+ # model_file = Llama.from_pretrained(
12
+ # repo_id="Pudding48/TinyLLamaTest",
13
+ # filename="tinyllama-1.1b-chat-v1.0.Q8_0.gguf",
14
+ # )
15
+
16
+ # Khai bao bien
17
+ pdf_data_path = "data"
18
+ vector_dp_path = "vectorstores/db_faiss"
19
+
20
+ model_file = hf_hub_download(
21
+ repo_id="Pudding48/TinyLlamaTest", # 🟢 This must be a model repo, not a Space
22
+ filename="tinyllama-1.1b-chat-v1.0.Q8_0.gguf",
23
+ cache_dir="model"
24
+ )
25
+
26
+ # Ham 1. Tao ra vector DB tu 1 doan text
27
+ def create_db_from_text():
28
+ os.makedirs(pdf_data_path, exist_ok=True)
29
+ os.makedirs(vector_dp_path, exist_ok=True)
30
+
31
+ raw_text = "Trường Đại học Khoa học – Đại học Huế là một trong những cơ sở đào tạo và nghiên cứu hàng đầu tại khu vực miền Trung và Tây Nguyên. Được thành lập từ năm 1957, trường có bề dày truyền thống trong giảng dạy các ngành khoa học tự nhiên, xã hội và nhân văn. Với đội ngũ giảng viên giàu kinh nghiệm, cơ sở vật chất hiện đại và môi trường học tập năng động, Trường Đại học Khoa học luôn là lựa chọn uy tín của sinh viên trong và ngoài nước. Trường hiện tọa lạc tại số 77 Nguyễn Huệ, thành phố Huế – trung tâm văn hóa, giáo dục lớn của cả nước."
32
+
33
+ text_splitter = CharacterTextSplitter(
34
+ separator="\n",
35
+ chunk_size=512,
36
+ chunk_overlap=50,
37
+ length_function=len
38
+ )
39
+
40
+ chunks = text_splitter.split_text(raw_text)
41
+
42
+ # Embeding
43
+ embedding_model = GPT4AllEmbeddings(model_file= model_file)
44
+
45
+ # Dua vao Faiss Vector DB
46
+ db = FAISS.from_texts(texts=chunks, embedding=embedding_model)
47
+ db.save_local(vector_dp_path)
48
+ return db
49
+
50
+ def create_dp_from_files():
51
+ os.makedirs(pdf_data_path, exist_ok=True)
52
+ os.makedirs(vector_dp_path, exist_ok=True)
53
+
54
+ # Khai bao loader de quet toan bo thu muc data
55
+ loader = DirectoryLoader(pdf_data_path, glob="*.pdf",loader_cls=PyPDFLoader)
56
+ documents = loader.load()
57
+
58
+ text_splitter = CharacterTextSplitter(chunk_size = 512, chunk_overlap = 50)
59
+ chunks = text_splitter.split_documents(documents)
60
+
61
+ embedding_model = GPT4AllEmbeddings(model_file = model_file)
62
+ dp = FAISS.from_documents(chunks, embedding_model)
63
+ dp.save_local(vector_dp_path)
64
+ return dp
65
+
66
+ # create_db_from_text()
67
+ # create_dp_from_files()