Pudding48 commited on
Commit
ddd1495
·
verified ·
1 Parent(s): 4772d70

Update prepare_vector_dp.py

Browse files
Files changed (1) hide show
  1. prepare_vector_dp.py +59 -52
prepare_vector_dp.py CHANGED
@@ -1,53 +1,60 @@
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 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
+
10
+ model_file = Llama.from_pretrained(
11
+ repo_id="Pudding48/TinyLLamaTest",
12
+ filename="tinyllama-1.1b-chat-v1.0.Q8_0.gguf",
13
+ )
14
+
15
+ # Khai bao bien
16
+ pdf_data_path = "data"
17
+ vector_dp_path = "vectorstores/db_faiss"
18
+
19
+ # model_file = hf_hub_download(
20
+ # repo_id="Pudding48/TinyLlamaTest", # Replace with your model repo
21
+ # filename="tinyllama-1.1b-chat-v1.0.Q8_0.gguf",
22
+ # cache_dir="model" # Will be created in the Space's environment
23
+ # )
24
+
25
+ # Ham 1. Tao ra vector DB tu 1 doan text
26
+ def create_db_from_text():
27
+ 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."
28
+
29
+ text_splitter = CharacterTextSplitter(
30
+ separator="\n",
31
+ chunk_size=512,
32
+ chunk_overlap=50,
33
+ length_function=len
34
+ )
35
+
36
+ chunks = text_splitter.split_text(raw_text)
37
+
38
+ # Embeding
39
+ embedding_model = GPT4AllEmbeddings(model_file= model_file)
40
+
41
+ # Dua vao Faiss Vector DB
42
+ db = FAISS.from_texts(texts=chunks, embedding=embedding_model)
43
+ db.save_local(vector_dp_path)
44
+ return db
45
+
46
+ def create_dp_from_files():
47
+ # Khai bao loader de quet toan bo thu muc data
48
+ loader = DirectoryLoader(pdf_data_path, glob="*.pdf",loader_cls=PyPDFLoader)
49
+ documents = loader.load()
50
+
51
+ text_splitter = CharacterTextSplitter(chunk_size = 512, chunk_overlap = 50)
52
+ chunks = text_splitter.split_documents(documents)
53
+
54
+ embedding_model = GPT4AllEmbeddings(model_file = model_file)
55
+ dp = FAISS.from_documents(chunks, embedding_model)
56
+ dp.save_local(vector_dp_path)
57
+ return dp
58
+
59
+ # create_db_from_text()
60
  create_dp_from_files()