gpt4free/g4f/gui/webview.py

52 lines
1.3 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-03-17 12:51:08 +03:00
import sys
import os.path
import webview
try:
from platformdirs import user_config_dir
has_platformdirs = True
except ImportError:
has_platformdirs = False
2024-04-06 17:30:35 +03:00
from g4f.gui.gui_parser import gui_parser
2024-04-06 17:58:15 +03:00
from g4f.gui.server.js_api import JsApi
import g4f.version
import g4f.debug
def run_webview(
debug: bool = False,
2024-04-06 17:37:19 +03:00
http_port: int = None,
ssl: bool = True,
storage_path: str = None,
gui: str = None
):
2024-03-17 12:51:08 +03:00
if getattr(sys, 'frozen', False):
dirname = sys._MEIPASS
else:
dirname = os.path.dirname(__file__)
webview.settings['OPEN_EXTERNAL_LINKS_IN_BROWSER'] = True
webview.settings['ALLOW_DOWNLOADS'] = True
webview.create_window(
f"g4f - {g4f.version.utils.current_version}",
2024-03-17 12:51:08 +03:00
os.path.join(dirname, "client/index.html"),
2024-03-16 12:48:37 +03:00
text_select=True,
js_api=JsApi(),
)
if has_platformdirs and storage_path is None:
storage_path = user_config_dir("g4f-webview")
webview.start(
private_mode=False,
storage_path=storage_path,
2024-03-16 12:48:37 +03:00
debug=debug,
2024-04-06 17:37:19 +03:00
http_port=http_port,
ssl=ssl,
gui=gui
)
if __name__ == "__main__":
parser = gui_parser()
args = parser.parse_args()
if args.debug:
g4f.debug.logging = True
2024-04-06 17:40:14 +03:00
run_webview(args.debug, args.port)