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-09-10 15:40:22 +02:00
|
|
|
import warnings
|
2022-08-18 22:14:39 +02:00
|
|
|
from platform import system
|
2021-08-17 12:59:58 +02:00
|
|
|
|
2022-09-10 15:40:22 +02:00
|
|
|
# Ignore non-actionable warnings
|
|
|
|
warnings.filterwarnings("ignore", message=r'snapshot_download.py has been made private', category=FutureWarning)
|
2022-09-14 20:29:48 +02:00
|
|
|
warnings.filterwarnings("ignore", message=r'legacy way to download files from the HF hub,', category=FutureWarning)
|
2022-09-10 15:40:22 +02:00
|
|
|
|
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
|
2023-01-01 20:22:35 +01:00
|
|
|
import schedule
|
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-10-08 12:16:08 +02:00
|
|
|
from src.routers.api import api
|
2022-09-14 20:22:20 +02:00
|
|
|
from src.routers.api_beta import api_beta
|
2022-10-19 12:45:57 +02:00
|
|
|
from src.routers.web_client import web_client
|
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-10-19 20:56:23 +02:00
|
|
|
from src.utils.helpers import CustomFormatter
|
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")
|
2022-10-08 12:16:08 +02:00
|
|
|
app.include_router(api, prefix="/api")
|
2022-09-14 20:22:20 +02:00
|
|
|
app.include_router(api_beta, prefix="/api/beta")
|
2022-10-19 12:45:57 +02:00
|
|
|
app.include_router(web_client)
|
2022-08-05 22:49:48 +02:00
|
|
|
|
2022-09-03 13:43:32 +02:00
|
|
|
logger = logging.getLogger('src')
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-09-09 06:15:28 +02:00
|
|
|
# Create app directory, if it doesn't exist
|
|
|
|
state.config_file.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
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)
|
2023-01-01 20:22:35 +01:00
|
|
|
# Invoke Python interpreter every 500ms to handle signals, run scheduled tasks
|
2022-08-12 20:02:18 +02:00
|
|
|
timer = QTimer()
|
|
|
|
timer.start(500)
|
2023-01-01 20:22:35 +01:00
|
|
|
timer.timeout.connect(schedule.run_pending)
|
2022-08-12 20:02:18 +02:00
|
|
|
|
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()
|