2022-09-05 00:05:13 +02:00
|
|
|
# External Packages
|
2023-01-09 20:17:36 +01:00
|
|
|
from copy import deepcopy
|
|
|
|
from pathlib import Path
|
2021-10-03 04:46:29 +02:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
# Internal Packages
|
2022-07-21 16:05:43 +02:00
|
|
|
from src.search_type import image_search, text_search
|
2022-09-10 13:15:43 +02:00
|
|
|
from src.utils.helpers import resolve_absolute_path
|
2022-07-21 16:05:43 +02:00
|
|
|
from src.utils.rawconfig import ContentConfig, TextContentConfig, ImageContentConfig, SearchConfig, TextSearchConfig, ImageSearchConfig
|
2022-09-14 09:53:43 +02:00
|
|
|
from src.processor.org_mode.org_to_jsonl import OrgToJsonl
|
2022-09-05 00:05:13 +02:00
|
|
|
from src.search_filter.date_filter import DateFilter
|
|
|
|
from src.search_filter.word_filter import WordFilter
|
|
|
|
from src.search_filter.file_filter import FileFilter
|
2021-10-03 04:46:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
2022-09-10 13:15:43 +02:00
|
|
|
def search_config() -> SearchConfig:
|
|
|
|
model_dir = resolve_absolute_path('~/.khoj/search')
|
|
|
|
model_dir.mkdir(parents=True, exist_ok=True)
|
2022-01-15 02:54:38 +01:00
|
|
|
search_config = SearchConfig()
|
2022-01-15 02:13:14 +01:00
|
|
|
|
2022-07-21 16:05:43 +02:00
|
|
|
search_config.symmetric = TextSearchConfig(
|
2022-07-18 18:16:40 +02:00
|
|
|
encoder = "sentence-transformers/all-MiniLM-L6-v2",
|
2022-01-15 02:13:14 +01:00
|
|
|
cross_encoder = "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
2022-09-10 13:15:43 +02:00
|
|
|
model_directory = model_dir / 'symmetric/'
|
2022-01-15 02:13:14 +01:00
|
|
|
)
|
|
|
|
|
2022-07-21 16:05:43 +02:00
|
|
|
search_config.asymmetric = TextSearchConfig(
|
2022-07-18 18:00:19 +02:00
|
|
|
encoder = "sentence-transformers/multi-qa-MiniLM-L6-cos-v1",
|
2022-01-15 02:13:14 +01:00
|
|
|
cross_encoder = "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
2022-09-10 13:15:43 +02:00
|
|
|
model_directory = model_dir / 'asymmetric/'
|
2022-01-15 02:13:14 +01:00
|
|
|
)
|
|
|
|
|
2022-01-15 02:54:38 +01:00
|
|
|
search_config.image = ImageSearchConfig(
|
2022-08-04 21:54:39 +02:00
|
|
|
encoder = "sentence-transformers/clip-ViT-B-32",
|
2022-09-10 13:15:43 +02:00
|
|
|
model_directory = model_dir / 'image/'
|
2022-01-15 02:13:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return search_config
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
2022-09-10 13:15:43 +02:00
|
|
|
def content_config(tmp_path_factory, search_config: SearchConfig):
|
|
|
|
content_dir = tmp_path_factory.mktemp('content')
|
2022-01-15 02:13:14 +01:00
|
|
|
|
2021-10-03 04:46:29 +02:00
|
|
|
# Generate Image Embeddings from Test Images
|
2022-08-20 13:21:04 +02:00
|
|
|
content_config = ContentConfig()
|
|
|
|
content_config.image = ImageContentConfig(
|
|
|
|
input_directories = ['tests/data/images'],
|
2022-09-10 13:15:43 +02:00
|
|
|
embeddings_file = content_dir.joinpath('image_embeddings.pt'),
|
|
|
|
batch_size = 1,
|
2022-08-20 13:21:04 +02:00
|
|
|
use_xmp_metadata = False)
|
2021-10-03 04:46:29 +02:00
|
|
|
|
2022-09-03 21:13:25 +02:00
|
|
|
image_search.setup(content_config.image, search_config.image, regenerate=False)
|
2021-10-03 04:46:29 +02:00
|
|
|
|
|
|
|
# Generate Notes Embeddings from Test Notes
|
2022-01-15 02:54:38 +01:00
|
|
|
content_config.org = TextContentConfig(
|
2022-01-29 07:57:08 +01:00
|
|
|
input_files = None,
|
2022-09-12 09:39:39 +02:00
|
|
|
input_filter = ['tests/data/org/*.org'],
|
2022-09-10 13:15:43 +02:00
|
|
|
compressed_jsonl = content_dir.joinpath('notes.jsonl.gz'),
|
|
|
|
embeddings_file = content_dir.joinpath('note_embeddings.pt'))
|
2021-10-03 04:46:29 +02:00
|
|
|
|
2022-09-07 01:43:58 +02:00
|
|
|
filters = [DateFilter(), WordFilter(), FileFilter()]
|
2022-09-14 09:53:43 +02:00
|
|
|
text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=False, filters=filters)
|
2021-10-03 04:46:29 +02:00
|
|
|
|
2022-09-10 21:11:43 +02:00
|
|
|
return content_config
|
2023-01-09 20:17:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def new_org_file(content_config: ContentConfig):
|
|
|
|
# Setup
|
|
|
|
new_org_file = Path(content_config.org.input_filter[0]).parent / "new_file.org"
|
|
|
|
new_org_file.touch()
|
|
|
|
|
|
|
|
yield new_org_file
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
if new_org_file.exists():
|
|
|
|
new_org_file.unlink()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def org_config_with_only_new_file(content_config: ContentConfig, new_org_file: Path):
|
|
|
|
new_org_config = deepcopy(content_config.org)
|
|
|
|
new_org_config.input_files = [f'{new_org_file}']
|
|
|
|
new_org_config.input_filter = None
|
|
|
|
return new_org_config
|