Spaces:
Sleeping
Sleeping
Stéphanie Kamgnia Wonkap
commited on
Commit
•
b99886d
1
Parent(s):
eb67361
fixing main
Browse files- app.py +67 -62
- src/embeddings.py +1 -1
app.py
CHANGED
@@ -10,7 +10,7 @@ from src.embeddings import init_embedding_model
|
|
10 |
|
11 |
from transformers import pipeline
|
12 |
from langchain_community.document_loaders import PyPDFLoader
|
13 |
-
from
|
14 |
from src.retriever import init_vectorDB_from_doc, retriever
|
15 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
16 |
from langchain_community.vectorstores import FAISS
|
@@ -36,77 +36,82 @@ READER_MODEL_NAME=cfg['READER_MODEL_NAME']
|
|
36 |
RERANKER_MODEL_NAME=cfg['RERANKER_MODEL_NAME']
|
37 |
VECTORDB_PATH=cfg['VECTORDB_PATH']
|
38 |
|
39 |
-
st.title("Une application RAG pour interroger le Collège de Pédiatrie 2024")
|
40 |
|
41 |
-
|
|
|
|
|
42 |
|
43 |
|
44 |
# Initialize the retriever and LLM
|
45 |
|
46 |
-
loader = PyPDFLoader(DATA_FILE_PATH)
|
47 |
-
#loader = PyPDFDirectoryLoader(DATA_FILE_PATH)
|
48 |
-
raw_document_base = loader.load()
|
49 |
-
MARKDOWN_SEPARATORS = [
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
docs_processed = split_documents(
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
)
|
65 |
-
embedding_model=init_embedding_model(EMBEDDING_MODEL_NAME)
|
66 |
|
67 |
-
if os.path.exists(VECTORDB_PATH):
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
else:
|
72 |
-
|
73 |
-
|
74 |
|
75 |
|
76 |
-
if st.button("Get Answer"):
|
77 |
# Get the answer and relevant documents
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
from transformers import pipeline
|
12 |
from langchain_community.document_loaders import PyPDFLoader
|
13 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
14 |
from src.retriever import init_vectorDB_from_doc, retriever
|
15 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
16 |
from langchain_community.vectorstores import FAISS
|
|
|
36 |
RERANKER_MODEL_NAME=cfg['RERANKER_MODEL_NAME']
|
37 |
VECTORDB_PATH=cfg['VECTORDB_PATH']
|
38 |
|
|
|
39 |
|
40 |
+
def main():
|
41 |
+
st.title("Un RAG pour interroger le Collège de Pédiatrie 2024")
|
42 |
+
user_query = st.text_input("Entrez votre question:")
|
43 |
|
44 |
|
45 |
# Initialize the retriever and LLM
|
46 |
|
47 |
+
loader = PyPDFLoader(DATA_FILE_PATH)
|
48 |
+
#loader = PyPDFDirectoryLoader(DATA_FILE_PATH)
|
49 |
+
raw_document_base = loader.load()
|
50 |
+
MARKDOWN_SEPARATORS = [
|
51 |
+
"\n#{1,6} ",
|
52 |
+
"```\n",
|
53 |
+
"\n\\*\\*\\*+\n",
|
54 |
+
"\n---+\n",
|
55 |
+
"\n___+\n",
|
56 |
+
"\n\n",
|
57 |
+
"\n",
|
58 |
+
" ",
|
59 |
+
"",]
|
60 |
+
docs_processed = split_documents(
|
61 |
+
512, # We choose a chunk size adapted to our model
|
62 |
+
raw_document_base,
|
63 |
+
tokenizer_name=EMBEDDING_MODEL_NAME,
|
64 |
+
separator=MARKDOWN_SEPARATORS
|
65 |
+
)
|
66 |
+
embedding_model=init_embedding_model(EMBEDDING_MODEL_NAME)
|
67 |
|
68 |
+
if os.path.exists(VECTORDB_PATH):
|
69 |
+
new_vector_store = FAISS.load_local(
|
70 |
+
VECTORDB_PATH, embedding_model,
|
71 |
+
allow_dangerous_deserialization=True)
|
72 |
+
else:
|
73 |
+
KNOWLEDGE_VECTOR_DATABASE=init_vectorDB_from_doc(docs_processed, embedding_model)
|
74 |
+
KNOWLEDGE_VECTOR_DATABASE.save_local(VECTORDB_PATH)
|
75 |
|
76 |
|
77 |
+
if st.button("Get Answer"):
|
78 |
# Get the answer and relevant documents
|
79 |
+
bnb_config = BitsAndBytesConfig(
|
80 |
+
load_in_4bit=True,
|
81 |
+
bnb_4bit_use_double_quant=True,
|
82 |
+
bnb_4bit_quant_type="nf4",
|
83 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
84 |
+
)
|
85 |
+
model = AutoModelForCausalLM.from_pretrained(READER_MODEL_NAME, quantization_config=bnb_config)
|
86 |
+
tokenizer = AutoTokenizer.from_pretrained(READER_MODEL_NAME)
|
87 |
|
88 |
+
READER_LLM = pipeline(
|
89 |
+
model=model,
|
90 |
+
tokenizer=tokenizer,
|
91 |
+
task="text-generation",
|
92 |
+
do_sample=True,
|
93 |
+
temperature=0.2,
|
94 |
+
repetition_penalty=1.1,
|
95 |
+
return_full_text=False,
|
96 |
+
max_new_tokens=500,
|
97 |
+
token = os.getenv("HF_TOKEN")
|
98 |
+
)
|
99 |
+
RERANKER = RAGPretrainedModel.from_pretrained(RERANKER_MODEL_NAME)
|
100 |
+
num_doc_before_rerank=15
|
101 |
+
num_final_releveant_docs=5
|
102 |
+
answer, relevant_docs = answer_with_rag(query=user_query, READER_MODEL_NAME=READER_MODEL_NAME,embedding_model=embedding_model,vectorDB=KNOWLEDGE_VECTOR_DATABASE,reranker=RERANKER, llm=READER_LLM,num_doc_before_rerank=num_doc_before_rerank,num_final_relevant_docs=num_final_releveant_docs,rerank=True)
|
103 |
+
#print(answer)
|
104 |
|
105 |
|
106 |
+
# Display the answer
|
107 |
+
st.write("### Answer:")
|
108 |
+
st.write(answer)
|
109 |
|
110 |
+
# Display the relevant documents
|
111 |
+
st.write("### Relevant Documents:")
|
112 |
+
for i, doc in enumerate(relevant_docs):
|
113 |
+
st.write(f"Document {i}:\n{doc.text}")
|
114 |
+
|
115 |
+
|
116 |
+
if __name__ == "__main__":
|
117 |
+
main()
|
src/embeddings.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
# Databricks notebook source
|
2 |
-
from
|
3 |
from langchain_community.vectorstores.utils import DistanceStrategy
|
4 |
|
5 |
|
|
|
1 |
# Databricks notebook source
|
2 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
3 |
from langchain_community.vectorstores.utils import DistanceStrategy
|
4 |
|
5 |
|