2021-08-17 12:59:58 +02:00
|
|
|
# Standard Packages
|
2022-08-03 13:00:30 +02:00
|
|
|
import sys, json, yaml
|
2022-07-26 19:03:53 +02:00
|
|
|
import time
|
2021-08-16 02:50:08 +02:00
|
|
|
from typing import Optional
|
2022-08-02 23:02:39 +02:00
|
|
|
from pathlib import Path
|
2022-08-03 13:46:44 +02:00
|
|
|
from functools import lru_cache
|
2022-08-05 22:49:48 +02:00
|
|
|
import webbrowser
|
2021-08-17 12:59:58 +02:00
|
|
|
|
|
|
|
# External Packages
|
|
|
|
import uvicorn
|
2022-06-29 22:59:57 +02:00
|
|
|
import torch
|
2021-11-28 18:05:04 +01:00
|
|
|
from fastapi import FastAPI, Request
|
2022-07-15 20:07:39 +02:00
|
|
|
from fastapi.responses import HTMLResponse, FileResponse
|
2021-11-27 16:49:33 +01:00
|
|
|
from fastapi.staticfiles import StaticFiles
|
2021-11-26 20:51:11 +01:00
|
|
|
from fastapi.templating import Jinja2Templates
|
2022-08-05 22:49:48 +02:00
|
|
|
from PyQt6 import QtCore, QtGui, QtWidgets
|
2021-08-17 12:59:58 +02:00
|
|
|
|
|
|
|
# Internal Packages
|
2022-07-21 16:05:43 +02:00
|
|
|
from src.search_type import image_search, text_search
|
|
|
|
from src.processor.org_mode.org_to_jsonl import org_to_jsonl
|
|
|
|
from src.processor.ledger.beancount_to_jsonl import beancount_to_jsonl
|
2022-07-21 18:22:24 +02:00
|
|
|
from src.processor.markdown.markdown_to_jsonl import markdown_to_jsonl
|
2021-11-28 08:43:39 +01:00
|
|
|
from src.utils.helpers import get_absolute_path, get_from_dict
|
2021-09-30 13:12:14 +02:00
|
|
|
from src.utils.cli import cli
|
2021-12-09 14:50:38 +01:00
|
|
|
from src.utils.config import SearchType, SearchModels, ProcessorConfigModel, ConversationProcessorConfigModel
|
|
|
|
from src.utils.rawconfig import FullConfig
|
2022-02-28 03:01:33 +01:00
|
|
|
from src.processor.conversation.gpt import converse, extract_search_type, message_to_log, message_to_prompt, understand, summarize
|
2022-07-26 20:47:26 +02:00
|
|
|
from src.search_filter.explicit_filter import ExplicitFilter
|
|
|
|
from src.search_filter.date_filter import DateFilter
|
2021-08-17 12:59:58 +02:00
|
|
|
|
2021-09-30 05:24:27 +02:00
|
|
|
# Application Global State
|
2021-12-11 20:13:14 +01:00
|
|
|
config = FullConfig()
|
2021-09-30 05:24:27 +02:00
|
|
|
model = SearchModels()
|
2021-12-09 14:50:38 +01:00
|
|
|
processor_config = ProcessorConfigModel()
|
2021-11-28 18:34:40 +01:00
|
|
|
config_file = ""
|
2021-12-04 17:43:48 +01:00
|
|
|
verbose = 0
|
2021-08-16 02:50:08 +02:00
|
|
|
app = FastAPI()
|
2022-08-02 23:02:39 +02:00
|
|
|
this_directory = Path(__file__).parent
|
|
|
|
web_directory = this_directory / 'interface/web/'
|
2021-08-16 02:50:08 +02:00
|
|
|
|
2022-07-15 20:07:39 +02:00
|
|
|
app.mount("/static", StaticFiles(directory=web_directory), name="static")
|
2022-07-31 23:47:41 +02:00
|
|
|
templates = Jinja2Templates(directory=web_directory)
|
2021-11-26 20:51:11 +01:00
|
|
|
|
2022-08-03 13:00:30 +02:00
|
|
|
|
|
|
|
# Controllers
|
2022-07-15 20:07:39 +02:00
|
|
|
@app.get("/", response_class=FileResponse)
|
|
|
|
def index():
|
2022-08-02 23:02:39 +02:00
|
|
|
return FileResponse(web_directory / "index.html")
|
2022-07-15 20:07:39 +02:00
|
|
|
|
2022-07-31 23:47:41 +02:00
|
|
|
@app.get('/config', response_class=HTMLResponse)
|
2022-08-03 13:00:30 +02:00
|
|
|
def config(request: Request):
|
2021-11-26 20:51:11 +01:00
|
|
|
return templates.TemplateResponse("config.html", context={'request': request})
|
2021-08-16 02:50:08 +02:00
|
|
|
|
2022-07-31 23:47:41 +02:00
|
|
|
@app.get('/config/data', response_model=FullConfig)
|
2021-12-11 20:13:14 +01:00
|
|
|
def config_data():
|
2021-11-28 01:17:15 +01:00
|
|
|
return config
|
|
|
|
|
2022-07-31 23:47:41 +02:00
|
|
|
@app.post('/config/data')
|
2021-12-11 20:13:14 +01:00
|
|
|
async def config_data(updated_config: FullConfig):
|
2021-11-28 19:28:22 +01:00
|
|
|
global config
|
|
|
|
config = updated_config
|
2021-11-28 18:34:40 +01:00
|
|
|
with open(config_file, 'w') as outfile:
|
2021-11-28 19:28:22 +01:00
|
|
|
yaml.dump(yaml.safe_load(config.json(by_alias=True)), outfile)
|
2021-11-28 18:26:07 +01:00
|
|
|
outfile.close()
|
2021-11-28 19:28:22 +01:00
|
|
|
return config
|
2021-08-16 02:50:08 +02:00
|
|
|
|
|
|
|
@app.get('/search')
|
2022-08-03 13:46:44 +02:00
|
|
|
@lru_cache(maxsize=100)
|
2022-07-26 20:56:36 +02:00
|
|
|
def search(q: str, n: Optional[int] = 5, t: Optional[SearchType] = None, r: Optional[bool] = False):
|
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 {}
|
|
|
|
|
2022-06-29 23:32:56 +02:00
|
|
|
device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
|
2021-08-16 02:50:08 +02:00
|
|
|
user_query = q
|
|
|
|
results_count = n
|
2022-07-26 19:03:53 +02:00
|
|
|
results = {}
|
2021-08-16 02:50:08 +02:00
|
|
|
|
2022-07-21 19:57:57 +02:00
|
|
|
if (t == SearchType.Org or t == None) and model.orgmode_search:
|
|
|
|
# query org-mode notes
|
2022-07-26 19:03:53 +02:00
|
|
|
query_start = time.time()
|
2022-07-26 20:56:36 +02:00
|
|
|
hits, entries = text_search.query(user_query, model.orgmode_search, rank_results=r, device=device, filters=[DateFilter(), ExplicitFilter()], verbose=verbose)
|
2022-07-26 19:03:53 +02:00
|
|
|
query_end = time.time()
|
2021-08-16 02:50:08 +02:00
|
|
|
|
|
|
|
# collate and return results
|
2022-07-26 19:03:53 +02:00
|
|
|
collate_start = time.time()
|
|
|
|
results = text_search.collate_results(hits, entries, results_count)
|
|
|
|
collate_end = time.time()
|
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
|
2022-07-26 19:03:53 +02:00
|
|
|
query_start = time.time()
|
2022-07-26 20:56:36 +02:00
|
|
|
hits, entries = text_search.query(user_query, model.music_search, rank_results=r, device=device, filters=[DateFilter(), ExplicitFilter()], verbose=verbose)
|
2022-07-26 19:03:53 +02:00
|
|
|
query_end = time.time()
|
2021-08-29 12:07:36 +02:00
|
|
|
|
|
|
|
# collate and return results
|
2022-07-26 19:03:53 +02:00
|
|
|
collate_start = time.time()
|
|
|
|
results = text_search.collate_results(hits, entries, results_count)
|
|
|
|
collate_end = time.time()
|
2021-08-29 12:07:36 +02:00
|
|
|
|
2022-07-21 19:57:57 +02:00
|
|
|
if (t == SearchType.Markdown or t == None) and model.orgmode_search:
|
2022-07-21 18:22:24 +02:00
|
|
|
# query markdown files
|
2022-07-26 19:03:53 +02:00
|
|
|
query_start = time.time()
|
2022-07-26 20:56:36 +02:00
|
|
|
hits, entries = text_search.query(user_query, model.markdown_search, rank_results=r, device=device, filters=[ExplicitFilter(), DateFilter()], verbose=verbose)
|
2022-07-26 19:03:53 +02:00
|
|
|
query_end = time.time()
|
2022-07-21 18:22:24 +02:00
|
|
|
|
|
|
|
# collate and return results
|
2022-07-26 19:03:53 +02:00
|
|
|
collate_start = time.time()
|
|
|
|
results = text_search.collate_results(hits, entries, results_count)
|
|
|
|
collate_end = time.time()
|
2022-07-21 18:22:24 +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
|
2022-07-26 19:03:53 +02:00
|
|
|
query_start = time.time()
|
2022-07-26 20:56:36 +02:00
|
|
|
hits, entries = text_search.query(user_query, model.ledger_search, rank_results=r, device=device, filters=[ExplicitFilter(), DateFilter()], verbose=verbose)
|
2022-07-26 19:03:53 +02:00
|
|
|
query_end = time.time()
|
2021-08-22 12:16:57 +02:00
|
|
|
|
|
|
|
# collate and return results
|
2022-07-26 19:03:53 +02:00
|
|
|
collate_start = time.time()
|
|
|
|
results = text_search.collate_results(hits, entries, results_count)
|
|
|
|
collate_end = time.time()
|
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:
|
2022-07-21 18:22:24 +02:00
|
|
|
# query images
|
2022-07-26 19:03:53 +02:00
|
|
|
query_start = time.time()
|
2022-07-28 02:32:34 +02:00
|
|
|
hits = image_search.query(user_query, results_count, model.image_search)
|
2022-08-02 23:02:39 +02:00
|
|
|
output_directory = web_directory / 'images'
|
2022-07-26 19:03:53 +02:00
|
|
|
query_end = time.time()
|
2021-08-23 06:00:54 +02:00
|
|
|
|
|
|
|
# collate and return results
|
2022-07-26 19:03:53 +02:00
|
|
|
collate_start = time.time()
|
|
|
|
results = image_search.collate_results(
|
2021-08-23 06:00:54 +02:00
|
|
|
hits,
|
2022-07-15 21:20:19 +02:00
|
|
|
image_names=model.image_search.image_names,
|
|
|
|
output_directory=output_directory,
|
2022-07-28 18:45:12 +02:00
|
|
|
image_files_url='/static/images',
|
2022-07-15 21:20:19 +02:00
|
|
|
count=results_count)
|
2022-07-26 19:03:53 +02:00
|
|
|
collate_end = time.time()
|
2021-08-23 06:00:54 +02:00
|
|
|
|
2022-07-26 19:03:53 +02:00
|
|
|
if verbose > 1:
|
|
|
|
print(f"Query took {query_end - query_start:.3f} seconds")
|
|
|
|
print(f"Collating results took {collate_end - collate_start:.3f} seconds")
|
|
|
|
|
|
|
|
return results
|
2021-08-16 02:50:08 +02:00
|
|
|
|
|
|
|
|
2022-06-29 20:46:17 +02:00
|
|
|
@app.get('/reload')
|
2022-08-05 02:51:34 +02:00
|
|
|
def reload(t: Optional[SearchType] = None):
|
2022-06-29 20:46:17 +02:00
|
|
|
global model
|
2022-06-29 23:32:56 +02:00
|
|
|
device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
|
2022-06-29 22:59:57 +02:00
|
|
|
model = initialize_search(config, regenerate=False, t=t, device=device)
|
2022-06-29 20:46:17 +02:00
|
|
|
return {'status': 'ok', 'message': 'reload completed'}
|
|
|
|
|
|
|
|
|
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):
|
2022-06-16 23:49:06 +02:00
|
|
|
global model
|
2022-06-29 23:32:56 +02:00
|
|
|
device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
|
2022-06-29 22:59:57 +02:00
|
|
|
model = initialize_search(config, regenerate=True, t=t, device=device)
|
2021-08-17 08:47:33 +02:00
|
|
|
return {'status': 'ok', 'message': 'regeneration completed'}
|
2021-08-17 03:52:38 +02:00
|
|
|
|
|
|
|
|
2022-02-28 03:01:33 +01:00
|
|
|
@app.get('/beta/search')
|
|
|
|
def search_beta(q: str, n: Optional[int] = 1):
|
|
|
|
# Extract Search Type using GPT
|
|
|
|
metadata = extract_search_type(q, api_key=processor_config.conversation.openai_api_key, verbose=verbose)
|
|
|
|
search_type = get_from_dict(metadata, "search-type")
|
|
|
|
|
|
|
|
# Search
|
|
|
|
search_results = search(q, n=n, t=SearchType(search_type))
|
|
|
|
|
|
|
|
# Return response
|
|
|
|
return {'status': 'ok', 'result': search_results, 'type': search_type}
|
|
|
|
|
|
|
|
|
2021-11-26 20:57:46 +01:00
|
|
|
@app.get('/chat')
|
|
|
|
def chat(q: str):
|
|
|
|
# Load Conversation History
|
2021-12-07 22:04:52 +01:00
|
|
|
chat_session = processor_config.conversation.chat_session
|
2021-11-27 19:34:39 +01:00
|
|
|
meta_log = processor_config.conversation.meta_log
|
2021-11-26 20:57:46 +01:00
|
|
|
|
|
|
|
# Converse with OpenAI GPT
|
2022-01-12 15:06:32 +01:00
|
|
|
metadata = understand(q, api_key=processor_config.conversation.openai_api_key, verbose=verbose)
|
|
|
|
if verbose > 1:
|
|
|
|
print(f'Understood: {get_from_dict(metadata, "intent")}')
|
|
|
|
|
2021-11-28 08:43:39 +01:00
|
|
|
if get_from_dict(metadata, "intent", "memory-type") == "notes":
|
|
|
|
query = get_from_dict(metadata, "intent", "query")
|
2022-07-21 19:57:57 +02:00
|
|
|
result_list = search(query, n=1, t=SearchType.Org)
|
2022-07-20 18:33:27 +02:00
|
|
|
collated_result = "\n".join([item["entry"] for item in result_list])
|
2022-01-12 15:06:32 +01:00
|
|
|
if verbose > 1:
|
|
|
|
print(f'Semantically Similar Notes:\n{collated_result}')
|
2021-12-07 22:04:52 +01:00
|
|
|
gpt_response = summarize(collated_result, summary_type="notes", user_query=q, api_key=processor_config.conversation.openai_api_key)
|
2021-11-28 08:43:39 +01:00
|
|
|
else:
|
2021-12-07 22:04:52 +01:00
|
|
|
gpt_response = converse(q, chat_session, api_key=processor_config.conversation.openai_api_key)
|
2021-11-26 20:57:46 +01:00
|
|
|
|
|
|
|
# Update Conversation History
|
2021-12-07 22:04:52 +01:00
|
|
|
processor_config.conversation.chat_session = message_to_prompt(q, chat_session, gpt_message=gpt_response)
|
|
|
|
processor_config.conversation.meta_log['chat'] = message_to_log(q, metadata, gpt_response, meta_log.get('chat', []))
|
2021-11-26 20:57:46 +01:00
|
|
|
|
|
|
|
return {'status': 'ok', 'response': gpt_response}
|
|
|
|
|
|
|
|
|
2022-06-29 23:32:56 +02:00
|
|
|
def initialize_search(config: FullConfig, regenerate: bool, t: SearchType = None, device=torch.device("cpu")):
|
2021-08-22 12:16:57 +02:00
|
|
|
# Initialize Org Notes Search
|
2022-07-21 19:57:57 +02:00
|
|
|
if (t == SearchType.Org or t == None) and config.content_type.org:
|
2021-12-04 17:43:48 +01:00
|
|
|
# Extract Entries, Generate Notes Embeddings
|
2022-07-21 19:57:57 +02:00
|
|
|
model.orgmode_search = text_search.setup(org_to_jsonl, config.content_type.org, search_config=config.search_type.asymmetric, regenerate=regenerate, device=device, verbose=verbose)
|
2021-08-16 02:50:08 +02:00
|
|
|
|
2021-08-29 12:07:36 +02:00
|
|
|
# Initialize Org Music Search
|
2021-12-04 17:43:48 +01:00
|
|
|
if (t == SearchType.Music or t == None) and config.content_type.music:
|
|
|
|
# Extract Entries, Generate Music Embeddings
|
2022-07-21 16:05:43 +02:00
|
|
|
model.music_search = text_search.setup(org_to_jsonl, config.content_type.music, search_config=config.search_type.asymmetric, regenerate=regenerate, device=device, verbose=verbose)
|
2021-08-29 12:07:36 +02:00
|
|
|
|
2022-07-21 18:22:24 +02:00
|
|
|
# Initialize Markdown Search
|
|
|
|
if (t == SearchType.Markdown or t == None) and config.content_type.markdown:
|
|
|
|
# Extract Entries, Generate Markdown Embeddings
|
|
|
|
model.markdown_search = text_search.setup(markdown_to_jsonl, config.content_type.markdown, search_config=config.search_type.asymmetric, regenerate=regenerate, device=device, verbose=verbose)
|
|
|
|
|
2021-08-22 12:16:57 +02:00
|
|
|
# Initialize Ledger Search
|
2021-12-04 17:43:48 +01:00
|
|
|
if (t == SearchType.Ledger or t == None) and config.content_type.ledger:
|
|
|
|
# Extract Entries, Generate Ledger Embeddings
|
2022-07-21 16:05:43 +02:00
|
|
|
model.ledger_search = text_search.setup(beancount_to_jsonl, config.content_type.ledger, search_config=config.search_type.symmetric, regenerate=regenerate, verbose=verbose)
|
2021-08-16 02:50:08 +02:00
|
|
|
|
2021-08-23 06:00:54 +02:00
|
|
|
# Initialize Image Search
|
2021-12-04 17:43:48 +01:00
|
|
|
if (t == SearchType.Image or t == None) and config.content_type.image:
|
|
|
|
# Extract Entries, Generate Image Embeddings
|
2022-01-14 23:09:18 +01:00
|
|
|
model.image_search = image_search.setup(config.content_type.image, search_config=config.search_type.image, regenerate=regenerate, verbose=verbose)
|
2021-09-30 11:04:04 +02:00
|
|
|
|
2021-12-04 17:43:48 +01:00
|
|
|
return model
|
2021-09-30 11:04:04 +02:00
|
|
|
|
|
|
|
|
2021-12-09 14:50:38 +01:00
|
|
|
def initialize_processor(config: FullConfig):
|
2021-12-04 16:11:00 +01:00
|
|
|
if not config.processor:
|
|
|
|
return
|
2021-12-20 08:56:35 +01:00
|
|
|
|
2021-12-09 14:50:38 +01:00
|
|
|
processor_config = ProcessorConfigModel()
|
2021-09-30 11:04:04 +02:00
|
|
|
|
2021-11-26 20:57:46 +01:00
|
|
|
# Initialize Conversation Processor
|
2021-12-09 14:50:38 +01:00
|
|
|
processor_config.conversation = ConversationProcessorConfigModel(config.processor.conversation, verbose)
|
2021-11-26 20:57:46 +01:00
|
|
|
|
|
|
|
conversation_logfile = processor_config.conversation.conversation_logfile
|
|
|
|
if processor_config.conversation.verbose:
|
2021-11-27 19:34:39 +01:00
|
|
|
print('INFO:\tLoading conversation logs from disk...')
|
2021-11-26 20:57:46 +01:00
|
|
|
|
|
|
|
if conversation_logfile.expanduser().absolute().is_file():
|
2021-11-27 19:34:39 +01:00
|
|
|
# Load Metadata Logs from Conversation Logfile
|
2021-11-26 20:57:46 +01:00
|
|
|
with open(get_absolute_path(conversation_logfile), 'r') as f:
|
2021-11-27 19:34:39 +01:00
|
|
|
processor_config.conversation.meta_log = json.load(f)
|
|
|
|
|
|
|
|
print('INFO:\tConversation logs loaded from disk.')
|
2021-11-26 20:57:46 +01:00
|
|
|
else:
|
2021-11-27 19:34:39 +01:00
|
|
|
# Initialize Conversation Logs
|
2021-12-07 22:04:52 +01:00
|
|
|
processor_config.conversation.meta_log = {}
|
|
|
|
processor_config.conversation.chat_session = ""
|
2021-11-26 20:57:46 +01:00
|
|
|
|
|
|
|
return processor_config
|
|
|
|
|
|
|
|
|
|
|
|
@app.on_event('shutdown')
|
|
|
|
def shutdown_event():
|
2021-11-27 19:34:39 +01:00
|
|
|
# No need to create empty log file
|
2022-08-05 03:57:36 +02:00
|
|
|
if not (processor_config and processor_config.conversation and processor_config.conversation.meta_log):
|
2021-11-27 19:34:39 +01:00
|
|
|
return
|
|
|
|
elif processor_config.conversation.verbose:
|
|
|
|
print('INFO:\tSaving conversation logs to disk...')
|
2021-11-26 20:57:46 +01:00
|
|
|
|
2021-11-28 08:43:39 +01:00
|
|
|
# Summarize Conversation Logs for this Session
|
2021-12-07 22:04:52 +01:00
|
|
|
chat_session = processor_config.conversation.chat_session
|
|
|
|
openai_api_key = processor_config.conversation.openai_api_key
|
|
|
|
conversation_log = processor_config.conversation.meta_log
|
|
|
|
session = {
|
|
|
|
"summary": summarize(chat_session, summary_type="chat", api_key=openai_api_key),
|
|
|
|
"session-start": conversation_log.get("session", [{"session-end": 0}])[-1]["session-end"],
|
|
|
|
"session-end": len(conversation_log["chat"])
|
|
|
|
}
|
|
|
|
if 'session' in conversation_log:
|
|
|
|
conversation_log['session'].append(session)
|
|
|
|
else:
|
|
|
|
conversation_log['session'] = [session]
|
2021-11-28 08:43:39 +01:00
|
|
|
|
2021-11-27 19:34:39 +01:00
|
|
|
# Save Conversation Metadata Logs to Disk
|
2021-11-26 20:57:46 +01:00
|
|
|
conversation_logfile = get_absolute_path(processor_config.conversation.conversation_logfile)
|
|
|
|
with open(conversation_logfile, "w+", encoding='utf-8') as logfile:
|
2021-12-07 22:04:52 +01:00
|
|
|
json.dump(conversation_log, logfile)
|
2021-11-26 20:57:46 +01:00
|
|
|
|
2021-11-27 19:34:39 +01:00
|
|
|
print('INFO:\tConversation logs saved to disk.')
|
2021-11-26 20:57:46 +01:00
|
|
|
|
|
|
|
|
2022-08-05 22:49:48 +02:00
|
|
|
def setup_server():
|
2021-09-30 11:04:04 +02:00
|
|
|
# Load config from CLI
|
|
|
|
args = cli(sys.argv[1:])
|
|
|
|
|
2021-11-28 18:34:40 +01:00
|
|
|
# Stores the file path to the config file.
|
2022-08-02 19:13:14 +02:00
|
|
|
global config_file
|
2021-11-28 18:34:40 +01:00
|
|
|
config_file = args.config_file
|
2021-09-30 11:04:04 +02:00
|
|
|
|
2021-11-28 18:34:40 +01:00
|
|
|
# Store the raw config data.
|
2022-08-02 19:13:14 +02:00
|
|
|
global config
|
2021-11-27 16:49:33 +01:00
|
|
|
config = args.config
|
|
|
|
|
2022-08-05 00:24:21 +02:00
|
|
|
# Store the verbose flag
|
|
|
|
global verbose
|
|
|
|
verbose = args.verbose
|
|
|
|
|
2022-06-29 23:32:56 +02:00
|
|
|
# Set device to GPU if available
|
|
|
|
device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
|
|
|
|
|
2021-12-04 17:43:48 +01:00
|
|
|
# Initialize the search model from Config
|
2022-08-02 19:13:14 +02:00
|
|
|
global model
|
2022-06-29 23:32:56 +02:00
|
|
|
model = initialize_search(args.config, args.regenerate, device=device)
|
2021-08-23 06:00:54 +02:00
|
|
|
|
2021-11-26 20:57:46 +01:00
|
|
|
# Initialize Processor from Config
|
2022-08-02 19:13:14 +02:00
|
|
|
global processor_config
|
2021-12-09 14:50:38 +01:00
|
|
|
processor_config = initialize_processor(args.config)
|
2021-11-26 20:57:46 +01:00
|
|
|
|
2022-08-05 22:49:48 +02:00
|
|
|
return args.host, args.port, args.socket
|
|
|
|
|
|
|
|
|
|
|
|
def run():
|
|
|
|
# Setup Application Server
|
|
|
|
host, port, socket = setup_server()
|
|
|
|
|
|
|
|
# Setup GUI
|
|
|
|
gui = QtWidgets.QApplication([])
|
|
|
|
gui.setQuitOnLastWindowClosed(False)
|
|
|
|
tray = create_system_tray()
|
|
|
|
|
2021-08-16 02:50:08 +02:00
|
|
|
# Start Application Server
|
2022-08-05 22:49:48 +02:00
|
|
|
server = ServerThread(app, host, port, socket)
|
|
|
|
server.start()
|
|
|
|
gui.aboutToQuit.connect(server.terminate)
|
|
|
|
|
|
|
|
# Start the GUI
|
|
|
|
tray.show()
|
|
|
|
gui.exec()
|
|
|
|
|
|
|
|
|
|
|
|
class ServerThread(QtCore.QThread):
|
|
|
|
def __init__(self, app, host=None, port=None, socket=None):
|
|
|
|
super(ServerThread, self).__init__()
|
|
|
|
self.app = app
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
self.socket = socket
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.wait()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
if self.socket:
|
|
|
|
uvicorn.run(app, proxy_headers=True, uds=self.socket)
|
|
|
|
else:
|
|
|
|
uvicorn.run(app, host=self.host, port=self.port)
|
|
|
|
|
|
|
|
|
|
|
|
def create_system_tray():
|
|
|
|
"""Create System Tray with Menu
|
|
|
|
Menu Actions should contain
|
|
|
|
1. option to open search page at localhost:8000/
|
|
|
|
2. option to open config page at localhost:8000/config
|
|
|
|
3. to quit
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Create the system tray with icon
|
|
|
|
icon_path = web_directory / 'assets/icons/favicon-144x144.png'
|
|
|
|
icon = QtGui.QIcon(f'{icon_path.absolute()}')
|
|
|
|
tray = QtWidgets.QSystemTrayIcon(icon)
|
|
|
|
tray.setVisible(True)
|
|
|
|
|
|
|
|
# Create the menu and menu actions
|
|
|
|
menu = QtWidgets.QMenu()
|
|
|
|
menu_actions = [
|
|
|
|
('Search', lambda: webbrowser.open('http://localhost:8000/')),
|
|
|
|
('Configure', lambda: webbrowser.open('http://localhost:8000/config')),
|
|
|
|
('Quit', quit),
|
|
|
|
]
|
|
|
|
|
|
|
|
# Add the menu actions to the menu
|
|
|
|
for action_text, action_function in menu_actions:
|
|
|
|
menu_action = QtGui.QAction(action_text, menu)
|
|
|
|
menu_action.triggered.connect(action_function)
|
|
|
|
menu.addAction(menu_action)
|
|
|
|
|
|
|
|
# Add the menu to the system tray
|
|
|
|
tray.setContextMenu(menu)
|
|
|
|
|
|
|
|
return tray
|
2022-08-02 19:13:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
run()
|