2021-08-17 12:59:58 +02:00
|
|
|
# Standard Packages
|
|
|
|
import sys
|
|
|
|
import pathlib
|
2021-08-16 02:50:08 +02:00
|
|
|
from typing import Optional
|
2021-08-17 12:59:58 +02:00
|
|
|
|
|
|
|
# External Packages
|
|
|
|
import uvicorn
|
2021-08-16 02:50:08 +02:00
|
|
|
from fastapi import FastAPI
|
2021-08-17 12:59:58 +02:00
|
|
|
|
|
|
|
# Internal Packages
|
2021-08-23 06:00:54 +02:00
|
|
|
from search_type import asymmetric, symmetric_ledger, image_search
|
2021-08-22 04:21:38 +02:00
|
|
|
from utils.helpers import get_from_dict
|
|
|
|
from utils.cli import cli
|
2021-09-30 05:24:27 +02:00
|
|
|
from utils.config import SearchType, SearchSettings, SearchModels
|
2021-08-17 12:59:58 +02:00
|
|
|
|
2021-08-16 02:50:08 +02:00
|
|
|
|
2021-09-30 05:24:27 +02:00
|
|
|
# Application Global State
|
|
|
|
model = SearchModels()
|
2021-09-30 04:18:33 +02:00
|
|
|
search_settings = SearchSettings()
|
2021-08-16 02:50:08 +02:00
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
@app.get('/search')
|
2021-09-30 04:02:55 +02:00
|
|
|
def search(q: str, n: Optional[int] = 5, t: Optional[SearchType] = None):
|
2021-08-16 02:50:08 +02:00
|
|
|
if q is None or q == '':
|
|
|
|
print(f'No query param (q) passed in API call to initiate search')
|
|
|
|
return {}
|
|
|
|
|
|
|
|
user_query = q
|
|
|
|
results_count = n
|
|
|
|
|
2021-09-30 04:18:33 +02:00
|
|
|
if (t == SearchType.Notes or t == None) and search_settings.notes_search_enabled:
|
2021-08-16 02:50:08 +02:00
|
|
|
# query notes
|
2021-09-30 05:24:27 +02:00
|
|
|
hits = asymmetric.query_notes(user_query, model.notes_search)
|
2021-08-16 02:50:08 +02:00
|
|
|
|
|
|
|
# collate and return results
|
2021-09-30 05:24:27 +02:00
|
|
|
return asymmetric.collate_results(hits, model.notes_search.entries, results_count)
|
2021-08-16 02:50:08 +02:00
|
|
|
|
2021-09-30 04:18:33 +02:00
|
|
|
if (t == SearchType.Music or t == None) and search_settings.music_search_enabled:
|
2021-08-29 12:07:36 +02:00
|
|
|
# query music library
|
2021-09-30 06:09:42 +02:00
|
|
|
hits = asymmetric.query_notes(user_query, model.music_search)
|
2021-08-29 12:07:36 +02:00
|
|
|
|
|
|
|
# collate and return results
|
2021-09-30 06:09:42 +02:00
|
|
|
return asymmetric.collate_results(hits, model.music_search.entries, results_count)
|
2021-08-29 12:07:36 +02:00
|
|
|
|
2021-09-30 04:18:33 +02:00
|
|
|
if (t == SearchType.Ledger or t == None) and search_settings.ledger_search_enabled:
|
2021-08-22 12:16:57 +02:00
|
|
|
# query transactions
|
2021-09-30 06:09:42 +02:00
|
|
|
hits = symmetric_ledger.query_transactions(user_query, model.ledger_search)
|
2021-08-22 12:16:57 +02:00
|
|
|
|
|
|
|
# collate and return results
|
2021-09-30 06:09:42 +02:00
|
|
|
return symmetric_ledger.collate_results(hits, model.ledger_search.entries, results_count)
|
2021-08-22 12:16:57 +02:00
|
|
|
|
2021-09-30 04:18:33 +02:00
|
|
|
if (t == SearchType.Image or t == None) and search_settings.image_search_enabled:
|
2021-08-23 06:00:54 +02:00
|
|
|
# query transactions
|
2021-09-30 06:09:42 +02:00
|
|
|
hits = image_search.query_images(user_query, model.image_search, args.verbose)
|
2021-08-23 06:00:54 +02:00
|
|
|
|
|
|
|
# collate and return results
|
|
|
|
return image_search.collate_results(
|
|
|
|
hits,
|
2021-09-30 06:09:42 +02:00
|
|
|
model.image_search.image_names,
|
2021-08-23 06:00:54 +02:00
|
|
|
image_config['input-directory'],
|
|
|
|
results_count)
|
|
|
|
|
2021-08-16 02:50:08 +02:00
|
|
|
else:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2021-08-17 03:52:38 +02:00
|
|
|
@app.get('/regenerate')
|
2021-09-30 04:02:55 +02:00
|
|
|
def regenerate(t: Optional[SearchType] = None):
|
2021-09-30 04:18:33 +02:00
|
|
|
if (t == SearchType.Notes or t == None) and search_settings.notes_search_enabled:
|
2021-08-17 13:36:48 +02:00
|
|
|
# Extract Entries, Generate Embeddings
|
2021-09-30 05:24:27 +02:00
|
|
|
models.notes_search = asymmetric.setup(
|
2021-08-22 03:47:55 +02:00
|
|
|
org_config['input-files'],
|
|
|
|
org_config['input-filter'],
|
|
|
|
pathlib.Path(org_config['compressed-jsonl']),
|
|
|
|
pathlib.Path(org_config['embeddings-file']),
|
|
|
|
regenerate=True,
|
|
|
|
verbose=args.verbose)
|
|
|
|
|
2021-09-30 04:18:33 +02:00
|
|
|
if (t == SearchType.Music or t == None) and search_settings.music_search_enabled:
|
2021-08-29 12:07:36 +02:00
|
|
|
# Extract Entries, Generate Song Embeddings
|
2021-09-30 06:09:42 +02:00
|
|
|
model.music_search = asymmetric.setup(
|
2021-08-29 12:07:36 +02:00
|
|
|
song_config['input-files'],
|
|
|
|
song_config['input-filter'],
|
|
|
|
pathlib.Path(song_config['compressed-jsonl']),
|
|
|
|
pathlib.Path(song_config['embeddings-file']),
|
|
|
|
regenerate=True,
|
|
|
|
verbose=args.verbose)
|
|
|
|
|
2021-09-30 04:18:33 +02:00
|
|
|
if (t == SearchType.Ledger or t == None) and search_settings.ledger_search_enabled:
|
2021-08-22 12:16:57 +02:00
|
|
|
# Extract Entries, Generate Embeddings
|
2021-09-30 06:09:42 +02:00
|
|
|
model.ledger_search = symmetric_ledger.setup(
|
2021-08-22 12:16:57 +02:00
|
|
|
ledger_config['input-files'],
|
|
|
|
ledger_config['input-filter'],
|
|
|
|
pathlib.Path(ledger_config['compressed-jsonl']),
|
|
|
|
pathlib.Path(ledger_config['embeddings-file']),
|
|
|
|
regenerate=True,
|
|
|
|
verbose=args.verbose)
|
2021-08-17 08:47:33 +02:00
|
|
|
|
2021-09-30 04:18:33 +02:00
|
|
|
if (t == SearchType.Image or t == None) and search_settings.image_search_enabled:
|
2021-08-23 06:00:54 +02:00
|
|
|
# Extract Images, Generate Embeddings
|
2021-09-30 06:09:42 +02:00
|
|
|
model.image_search = image_search.setup(
|
2021-08-23 06:00:54 +02:00
|
|
|
pathlib.Path(image_config['input-directory']),
|
|
|
|
pathlib.Path(image_config['embeddings-file']),
|
|
|
|
regenerate=True,
|
|
|
|
verbose=args.verbose)
|
|
|
|
|
2021-08-17 08:47:33 +02:00
|
|
|
return {'status': 'ok', 'message': 'regeneration completed'}
|
2021-08-17 03:52:38 +02:00
|
|
|
|
|
|
|
|
2021-08-17 13:00:45 +02:00
|
|
|
if __name__ == '__main__':
|
2021-08-22 04:21:38 +02:00
|
|
|
args = cli(sys.argv[1:])
|
2021-08-22 03:47:55 +02:00
|
|
|
|
2021-08-22 12:16:57 +02:00
|
|
|
# Initialize Org Notes Search
|
|
|
|
org_config = get_from_dict(args.config, 'content-type', 'org')
|
|
|
|
if org_config and ('input-files' in org_config or 'input-filter' in org_config):
|
2021-09-30 04:18:33 +02:00
|
|
|
search_settings.notes_search_enabled = True
|
2021-09-30 05:24:27 +02:00
|
|
|
model.notes_search = asymmetric.setup(
|
2021-08-22 03:47:55 +02:00
|
|
|
org_config['input-files'],
|
|
|
|
org_config['input-filter'],
|
|
|
|
pathlib.Path(org_config['compressed-jsonl']),
|
|
|
|
pathlib.Path(org_config['embeddings-file']),
|
|
|
|
args.regenerate,
|
|
|
|
args.verbose)
|
2021-08-16 02:50:08 +02:00
|
|
|
|
2021-08-29 12:07:36 +02:00
|
|
|
# Initialize Org Music Search
|
|
|
|
song_config = get_from_dict(args.config, 'content-type', 'music')
|
|
|
|
music_search_enabled = False
|
|
|
|
if song_config and ('input-files' in song_config or 'input-filter' in song_config):
|
2021-09-30 04:18:33 +02:00
|
|
|
search_settings.music_search_enabled = True
|
2021-09-30 06:09:42 +02:00
|
|
|
model.music_search = asymmetric.setup(
|
2021-08-29 12:07:36 +02:00
|
|
|
song_config['input-files'],
|
|
|
|
song_config['input-filter'],
|
|
|
|
pathlib.Path(song_config['compressed-jsonl']),
|
|
|
|
pathlib.Path(song_config['embeddings-file']),
|
|
|
|
args.regenerate,
|
|
|
|
args.verbose)
|
|
|
|
|
2021-08-22 12:16:57 +02:00
|
|
|
# Initialize Ledger Search
|
|
|
|
ledger_config = get_from_dict(args.config, 'content-type', 'ledger')
|
|
|
|
if ledger_config and ('input-files' in ledger_config or 'input-filter' in ledger_config):
|
2021-09-30 04:18:33 +02:00
|
|
|
search_settings.ledger_search_enabled = True
|
2021-09-30 06:09:42 +02:00
|
|
|
model.ledger_search = symmetric_ledger.setup(
|
2021-08-22 12:16:57 +02:00
|
|
|
ledger_config['input-files'],
|
|
|
|
ledger_config['input-filter'],
|
|
|
|
pathlib.Path(ledger_config['compressed-jsonl']),
|
|
|
|
pathlib.Path(ledger_config['embeddings-file']),
|
|
|
|
args.regenerate,
|
|
|
|
args.verbose)
|
2021-08-16 02:50:08 +02:00
|
|
|
|
2021-08-23 06:00:54 +02:00
|
|
|
# Initialize Image Search
|
|
|
|
image_config = get_from_dict(args.config, 'content-type', 'image')
|
|
|
|
if image_config and 'input-directory' in image_config:
|
2021-09-30 04:18:33 +02:00
|
|
|
search_settings.image_search_enabled = True
|
2021-09-30 06:09:42 +02:00
|
|
|
model.image_search = image_search.setup(
|
2021-08-23 06:00:54 +02:00
|
|
|
pathlib.Path(image_config['input-directory']),
|
|
|
|
pathlib.Path(image_config['embeddings-file']),
|
2021-09-16 19:51:39 +02:00
|
|
|
batch_size=image_config['batch-size'],
|
|
|
|
regenerate=args.regenerate,
|
2021-09-16 21:01:05 +02:00
|
|
|
use_xmp_metadata={'yes': True, 'no': False}[image_config['use-xmp-metadata']],
|
2021-09-16 19:51:39 +02:00
|
|
|
verbose=args.verbose)
|
2021-08-23 06:00:54 +02:00
|
|
|
|
2021-08-16 02:50:08 +02:00
|
|
|
# Start Application Server
|
|
|
|
uvicorn.run(app)
|