Update app.py
Browse files
app.py
CHANGED
@@ -215,17 +215,9 @@ def custom_tokenize(text, tokenizer):
|
|
215 |
return tokenizer.encode(text).tokens
|
216 |
|
217 |
# Embedding and Vector Store
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
if model_type == 'HuggingFace':
|
222 |
-
return HuggingFaceEmbeddings(model_name=model_path)
|
223 |
-
elif model_type == 'OpenAI':
|
224 |
-
return OpenAIEmbeddings(model=model_path)
|
225 |
-
elif model_type == 'Cohere':
|
226 |
-
return CohereEmbeddings(model=model_path)
|
227 |
-
else:
|
228 |
-
raise ValueError(f"Unsupported model type: {model_type}")
|
229 |
|
230 |
def get_text_splitter(split_strategy, chunk_size, overlap_size, custom_separators=None):
|
231 |
if split_strategy == 'token':
|
@@ -239,9 +231,35 @@ def get_text_splitter(split_strategy, chunk_size, overlap_size, custom_separator
|
|
239 |
else:
|
240 |
raise ValueError(f"Unsupported split strategy: {split_strategy}")
|
241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
242 |
def get_vector_store(vector_store_type, chunks, embedding_model):
|
243 |
chunks_tuple = tuple(chunks)
|
244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
245 |
|
246 |
def custom_similarity(query_embedding, doc_embedding, query, doc_text, phonetic_weight=0.3):
|
247 |
embedding_sim = np.dot(query_embedding, doc_embedding) / (np.linalg.norm(query_embedding) * np.linalg.norm(doc_embedding))
|
@@ -259,18 +277,9 @@ def _create_vector_store(vector_store_type, chunks_tuple, embedding_model):
|
|
259 |
else:
|
260 |
raise ValueError(f"Unsupported vector store type: {vector_store_type}")
|
261 |
|
262 |
-
def get_retriever(vector_store, search_type, search_kwargs):
|
263 |
-
if search_type == 'similarity':
|
264 |
-
return vector_store.as_retriever(search_type="similarity", search_kwargs=search_kwargs)
|
265 |
-
elif search_type == 'mmr':
|
266 |
-
return vector_store.as_retriever(search_type="mmr", search_kwargs=search_kwargs)
|
267 |
-
elif search_type == 'custom':
|
268 |
-
return vector_store.as_retriever(search_type="similarity", search_kwargs=search_kwargs)
|
269 |
-
else:
|
270 |
-
raise ValueError(f"Unsupported search type: {search_type}")
|
271 |
|
272 |
# Main Processing Functions
|
273 |
-
def process_files(file_path, model_type, model_name, split_strategy, chunk_size, overlap_size, custom_separators, lang='german', custom_tokenizer_file=None, custom_tokenizer_model=None, custom_tokenizer_vocab_size=10000, custom_tokenizer_special_tokens=None):
|
274 |
if file_path:
|
275 |
text = FileHandler.extract_text(file_path)
|
276 |
else:
|
@@ -282,7 +291,7 @@ def process_files(file_path, model_type, model_name, split_strategy, chunk_size,
|
|
282 |
if custom_tokenizer_file:
|
283 |
tokenizer = create_custom_tokenizer(custom_tokenizer_file, custom_tokenizer_model, custom_tokenizer_vocab_size, custom_tokenizer_special_tokens)
|
284 |
text = ' '.join(custom_tokenize(text, tokenizer))
|
285 |
-
|
286 |
text = preprocess_text(text, lang)
|
287 |
|
288 |
text_splitter = get_text_splitter(split_strategy, chunk_size, overlap_size, custom_separators)
|
@@ -292,8 +301,8 @@ def process_files(file_path, model_type, model_name, split_strategy, chunk_size,
|
|
292 |
|
293 |
return chunks, embedding_model, len(text.split())
|
294 |
|
295 |
-
def search_embeddings(chunks, embedding_model, vector_store_type, search_type, query, top_k, lang='german', phonetic_weight=0.3):
|
296 |
-
preprocessed_query = preprocess_text(query, lang)
|
297 |
|
298 |
vector_store = get_vector_store(vector_store_type, chunks, embedding_model)
|
299 |
retriever = get_retriever(vector_store, search_type, {"k": top_k})
|
@@ -303,8 +312,11 @@ def search_embeddings(chunks, embedding_model, vector_store_type, search_type, q
|
|
303 |
|
304 |
def score_result(doc):
|
305 |
similarity_score = vector_store.similarity_search_with_score(doc.page_content, k=1)[0][1]
|
306 |
-
|
307 |
-
|
|
|
|
|
|
|
308 |
|
309 |
results = sorted(results, key=score_result, reverse=True)
|
310 |
end_time = time.time()
|
@@ -323,6 +335,8 @@ def search_embeddings(chunks, embedding_model, vector_store_type, search_type, q
|
|
323 |
|
324 |
return results_df, end_time - start_time, vector_store, results
|
325 |
|
|
|
|
|
326 |
# Evaluation Metrics
|
327 |
# ... (previous code remains the same)
|
328 |
|
|
|
215 |
return tokenizer.encode(text).tokens
|
216 |
|
217 |
# Embedding and Vector Store
|
218 |
+
#@lru_cache(maxsize=None)
|
219 |
+
|
220 |
+
# Helper functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
|
222 |
def get_text_splitter(split_strategy, chunk_size, overlap_size, custom_separators=None):
|
223 |
if split_strategy == 'token':
|
|
|
231 |
else:
|
232 |
raise ValueError(f"Unsupported split strategy: {split_strategy}")
|
233 |
|
234 |
+
def get_embedding_model(model_type, model_name):
|
235 |
+
model_path = model_manager.get_model(model_type, model_name)
|
236 |
+
if model_type == 'HuggingFace':
|
237 |
+
return HuggingFaceEmbeddings(model_name=model_path)
|
238 |
+
elif model_type == 'OpenAI':
|
239 |
+
return OpenAIEmbeddings(model=model_path)
|
240 |
+
elif model_type == 'Cohere':
|
241 |
+
return CohereEmbeddings(model=model_path)
|
242 |
+
else:
|
243 |
+
raise ValueError(f"Unsupported model type: {model_type}")
|
244 |
+
|
245 |
def get_vector_store(vector_store_type, chunks, embedding_model):
|
246 |
chunks_tuple = tuple(chunks)
|
247 |
+
if vector_store_type == 'FAISS':
|
248 |
+
return FAISS.from_texts(chunks, embedding_model)
|
249 |
+
elif vector_store_type == 'Chroma':
|
250 |
+
return Chroma.from_texts(chunks, embedding_model)
|
251 |
+
else:
|
252 |
+
raise ValueError(f"Unsupported vector store type: {vector_store_type}")
|
253 |
+
|
254 |
+
def get_retriever(vector_store, search_type, search_kwargs):
|
255 |
+
if search_type == 'similarity':
|
256 |
+
return vector_store.as_retriever(search_type="similarity", search_kwargs=search_kwargs)
|
257 |
+
elif search_type == 'mmr':
|
258 |
+
return vector_store.as_retriever(search_type="mmr", search_kwargs=search_kwargs)
|
259 |
+
elif search_type == 'custom':
|
260 |
+
return vector_store.as_retriever(search_type="similarity", search_kwargs=search_kwargs)
|
261 |
+
else:
|
262 |
+
raise ValueError(f"Unsupported search type: {search_type}")
|
263 |
|
264 |
def custom_similarity(query_embedding, doc_embedding, query, doc_text, phonetic_weight=0.3):
|
265 |
embedding_sim = np.dot(query_embedding, doc_embedding) / (np.linalg.norm(query_embedding) * np.linalg.norm(doc_embedding))
|
|
|
277 |
else:
|
278 |
raise ValueError(f"Unsupported vector store type: {vector_store_type}")
|
279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
|
281 |
# Main Processing Functions
|
282 |
+
def process_files(file_path, model_type, model_name, split_strategy, chunk_size, overlap_size, custom_separators, lang='german', apply_preprocessing=True, custom_tokenizer_file=None, custom_tokenizer_model=None, custom_tokenizer_vocab_size=10000, custom_tokenizer_special_tokens=None):
|
283 |
if file_path:
|
284 |
text = FileHandler.extract_text(file_path)
|
285 |
else:
|
|
|
291 |
if custom_tokenizer_file:
|
292 |
tokenizer = create_custom_tokenizer(custom_tokenizer_file, custom_tokenizer_model, custom_tokenizer_vocab_size, custom_tokenizer_special_tokens)
|
293 |
text = ' '.join(custom_tokenize(text, tokenizer))
|
294 |
+
elif apply_preprocessing:
|
295 |
text = preprocess_text(text, lang)
|
296 |
|
297 |
text_splitter = get_text_splitter(split_strategy, chunk_size, overlap_size, custom_separators)
|
|
|
301 |
|
302 |
return chunks, embedding_model, len(text.split())
|
303 |
|
304 |
+
def search_embeddings(chunks, embedding_model, vector_store_type, search_type, query, top_k, lang='german', apply_phonetic=True, phonetic_weight=0.3):
|
305 |
+
preprocessed_query = preprocess_text(query, lang) if apply_phonetic else query
|
306 |
|
307 |
vector_store = get_vector_store(vector_store_type, chunks, embedding_model)
|
308 |
retriever = get_retriever(vector_store, search_type, {"k": top_k})
|
|
|
312 |
|
313 |
def score_result(doc):
|
314 |
similarity_score = vector_store.similarity_search_with_score(doc.page_content, k=1)[0][1]
|
315 |
+
if apply_phonetic:
|
316 |
+
phonetic_score = phonetic_match(doc.page_content, query)
|
317 |
+
return (1 - phonetic_weight) * similarity_score + phonetic_weight * phonetic_score
|
318 |
+
else:
|
319 |
+
return similarity_score
|
320 |
|
321 |
results = sorted(results, key=score_result, reverse=True)
|
322 |
end_time = time.time()
|
|
|
335 |
|
336 |
return results_df, end_time - start_time, vector_store, results
|
337 |
|
338 |
+
|
339 |
+
|
340 |
# Evaluation Metrics
|
341 |
# ... (previous code remains the same)
|
342 |
|