def check_anonymous(self): # If there's no API key set, ask if the run should be logged anonymously. Only launch this prompt in # environments with a tty. if not self.api.api_key and sys.stdin.isatty(): # Require anonymous mode to be explicitly enabled for now if os.environ.get(env.ANONYMOUS) != "enable": return False termlog( 'No API key found. Would you like to log runs anonymously to {}? (y/n)' .format(self.api.app_url)) resp = str(input().lower().strip()) while not (resp == 'y' or resp == 'n'): termlog('Invalid response. Please enter y/n.') resp = str(input()).lower().strip() if resp == 'y': key = self.api.create_anonymous_api_key() url = self.api.app_url + '/login?apiKey={}'.format(key) termlog( 'Your anonymous login link: {}. Do not share or lose this link!' .format(url)) os.environ[env.API_KEY] = key self.api.set_setting('anonymous', True) util.write_netrc(self.api.api_url, "user", key) util.write_settings(settings=self.api.settings()) self.api.reauth() return True return False
def _init_jupyter(run): """Asks for user input to configure the machine if it isn't already and creates a new run. Log pushing and system stats don't start until `wandb.log()` is first called. """ from wandb import jupyter from IPython.core.display import display, HTML # TODO: Should we log to jupyter? # global logging had to be disabled because it set the level to debug # I also disabled run logging because we're rairly using it. # try_to_set_up_global_logging() # run.enable_logging() if not run.api.api_key: key = None if 'google.colab' in sys.modules: key = jupyter.attempt_colab_login(run.api.app_url) if key: os.environ[env.API_KEY] = key util.write_netrc(run.api.api_url, "user", key) if not key: termerror( "Not authenticated. Copy a key from https://app.wandb.ai/authorize" ) key = getpass.getpass("API Key: ").strip() if len(key) == 40: os.environ[env.API_KEY] = key util.write_netrc(run.api.api_url, "user", key) else: raise ValueError("API Key must be 40 characters long") # Ensure our api client picks up the new key run.api.reauth() os.environ["WANDB_JUPYTER"] = "true" run.resume = "allow" display( HTML(''' Notebook configured with <a href="https://wandb.com" target="_blank">W&B</a>. You can <a href="{}" target="_blank">open</a> the run page, or call <code>%%wandb</code> in a cell containing your training loop to display live results. Learn more in our <a href="https://docs.wandb.com/docs/integrations/jupyter.html" target="_blank">docs</a>. '''.format(run.get_url()))) try: run.save() except (CommError, ValueError) as e: termerror(str(e)) run.set_environment() run._init_jupyter_agent() ipython = get_ipython() ipython.register_magics(jupyter.WandBMagics) def reset_start(): """Reset START_TIME to when the cell starts""" global START_TIME START_TIME = time.time() ipython.events.register("pre_run_cell", reset_start) ipython.events.register('post_run_cell', run._stop_jupyter_agent)
def _init_jupyter(run): """Asks for user input to configure the machine if it isn't already and creates a new run. Log pushing and system stats don't start until `wandb.monitor()` is called. """ from wandb import jupyter # TODO: Should we log to jupyter? # global logging had to be disabled because it set the level to debug # I also disabled run logging because we're rairly using it. # try_to_set_up_global_logging() # run.enable_logging() api = InternalApi() if not api.api_key: termerror( "Not authenticated. Copy a key from https://app.wandb.ai/authorize" ) key = getpass.getpass("API Key: ").strip() if len(key) == 40: os.environ[env.API_KEY] = key util.write_netrc(api.api_url, "user", key) else: raise ValueError("API Key must be 40 characters long") # Ensure our api client picks up the new key api = InternalApi() os.environ["WANDB_JUPYTER"] = "true" run.resume = "allow" api.set_current_run_id(run.id) print("W&B Run: %s" % run.get_url(api)) print( "Call `%%wandb` in the cell containing your training loop to display live results." ) try: run.save(api=api) except (CommError, ValueError) as e: termerror(str(e)) run.set_environment() run._init_jupyter_agent() ipython = get_ipython() ipython.register_magics(jupyter.WandBMagics) def reset_start(): """Reset START_TIME to when the cell starts""" global START_TIME START_TIME = time.time() ipython.events.register("pre_run_cell", reset_start) ipython.events.register('post_run_cell', run._stop_jupyter_agent)
def _init_jupyter(run, job_type): """Asks for user input to configure the machine if it isn't already and creates a new run. Log pushing and system stats don't start until `wandb.monitor()` is called. """ global http_api # TODO: Should we log to jupyter? try_to_set_up_logging() if not http_api.api_key: termerror( "Not authenticated. Copy a key from https://app.wandb.ai/profile?message=true" ) key = getpass.getpass("API Key: ").strip() if len(key) == 40: os.environ["WANDB_API_KEY"] = key util.write_netrc(http_api.api_url, "user", key) else: raise ValueError("API Key must be 40 characters long") # Ensure our api client picks up the new key http_api = Api() if not http_api.settings('project'): termerror("No W&B project configured.") slug = six.moves.input("Enter username/project: ").strip() if "/" not in slug: raise ValueError( "Input must contain a slash between username and project") entity, project = slug.split("/") os.environ["WANDB_ENTITY"] = entity os.environ["WANDB_PROJECT"] = project util.write_settings(entity, project, http_api.settings()['base_url']) # Reset settings so they reload http_api._settings = None os.environ["WANDB_JUPYTER"] = "true" run.resume = "allow" http_api.set_current_run_id(run.id) print("W&B Run: %s" % run.get_url(http_api)) print( "Wrap your training loop with `with wandb.monitor():` to display live results." ) try: run.save(api=http_api, job_type=job_type) except (CommError, ValueError) as e: termerror(str(e)) run.set_environment()
def jupyter_login(force=True): """Attempt to login from a jupyter environment If force=False, we'll only attempt to auto-login, otherwise we'll prompt the user """ key = None if 'google.colab' in sys.modules: key = jupyter.attempt_colab_login(run.api.app_url) if key: os.environ[env.API_KEY] = key util.write_netrc(run.api.api_url, "user", key) if not key and force: termerror( "Not authenticated. Copy a key from https://app.wandb.ai/authorize") key = getpass.getpass("API Key: ").strip() if len(key) == 40: os.environ[env.API_KEY] = key util.write_netrc(run.api.api_url, "user", key) else: raise ValueError("API Key must be 40 characters long") return key
def login(key, server=LocalServer(), browser=True): global api key = key[0] if len(key) > 0 else None # Import in here for performance reasons import webbrowser # TODO: use Oauth?: https://community.auth0.com/questions/6501/authenticating-an-installed-cli-with-oidc-and-a-th url = api.app_url + '/authorize' browser = util.launch_browser(browser) if key or not browser: launched = False else: launched = webbrowser.open_new_tab(url + "?{}".format(server.qs())) if launched: click.echo('Opening [{}] in your default browser'.format(url)) server.start(blocking=False) elif not key: click.echo( "You can find your API keys in your browser here: {}".format(url)) def cancel_prompt(*args): raise KeyboardInterrupt() # Hijacking this signal broke tests in py2... # if not os.getenv("WANDB_TEST"): signal.signal(signal.SIGINT, cancel_prompt) try: key = key or click.prompt("Paste an API key from your profile", value_proc=lambda x: x.strip()) except Abort: if server.result.get("key"): key = server.result["key"][0] if key: # TODO: get the username here... # username = api.viewer().get('entity', 'models') if util.write_netrc(api.api_url, "user", key): click.secho("Successfully logged in to Weights & Biases!", fg="green") else: click.echo("No key provided, please try again") # reinitialize API to create the new client api = InternalApi() return key
def login(key, server=LocalServer(), browser=True, anonymous=False): global api key = key[0] if len(key) > 0 else None # Import in here for performance reasons import webbrowser browser = util.launch_browser(browser) # Go through the regular user login flow first, unless --anonymous is specified. if not key and not anonymous: # TODO: use Oauth?: https://community.auth0.com/questions/6501/authenticating-an-installed-cli-with-oidc-and-a-th url = api.app_url + '/authorize' if key or not browser: launched = False else: launched = webbrowser.open_new_tab(url + "?{}".format(server.qs())) if launched: click.echo('Opening [{}] in your default browser'.format(url)) server.start(blocking=False) elif not key: click.echo( "You can find your API keys in your browser here: {}".format( url)) def cancel_prompt(*args): raise KeyboardInterrupt() # Hijacking this signal broke tests in py2... # if not os.getenv("WANDB_TEST"): signal.signal(signal.SIGINT, cancel_prompt) try: key = key or click.prompt("Paste an API key from your profile", value_proc=lambda x: x.strip()) except Abort: if server.result.get("key"): key = server.result["key"][0] # If we still don't have a key, go through the anonymous user flow if we're running interactively. if not key: try: click.confirm( 'No API key found. Would you like to log runs anonymously?', abort=True) anonymous = True except Abort: anonymous = False # Go through the anonymous login flow. if not key and anonymous: if api.api_key: click.confirm( 'You are already logged in. Are you sure you want to create a new anonymous login?', abort=True) # Generate a new anonymous user and use its API key. key = api.create_anonymous_api_key() url = api.app_url + '/login?apiKey={}'.format(key) if browser: webbrowser.open_new_tab(url) click.echo( "Your anonymous login link: {}. Do not share or lose this link!". format(url)) if key: # TODO: get the username here... # username = api.viewer().get('entity', 'models') if util.write_netrc(api.api_url, "user", key): api.set_setting('anonymous', anonymous) util.write_settings(settings=api.settings()) click.secho("Successfully logged in to Weights & Biases!", fg="green") else: click.echo("No key provided, please try again") # reinitialize API to create the new client api = InternalApi() return key