File size: 3,531 Bytes
f383878
 
 
07b255e
 
f383878
 
 
07b255e
f383878
 
 
cc5bd3b
 
 
 
f62a847
f383878
 
5a05052
 
f383878
da2813f
 
 
f383878
 
cc5bd3b
 
f383878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07b255e
 
 
 
 
 
 
 
 
 
 
 
f383878
07b255e
 
4cf1a7c
f383878
 
 
 
 
 
 
da2813f
f383878
 
 
 
 
 
 
 
4cf1a7c
f383878
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader
from langchain_community.vectorstores import FAISS
#from langchain_community.embeddings import GPT4AllEmbeddings
from langchain_community.embeddings import HuggingFaceEmbeddings

from huggingface_hub import hf_hub_download


# from llama_cpp import Llama
import os

cache_path = "/home/user/app/hf_cache"
os.makedirs(cache_path, exist_ok=True)

#os.environ["HF_HOME"] = "/home/user/app/hf_cache"


# Khai bao bien 
pdf_data_path = "/home/user/app/data"
vector_dp_path = "/home/user/app/vectorstores/db_faiss"

os.makedirs(pdf_data_path, exist_ok=True)
os.makedirs(vector_dp_path, exist_ok=True)

model_file = hf_hub_download(
    repo_id="Pudding48/TinyLlamaTest",  # 🟢 This must be a model repo, not a Space
    filename="tinyllama-1.1b-chat-v1.0.Q8_0.gguf",
    cache_dir=cache_path
)

# Ham 1. Tao ra vector DB tu 1 doan text
def create_db_from_text():

    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."

    text_splitter = CharacterTextSplitter(
        separator="\n",
        chunk_size=512,
        chunk_overlap=50,
        length_function=len 
    )

    chunks = text_splitter.split_text(raw_text)

    # Embeding
    '''
    🔥 The gpt4all embedding library you’re using was compiled against GLIBC 2.32 or higher,
    but the Hugging Face Docker environment only provides GLIBC 2.31 or lower.
    
    So your Space crashes because it tries to load a C-based .so library that depends on a newer system-level runtime.
    
    🧠 What is GLIBC?
    GLIBC is the GNU C standard library — it’s a low-level part of Linux.
    Most .so libraries (like libllmodel.so) built from C++ depend on a minimum GLIBC version.
    
    You cannot change GLIBC in Hugging Face Docker — so if your library requires GLIBC 2.32+, it will not run.

    embedding_model = GPT4AllEmbeddings(model_file= model_file)
    '''
    
    embedding_model = HuggingFaceEmbeddings(model_name = "sentence-transformers/all-MiniLM-L6-v2")
    
    # Dua vao Faiss Vector DB
    db = FAISS.from_texts(texts=chunks, embedding=embedding_model)
    db.save_local(vector_dp_path)
    return db

def create_dp_from_files():
    
    
    # Khai bao loader de quet toan bo thu muc data
    loader = DirectoryLoader(pdf_data_path, glob="*.pdf",loader_cls=PyPDFLoader)
    documents = loader.load()

    text_splitter = CharacterTextSplitter(chunk_size = 512, chunk_overlap = 50)
    chunks = text_splitter.split_documents(documents)

    embedding_model = HuggingFaceEmbeddings(model_name = "sentence-transformers/all-MiniLM-L6-v2")
    dp = FAISS.from_documents(chunks, embedding_model)
    dp.save_local(vector_dp_path)
    return dp

# create_db_from_text()
# create_dp_from_files()