2021-08-17 12:59:58 +02:00
|
|
|
# Standard Packages
|
|
|
|
import sys
|
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-09-30 13:12:14 +02:00
|
|
|
from src.search_type import asymmetric, symmetric_ledger, image_search
|
|
|
|
from src.utils.helpers import get_from_dict
|
|
|
|
from src.utils.cli import cli
|
|
|
|
from src.utils.config import SearchType, SearchModels, TextSearchConfig, ImageSearchConfig, SearchConfig
|
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 11:04:04 +02:00
|
|
|
search_config = SearchConfig()
|
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 11:04:04 +02:00
|
|
|
if (t == SearchType.Notes or t == None) and model.notes_search:
|
2021-08-16 02:50:08 +02:00
|
|
|
# query notes
|
2021-09-30 11:04:04 +02:00
|
|
|
hits = asymmetric.query(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 11:04:04 +02:00
|
|
|
if (t == SearchType.Music or t == None) and model.music_search:
|
2021-08-29 12:07:36 +02:00
|
|
|
# query music library
|
2021-09-30 11:04:04 +02:00
|
|
|
hits = asymmetric.query(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 11:04:04 +02:00
|
|
|
if (t == SearchType.Ledger or t == None) and model.ledger_search:
|
2021-08-22 12:16:57 +02:00
|
|
|
# query transactions
|
2021-09-30 11:04:04 +02:00
|
|
|
hits = symmetric_ledger.query(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 11:04:04 +02:00
|
|
|
if (t == SearchType.Image or t == None) and model.image_search:
|
2021-08-23 06:00:54 +02:00
|
|
|
# query transactions
|
2021-09-30 11:04:04 +02:00
|
|
|
hits = image_search.query(user_query, results_count, model.image_search)
|
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-09-30 11:04:04 +02:00
|
|
|
search_config.image.input_directory,
|
2021-08-23 06:00:54 +02:00
|
|
|
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 11:04:04 +02:00
|
|
|
if (t == SearchType.Notes or t == None) and search_config.notes:
|
2021-08-17 13:36:48 +02:00
|
|
|
# Extract Entries, Generate Embeddings
|
2021-09-30 11:04:04 +02:00
|
|
|
model.notes_search = asymmetric.setup(search_config.notes, regenerate=True)
|
|
|
|
|
|
|
|
if (t == SearchType.Music or t == None) and search_config.music:
|
2021-08-29 12:07:36 +02:00
|
|
|
# Extract Entries, Generate Song Embeddings
|
2021-09-30 11:04:04 +02:00
|
|
|
model.music_search = asymmetric.setup(search_config.music, regenerate=True)
|
|
|
|
|
|
|
|
if (t == SearchType.Ledger or t == None) and search_config.ledger:
|
2021-08-22 12:16:57 +02:00
|
|
|
# Extract Entries, Generate Embeddings
|
2021-09-30 11:04:04 +02:00
|
|
|
model.ledger_search = symmetric_ledger.setup(search_config.ledger, regenerate=True)
|
|
|
|
|
|
|
|
if (t == SearchType.Image or t == None) and search_config.image:
|
2021-08-23 06:00:54 +02:00
|
|
|
# Extract Images, Generate Embeddings
|
2021-09-30 11:04:04 +02:00
|
|
|
model.image_search = image_search.setup(search_config.image, regenerate=True)
|
2021-08-23 06:00:54 +02:00
|
|
|
|
2021-08-17 08:47:33 +02:00
|
|
|
return {'status': 'ok', 'message': 'regeneration completed'}
|
2021-08-17 03:52:38 +02:00
|
|
|
|
|
|
|
|
2021-09-30 11:04:04 +02:00
|
|
|
def initialize_search(config, regenerate, verbose):
|
|
|
|
model = SearchModels()
|
|
|
|
search_config = SearchConfig()
|
2021-08-22 03:47:55 +02:00
|
|
|
|
2021-08-22 12:16:57 +02:00
|
|
|
# Initialize Org Notes Search
|
2021-09-30 11:04:04 +02:00
|
|
|
search_config.notes = TextSearchConfig.create_from_dictionary(config, ('content-type', 'org'), verbose)
|
|
|
|
if search_config.notes:
|
|
|
|
model.notes_search = asymmetric.setup(search_config.notes, regenerate=regenerate)
|
2021-08-16 02:50:08 +02:00
|
|
|
|
2021-08-29 12:07:36 +02:00
|
|
|
# Initialize Org Music Search
|
2021-09-30 11:04:04 +02:00
|
|
|
search_config.music = TextSearchConfig.create_from_dictionary(config, ('content-type', 'music'), verbose)
|
|
|
|
if search_config.music:
|
|
|
|
model.music_search = asymmetric.setup(search_config.music, regenerate=regenerate)
|
2021-08-29 12:07:36 +02:00
|
|
|
|
2021-08-22 12:16:57 +02:00
|
|
|
# Initialize Ledger Search
|
2021-09-30 11:04:04 +02:00
|
|
|
search_config.ledger = TextSearchConfig.create_from_dictionary(config, ('content-type', 'ledger'), verbose)
|
|
|
|
if search_config.ledger:
|
|
|
|
model.ledger_search = symmetric_ledger.setup(search_config.ledger, regenerate=regenerate)
|
2021-08-16 02:50:08 +02:00
|
|
|
|
2021-08-23 06:00:54 +02:00
|
|
|
# Initialize Image Search
|
2021-09-30 11:04:04 +02:00
|
|
|
search_config.image = ImageSearchConfig.create_from_dictionary(config, ('content-type', 'image'), verbose)
|
|
|
|
if search_config.image:
|
|
|
|
model.image_search = image_search.setup(search_config.image, regenerate=regenerate)
|
|
|
|
|
|
|
|
return model, search_config
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# Load config from CLI
|
|
|
|
args = cli(sys.argv[1:])
|
|
|
|
|
|
|
|
# Initialize Search from Config
|
|
|
|
model, search_config = initialize_search(args.config, args.regenerate, args.verbose)
|
2021-08-23 06:00:54 +02:00
|
|
|
|
2021-08-16 02:50:08 +02:00
|
|
|
# Start Application Server
|
2021-10-03 01:16:33 +02:00
|
|
|
if args.socket:
|
|
|
|
uvicorn.run(app, proxy_headers=True, uds=args.socket)
|
|
|
|
else:
|
|
|
|
uvicorn.run(app, host=args.host, port=args.port)
|