2021-08-17 12:59:58 +02:00
|
|
|
# Standard Packages
|
2022-08-18 22:14:39 +02:00
|
|
|
import os
|
2022-08-12 20:02:18 +02:00
|
|
|
import signal
|
2022-08-06 01:37:52 +02:00
|
|
|
import sys
|
2022-09-03 13:43:32 +02:00
|
|
|
import logging
|
2022-08-18 22:14:39 +02:00
|
|
|
from platform import system
|
2021-08-17 12:59:58 +02:00
|
|
|
|
|
|
|
# External Packages
|
|
|
|
import uvicorn
|
2022-08-06 01:37:52 +02:00
|
|
|
from fastapi import FastAPI
|
2021-11-27 16:49:33 +01:00
|
|
|
from fastapi.staticfiles import StaticFiles
|
2022-08-09 21:12:29 +02:00
|
|
|
from PyQt6 import QtWidgets
|
2022-08-12 20:02:18 +02:00
|
|
|
from PyQt6.QtCore import QThread, QTimer
|
2021-08-17 12:59:58 +02:00
|
|
|
|
|
|
|
# Internal Packages
|
2022-08-06 02:20:04 +02:00
|
|
|
from src.configure import configure_server
|
2022-08-06 01:37:52 +02:00
|
|
|
from src.router import router
|
2022-08-10 23:10:21 +02:00
|
|
|
from src.utils import constants, state
|
2022-08-09 16:05:27 +02:00
|
|
|
from src.utils.cli import cli
|
2022-08-13 00:39:46 +02:00
|
|
|
from src.interface.desktop.main_window import MainWindow
|
2022-08-09 21:12:29 +02:00
|
|
|
from src.interface.desktop.system_tray import create_system_tray
|
2021-08-17 12:59:58 +02:00
|
|
|
|
2022-01-12 15:06:32 +01:00
|
|
|
|
2022-08-06 01:37:52 +02:00
|
|
|
# Initialize the Application Server
|
|
|
|
app = FastAPI()
|
|
|
|
app.mount("/static", StaticFiles(directory=constants.web_directory), name="static")
|
|
|
|
app.include_router(router)
|
2022-08-05 22:49:48 +02:00
|
|
|
|
2022-09-03 13:43:32 +02:00
|
|
|
logger = logging.getLogger('src')
|
|
|
|
|
|
|
|
|
|
|
|
class CustomFormatter(logging.Formatter):
|
|
|
|
|
|
|
|
blue = "\x1b[1;34m"
|
|
|
|
green = "\x1b[1;32m"
|
|
|
|
grey = "\x1b[38;20m"
|
|
|
|
yellow = "\x1b[33;20m"
|
|
|
|
red = "\x1b[31;20m"
|
|
|
|
bold_red = "\x1b[31;1m"
|
|
|
|
reset = "\x1b[0m"
|
|
|
|
format = "%(levelname)s: %(asctime)s: %(name)s | %(message)s"
|
|
|
|
|
|
|
|
FORMATS = {
|
|
|
|
logging.DEBUG: blue + format + reset,
|
|
|
|
logging.INFO: green + format + reset,
|
|
|
|
logging.WARNING: yellow + format + reset,
|
|
|
|
logging.ERROR: red + format + reset,
|
|
|
|
logging.CRITICAL: bold_red + format + reset
|
|
|
|
}
|
|
|
|
|
|
|
|
def format(self, record):
|
|
|
|
log_fmt = self.FORMATS.get(record.levelno)
|
|
|
|
formatter = logging.Formatter(log_fmt)
|
|
|
|
return formatter.format(record)
|
|
|
|
|
2022-08-05 22:49:48 +02:00
|
|
|
|
|
|
|
def run():
|
2022-08-18 22:14:39 +02:00
|
|
|
# Turn Tokenizers Parallelism Off. App does not support it.
|
|
|
|
os.environ["TOKENIZERS_PARALLELISM"] = 'false'
|
|
|
|
|
2022-08-10 21:42:32 +02:00
|
|
|
# Load config from CLI
|
2022-08-10 23:10:21 +02:00
|
|
|
state.cli_args = sys.argv[1:]
|
|
|
|
args = cli(state.cli_args)
|
2022-09-06 13:51:48 +02:00
|
|
|
set_state(args)
|
2022-09-03 13:43:32 +02:00
|
|
|
|
|
|
|
# Setup Logger
|
|
|
|
if args.verbose == 0:
|
|
|
|
logger.setLevel(logging.WARN)
|
|
|
|
elif args.verbose == 1:
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
elif args.verbose >= 2:
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
2022-09-06 13:51:48 +02:00
|
|
|
# Set Log Format
|
|
|
|
ch = logging.StreamHandler()
|
|
|
|
ch.setFormatter(CustomFormatter())
|
|
|
|
logger.addHandler(ch)
|
|
|
|
|
|
|
|
# Set Log File
|
|
|
|
fh = logging.FileHandler(state.config_file.parent / 'khoj.log')
|
|
|
|
fh.setLevel(logging.DEBUG)
|
|
|
|
logger.addHandler(fh)
|
|
|
|
|
2022-09-03 13:43:32 +02:00
|
|
|
logger.info("Starting Khoj...")
|
2022-08-10 21:42:32 +02:00
|
|
|
|
2022-08-11 19:59:57 +02:00
|
|
|
if args.no_gui:
|
|
|
|
# Start Server
|
|
|
|
configure_server(args, required=True)
|
|
|
|
start_server(app, host=args.host, port=args.port, socket=args.socket)
|
|
|
|
else:
|
|
|
|
# Setup GUI
|
|
|
|
gui = QtWidgets.QApplication([])
|
2022-08-13 00:39:46 +02:00
|
|
|
main_window = MainWindow(args.config_file)
|
2022-08-12 23:43:53 +02:00
|
|
|
|
|
|
|
# System tray is only available on Windows, MacOS.
|
|
|
|
# On Linux (Gnome) the System tray is not supported.
|
2022-08-13 00:39:46 +02:00
|
|
|
# Since only the Main Window is available
|
2022-08-12 23:43:53 +02:00
|
|
|
# Quitting it should quit the application
|
|
|
|
if system() in ['Windows', 'Darwin']:
|
|
|
|
gui.setQuitOnLastWindowClosed(False)
|
2022-08-13 00:39:46 +02:00
|
|
|
tray = create_system_tray(gui, main_window)
|
2022-08-12 23:43:53 +02:00
|
|
|
tray.show()
|
2022-08-11 19:59:57 +02:00
|
|
|
|
|
|
|
# Setup Server
|
|
|
|
configure_server(args, required=False)
|
|
|
|
server = ServerThread(app, args.host, args.port, args.socket)
|
|
|
|
|
2022-08-13 00:39:46 +02:00
|
|
|
# Show Main Window on First Run Experience or if on Linux
|
2022-08-12 23:43:53 +02:00
|
|
|
if args.config is None or system() not in ['Windows', 'Darwin']:
|
2022-08-13 00:39:46 +02:00
|
|
|
main_window.show()
|
2022-08-11 19:59:57 +02:00
|
|
|
|
2022-08-12 20:02:18 +02:00
|
|
|
# Setup Signal Handlers
|
|
|
|
signal.signal(signal.SIGINT, sigint_handler)
|
|
|
|
# Invoke python Interpreter every 500ms to handle signals
|
|
|
|
timer = QTimer()
|
|
|
|
timer.start(500)
|
|
|
|
timer.timeout.connect(lambda: None)
|
|
|
|
|
2022-08-11 19:59:57 +02:00
|
|
|
# Start Application
|
|
|
|
server.start()
|
|
|
|
gui.aboutToQuit.connect(server.terminate)
|
2022-08-18 19:50:25 +02:00
|
|
|
|
|
|
|
# Close Splash Screen if still open
|
|
|
|
if system() != 'Darwin':
|
|
|
|
try:
|
|
|
|
import pyi_splash
|
|
|
|
# Update the text on the splash screen
|
|
|
|
pyi_splash.update_text("Khoj setup complete")
|
|
|
|
# Close Splash Screen
|
|
|
|
pyi_splash.close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2022-08-11 19:59:57 +02:00
|
|
|
gui.exec()
|
2022-08-05 22:49:48 +02:00
|
|
|
|
|
|
|
|
2022-08-12 20:02:18 +02:00
|
|
|
def sigint_handler(*args):
|
|
|
|
print("\nShutting down Khoj...")
|
|
|
|
QtWidgets.QApplication.quit()
|
|
|
|
|
|
|
|
|
2022-08-10 23:13:14 +02:00
|
|
|
def set_state(args):
|
|
|
|
state.config_file = args.config_file
|
|
|
|
state.config = args.config
|
|
|
|
state.verbose = args.verbose
|
2022-08-15 22:07:22 +02:00
|
|
|
state.host = args.host
|
|
|
|
state.port = args.port
|
2022-08-10 23:13:14 +02:00
|
|
|
|
|
|
|
|
2022-08-11 19:59:57 +02:00
|
|
|
def start_server(app, host=None, port=None, socket=None):
|
|
|
|
if socket:
|
|
|
|
uvicorn.run(app, proxy_headers=True, uds=socket)
|
|
|
|
else:
|
|
|
|
uvicorn.run(app, host=host, port=port)
|
|
|
|
|
|
|
|
|
2022-08-06 14:18:28 +02:00
|
|
|
class ServerThread(QThread):
|
2022-08-05 22:49:48 +02:00
|
|
|
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):
|
2022-08-11 19:59:57 +02:00
|
|
|
start_server(self.app, self.host, self.port, self.socket)
|
2022-08-05 22:49:48 +02:00
|
|
|
|
|
|
|
|
2022-08-02 19:13:14 +02:00
|
|
|
if __name__ == '__main__':
|
2022-08-09 15:53:07 +02:00
|
|
|
run()
|