Spaces:
Sleeping
Sleeping
sgt444pepper
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -23,6 +23,10 @@ PROMPT = """/
|
|
23 |
|
24 |
"""
|
25 |
|
|
|
|
|
|
|
|
|
26 |
def load_documents(file_paths):
|
27 |
documents = []
|
28 |
for path in file_paths:
|
@@ -31,11 +35,11 @@ def load_documents(file_paths):
|
|
31 |
return documents
|
32 |
|
33 |
class DocumentIndexer:
|
34 |
-
def __init__(self, documents):
|
35 |
self.documents = documents
|
36 |
self.bm25 = BM25Okapi([doc.split() for doc in documents])
|
37 |
-
self.tokenizer =
|
38 |
-
self.model =
|
39 |
self.index = self.create_faiss_index()
|
40 |
|
41 |
def create_faiss_index(self):
|
@@ -63,9 +67,8 @@ class DocumentIndexer:
|
|
63 |
return [self.documents[i] for i in indices[0]]
|
64 |
|
65 |
class Reranker:
|
66 |
-
def __init__(self,
|
67 |
-
self.
|
68 |
-
self.model = CrossEncoder(model_name)
|
69 |
|
70 |
def rank(self, query, documents):
|
71 |
pairs = [(query, doc) for doc in documents]
|
@@ -105,8 +108,8 @@ def chatbot_interface(query, history):
|
|
105 |
file_paths = ["company.txt", "Base.txt"]
|
106 |
documents = load_documents(file_paths)
|
107 |
|
108 |
-
indexer = DocumentIndexer(documents)
|
109 |
-
reranker = Reranker()
|
110 |
|
111 |
chatbot = QAChatbot(indexer, reranker)
|
112 |
answer = chatbot.generate_answer(query)
|
|
|
23 |
|
24 |
"""
|
25 |
|
26 |
+
tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-mpnet-base-v2")
|
27 |
+
model = AutoModel.from_pretrained("sentence-transformers/all-mpnet-base-v2")
|
28 |
+
reranker_model = CrossEncoder("cross-encoder/ms-marco-TinyBERT-L-6")
|
29 |
+
|
30 |
def load_documents(file_paths):
|
31 |
documents = []
|
32 |
for path in file_paths:
|
|
|
35 |
return documents
|
36 |
|
37 |
class DocumentIndexer:
|
38 |
+
def __init__(self, documents, tokenizer, model):
|
39 |
self.documents = documents
|
40 |
self.bm25 = BM25Okapi([doc.split() for doc in documents])
|
41 |
+
self.tokenizer = tokenizer
|
42 |
+
self.model = model
|
43 |
self.index = self.create_faiss_index()
|
44 |
|
45 |
def create_faiss_index(self):
|
|
|
67 |
return [self.documents[i] for i in indices[0]]
|
68 |
|
69 |
class Reranker:
|
70 |
+
def __init__(self, reranker):
|
71 |
+
self.model = reranker
|
|
|
72 |
|
73 |
def rank(self, query, documents):
|
74 |
pairs = [(query, doc) for doc in documents]
|
|
|
108 |
file_paths = ["company.txt", "Base.txt"]
|
109 |
documents = load_documents(file_paths)
|
110 |
|
111 |
+
indexer = DocumentIndexer(documents, tokenizer, model)
|
112 |
+
reranker = Reranker(reranker_model)
|
113 |
|
114 |
chatbot = QAChatbot(indexer, reranker)
|
115 |
answer = chatbot.generate_answer(query)
|