Spaces:
Running
Running
Update src/doc_qa.py
Browse files- src/doc_qa.py +127 -124
src/doc_qa.py
CHANGED
@@ -1,124 +1,127 @@
|
|
1 |
-
from typing import Optional, List
|
2 |
-
|
3 |
-
from langchain.document_loaders import TextLoader #for textfiles
|
4 |
-
from langchain.text_splitter import CharacterTextSplitter #text splitter
|
5 |
-
from langchain.embeddings import HuggingFaceEmbeddings #for using HugginFace models
|
6 |
-
|
7 |
-
from langchain.document_loaders import UnstructuredPDFLoader #load pdf
|
8 |
-
from langchain.indexes import VectorstoreIndexCreator #vectorize db index with chromadb
|
9 |
-
from langchain.chains import RetrievalQA
|
10 |
-
from langchain.document_loaders import UnstructuredURLLoader #load urls into docoument-loader
|
11 |
-
from langchain.chains.question_answering import load_qa_chain
|
12 |
-
from langchain import HuggingFaceHub
|
13 |
-
import os
|
14 |
-
from langchain.document_loaders import TextLoader, PyMuPDFLoader
|
15 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
16 |
-
from langchain.llms import HuggingFacePipeline
|
17 |
-
from langchain.vectorstores import FAISS
|
18 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
19 |
-
from langchain import PromptTemplate
|
20 |
-
from langchain.chains import LLMChain
|
21 |
-
from langchain.base_language import BaseLanguageModel
|
22 |
-
from docx import Document
|
23 |
-
from langchain.document_loaders import DirectoryLoader
|
24 |
-
multi_directory_path=r'tmp/'
|
25 |
-
|
26 |
-
from transformers import pipeline
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
#
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
#
|
46 |
-
|
47 |
-
|
48 |
-
#
|
49 |
-
|
50 |
-
pipe = pipeline("
|
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 |
-
print(
|
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 |
-
#
|
113 |
-
#
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
|
1 |
+
from typing import Optional, List
|
2 |
+
|
3 |
+
from langchain.document_loaders import TextLoader #for textfiles
|
4 |
+
from langchain.text_splitter import CharacterTextSplitter #text splitter
|
5 |
+
from langchain.embeddings import HuggingFaceEmbeddings #for using HugginFace models
|
6 |
+
|
7 |
+
from langchain.document_loaders import UnstructuredPDFLoader #load pdf
|
8 |
+
from langchain.indexes import VectorstoreIndexCreator #vectorize db index with chromadb
|
9 |
+
from langchain.chains import RetrievalQA
|
10 |
+
from langchain.document_loaders import UnstructuredURLLoader #load urls into docoument-loader
|
11 |
+
from langchain.chains.question_answering import load_qa_chain
|
12 |
+
from langchain import HuggingFaceHub
|
13 |
+
import os
|
14 |
+
from langchain.document_loaders import TextLoader, PyMuPDFLoader
|
15 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
16 |
+
from langchain.llms import HuggingFacePipeline
|
17 |
+
from langchain.vectorstores import FAISS
|
18 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
19 |
+
from langchain import PromptTemplate
|
20 |
+
from langchain.chains import LLMChain
|
21 |
+
from langchain.base_language import BaseLanguageModel
|
22 |
+
from docx import Document
|
23 |
+
from langchain.document_loaders import DirectoryLoader
|
24 |
+
multi_directory_path=r'tmp/'
|
25 |
+
|
26 |
+
from transformers import pipeline
|
27 |
+
|
28 |
+
from sentence_transformers import SentenceTransformer
|
29 |
+
model = SentenceTransformer("sentence-transformers/LaBSE")
|
30 |
+
|
31 |
+
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/LaBSE')
|
32 |
+
|
33 |
+
|
34 |
+
from langchain_community.document_loaders import TextLoader, PyPDFLoader, Docx2txtLoader
|
35 |
+
|
36 |
+
after_rag_template = """Answer the question based only on the following context:
|
37 |
+
{context}
|
38 |
+
Question: {question}
|
39 |
+
"""
|
40 |
+
#pipe = pipeline("text2text-generation", model="google/flan-t5-large" ,max_new_tokens=100)
|
41 |
+
#pipe = pipeline("text2text-generation", model="google/mt5-large" ,max_new_tokens=200)
|
42 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
43 |
+
#tokenizer = AutoTokenizer.from_pretrained("rinna/bilingual-gpt-neox-4b", use_fast=False)
|
44 |
+
#model = AutoModelForSeq2SeqLM.from_pretrained("google/mt5-base")
|
45 |
+
# Load model directly
|
46 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
47 |
+
|
48 |
+
#tokenizer = AutoTokenizer.from_pretrained("rinna/bilingual-gpt-neox-4b")
|
49 |
+
#model = AutoModelForCausalLM.from_pretrained("rinna/bilingual-gpt-neox-4b")
|
50 |
+
#pipe = pipeline("text2text-generation", model="rinna/bilingual-gpt-neox-4b" ,max_new_tokens=200)
|
51 |
+
#pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer, max_new_tokens=200)
|
52 |
+
|
53 |
+
pipe = pipeline("question-answering", model="deepset/xlm-roberta-base-squad2")
|
54 |
+
|
55 |
+
llm = HuggingFacePipeline(pipeline=pipe)
|
56 |
+
|
57 |
+
def run_custom_qa(question, retrieved_docs):
|
58 |
+
context = " ".join([doc.page_content for doc in retrieved_docs])
|
59 |
+
output = pipe(question=question, context=context)
|
60 |
+
return output["answer"]
|
61 |
+
|
62 |
+
def docs_vector_index():
|
63 |
+
from langchain.document_loaders import DirectoryLoader
|
64 |
+
# Define a directory path
|
65 |
+
directory_path = r"C:\Users\savni\PycharmProjects\DocsSearchEngine\tmp"
|
66 |
+
|
67 |
+
# Create the DirectoryLoader, specifying loaders for each file type
|
68 |
+
loader = DirectoryLoader(
|
69 |
+
directory_path,
|
70 |
+
glob="**/*", # This pattern loads all files; modify as needed
|
71 |
+
|
72 |
+
)
|
73 |
+
docs = loader.load()
|
74 |
+
|
75 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
76 |
+
chunk_size=1024, chunk_overlap=100, separators=[" ", ",", "\n", "."]
|
77 |
+
)
|
78 |
+
print(docs)
|
79 |
+
docs_chunks = text_splitter.split_documents(docs)
|
80 |
+
|
81 |
+
print(f"docs_chunks length: {len(docs_chunks)}")
|
82 |
+
print('********************docs_chunks',docs_chunks)
|
83 |
+
if len(docs_chunks)>0:
|
84 |
+
db = FAISS.from_documents(docs_chunks, embeddings)
|
85 |
+
return db
|
86 |
+
else:
|
87 |
+
return ''
|
88 |
+
|
89 |
+
|
90 |
+
#chain = load_qa_chain(llm, chain_type="stuff")
|
91 |
+
|
92 |
+
from langchain.prompts import PromptTemplate
|
93 |
+
|
94 |
+
template = """You are an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. Below is some information.
|
95 |
+
{context}
|
96 |
+
|
97 |
+
Based on the above information only, answer the below question.
|
98 |
+
|
99 |
+
{question} Be concise."""
|
100 |
+
|
101 |
+
prompt = PromptTemplate.from_template(template)
|
102 |
+
print(prompt.input_variables)
|
103 |
+
|
104 |
+
|
105 |
+
#query_llm = LLMChain(llm=llm, prompt=prompt)
|
106 |
+
|
107 |
+
# def doc_qa1(query, db):
|
108 |
+
# similar_doc = db.similarity_search(query, k=2)
|
109 |
+
# doc_c=[]
|
110 |
+
# for c in similar_doc:
|
111 |
+
# doc_c.append(c.page_content)
|
112 |
+
# context=''.join(doc_c)
|
113 |
+
# #response = query_llm.run({"context": context, "question": query})
|
114 |
+
# response = query_llm.run(context=context, question=query)
|
115 |
+
# print('response',response)
|
116 |
+
# return response
|
117 |
+
|
118 |
+
def doc_qa(query, db):
|
119 |
+
print("*************************custom qa doc_qa",query)
|
120 |
+
retriever = db.as_retriever()
|
121 |
+
relevant_docs = retriever.get_relevant_documents(query)
|
122 |
+
response=run_custom_qa(query, relevant_docs)
|
123 |
+
print('response', response)
|
124 |
+
return response
|
125 |
+
|
126 |
+
|
127 |
+
|