Use ngrok-py library

This commit is contained in:
bobzilladev 2023-05-16 13:15:30 -04:00
parent 89f9faa633
commit 0d31f20cbd
4 changed files with 19 additions and 27 deletions

View File

@ -294,8 +294,8 @@ def prepare_environment():
elif platform.system() == "Linux": elif platform.system() == "Linux":
run_pip(f"install {xformers_package}", "xformers") run_pip(f"install {xformers_package}", "xformers")
if not is_installed("pyngrok") and args.ngrok: if not is_installed("ngrok") and args.ngrok:
run_pip("install pyngrok", "ngrok") run_pip("install ngrok", "ngrok")
os.makedirs(os.path.join(script_path, dir_repos), exist_ok=True) os.makedirs(os.path.join(script_path, dir_repos), exist_ok=True)

View File

@ -1,4 +1,5 @@
import argparse import argparse
import json
import os import os
from modules.paths_internal import models_path, script_path, data_path, extensions_dir, extensions_builtin_dir, sd_default_config, sd_model_file from modules.paths_internal import models_path, script_path, data_path, extensions_dir, extensions_builtin_dir, sd_default_config, sd_model_file
@ -39,7 +40,7 @@ parser.add_argument("--precision", type=str, help="evaluate at this precision",
parser.add_argument("--upcast-sampling", action='store_true', help="upcast sampling. No effect with --no-half. Usually produces similar results to --no-half with better performance while using less memory.") parser.add_argument("--upcast-sampling", action='store_true', help="upcast sampling. No effect with --no-half. Usually produces similar results to --no-half with better performance while using less memory.")
parser.add_argument("--share", action='store_true', help="use share=True for gradio and make the UI accessible through their site") parser.add_argument("--share", action='store_true', help="use share=True for gradio and make the UI accessible through their site")
parser.add_argument("--ngrok", type=str, help="ngrok authtoken, alternative to gradio --share", default=None) parser.add_argument("--ngrok", type=str, help="ngrok authtoken, alternative to gradio --share", default=None)
parser.add_argument("--ngrok-region", type=str, help="The region in which ngrok should start.", default="us") parser.add_argument("--ngrok-options", type=json.loads, help='The options to pass to ngrok in JSON format, e.g.: \'{"authtoken_from_env":true, "basic_auth":"user:password", "oauth_provider":"google", "oauth_allow_emails":"user@asdf.com"}\'', default=dict())
parser.add_argument("--enable-insecure-extension-access", action='store_true', help="enable extensions tab regardless of other options") parser.add_argument("--enable-insecure-extension-access", action='store_true', help="enable extensions tab regardless of other options")
parser.add_argument("--codeformer-models-path", type=str, help="Path to directory with codeformer model file(s).", default=os.path.join(models_path, 'Codeformer')) parser.add_argument("--codeformer-models-path", type=str, help="Path to directory with codeformer model file(s).", default=os.path.join(models_path, 'Codeformer'))
parser.add_argument("--gfpgan-models-path", type=str, help="Path to directory with GFPGAN model file(s).", default=os.path.join(models_path, 'GFPGAN')) parser.add_argument("--gfpgan-models-path", type=str, help="Path to directory with GFPGAN model file(s).", default=os.path.join(models_path, 'GFPGAN'))

View File

@ -1,6 +1,7 @@
from pyngrok import ngrok, conf, exception import ngrok
def connect(token, port, region): # Connect to ngrok for ingress
def connect(token, port, options):
account = None account = None
if token is None: if token is None:
token = 'None' token = 'None'
@ -10,28 +11,18 @@ def connect(token, port, region):
token, username, password = token.split(':', 2) token, username, password = token.split(':', 2)
account = f"{username}:{password}" account = f"{username}:{password}"
config = conf.PyngrokConfig( # For all options see: https://github.com/ngrok/ngrok-py/blob/main/examples/ngrok-connect-full.py
auth_token=token, region=region if not options.get('authtoken_from_env'):
) options['authtoken'] = token
if account:
# Guard for existing tunnels options['basic_auth'] = account
existing = ngrok.get_tunnels(pyngrok_config=config) if not options.get('session_metadata'):
if existing: options['session_metadata'] = 'stable-diffusion-webui'
for established in existing:
# Extra configuration in the case that the user is also using ngrok for other tunnels
if established.config['addr'][-4:] == str(port):
public_url = existing[0].public_url
print(f'ngrok has already been connected to localhost:{port}! URL: {public_url}\n'
'You can use this link after the launch is complete.')
return
try: try:
if account is None: public_url = ngrok.connect(f"127.0.0.1:{port}", **options).url()
public_url = ngrok.connect(port, pyngrok_config=config, bind_tls=True).public_url except Exception as e:
else: print(f'Invalid ngrok authtoken? ngrok connection aborted due to: {e}\n'
public_url = ngrok.connect(port, pyngrok_config=config, bind_tls=True, auth=account).public_url
except exception.PyngrokNgrokError:
print(f'Invalid ngrok authtoken, ngrok connection aborted.\n'
f'Your token: {token}, get the right one on https://dashboard.ngrok.com/get-started/your-authtoken') f'Your token: {token}, get the right one on https://dashboard.ngrok.com/get-started/your-authtoken')
else: else:
print(f'ngrok connected to localhost:{port}! URL: {public_url}\n' print(f'ngrok connected to localhost:{port}! URL: {public_url}\n'

View File

@ -59,7 +59,7 @@ if cmd_opts.ngrok is not None:
ngrok.connect( ngrok.connect(
cmd_opts.ngrok, cmd_opts.ngrok,
cmd_opts.port if cmd_opts.port is not None else 7860, cmd_opts.port if cmd_opts.port is not None else 7860,
cmd_opts.ngrok_region cmd_opts.ngrok_options
) )