def __init__(self, *, dashboard_type, model_data, public_ip, port, locale): """Initialize the dashboard.""" if model_data is None or type is None: raise ValueError("Required parameters not provided") try: self._service = FlaskHelper(ip=public_ip, port=port) except Exception as e: self._service = None raise e self.id = uuid.uuid4().hex self.config = { 'dashboardType': dashboard_type, 'id': self.id, 'baseUrl': self._service.env.base_url, 'withCredentials': self._service.with_credentials, 'locale': locale } self.model_data = model_data self.add_route() html = self.load_index() print(f'{dashboard_type} started at {self._service.env.base_url}') self._service.env.display(html)
def __init__(self, *, dashboard_type, model_data, public_ip=None, port=None, add_local_url=False): """Initialize the dashboard.""" if model_data is None or type is None: raise ValueError("Required parameters not provided") try: self._service = FlaskHelper(ip=public_ip, port=port) except Exception as e: self._service = None raise e self.id = uuid.uuid4().hex self.config = { "dashboardType": dashboard_type, "id": self.id, "baseUrl": self._service.env.base_url, 'withCredentials': self._service.with_credentials } if add_local_url: local_url = ExplanationDashboardInterface.LOCAL_URL model_data[local_url] = self._service.env.base_url self.model_data = model_data self.add_route() html = self.load_index() print(f'{dashboard_type} started at {self._service.env.base_url}') self._service.env.display(html)
def test_public_vm_fail_on_port(self, mocker): mocker.patch('rai_core_flask.FlaskHelper._is_local_port_available', return_value=False) with pytest.raises(RuntimeError) as exception: FlaskHelper(ip="not localhost", with_credentials=False) assert "Ports 5000 to 5100 not available." in \ exception.value.args[0]
def test_databricks(self): try: os.environ[DATABRICKS_ENV_VAR] = "mock" service = FlaskHelper() assert type(service.env) == DatabricksEnvironment finally: del os.environ[DATABRICKS_ENV_VAR]
def test_azure_nb(self, mocker): mocker.patch('os.path.exists', return_value=True) mocker.patch('os.path.isfile', return_value=True) mocker.patch( 'rai_core_flask.environments.azure_nb_environment.' 'AzureNBEnvironment.get_nbvm_config', return_value={ 'instance': "fakeaznbinstance", 'domainsuffix': "fakedomainsuffix"}) service = FlaskHelper() assert type(service.env) == AzureNBEnvironment assert service.with_credentials
def test_without_explicit_port(self): """Test the flask helper without setting an explicit port.""" http = self.get_http_client() flask_service = FlaskHelper(ip=self.test_ip_local) assert (flask_service.port >= 5000 and flask_service.port <= 5099) @flask_service.app.route("/hello", methods=["GET"]) def hello(): return "Hello" response = http.get(f"{flask_service.env.base_url}/hello") assert (response.status_code == 200) assert (response.text == "Hello")
def test_with_explicit_port(self): """Test the flask helper when setting an explicit port.""" http = self.get_http_client() flask_service = FlaskHelper(ip=self.test_ip_local, port=self.test_port_local) assert (flask_service.port == self.test_port_local) @flask_service.app.route("/hello_two", methods=["GET"]) def hello_two(): return "Hello" response = call_get_with_retries( http, f"{flask_service.env.base_url}/hello_two") assert (response.status_code == 200) assert (response.text == "Hello")
def __init__(self, *, dashboard_type, model_data, public_ip, port, locale, no_inline_dashboard=False, **kwargs): """Initialize the dashboard.""" if model_data is None or type is None: raise ValueError("Required parameters not provided") try: self._service = FlaskHelper(ip=public_ip, port=port) except Exception as e: self._service = None raise e self.id = uuid.uuid4().hex feature_flights = kwargs.get('feature_flights') if feature_flights and not isinstance(feature_flights, str): raise ValueError(invalid_feature_flights_error) self.config = { 'dashboardType': dashboard_type, 'id': self.id, 'baseUrl': self._service.env.base_url, 'withCredentials': self._service.with_credentials, 'locale': locale, 'featureFlights': feature_flights } self.model_data = model_data self.add_route() html = self.load_index() print(f'{dashboard_type} started at {self._service.env.base_url}') if no_inline_dashboard: return self._service.env.display(html)
def __init__(self, *, sensitive_features, y_true, y_pred, sensitive_feature_names=None, locale=None, port=None): """Initialize the fairness dashboard.""" if sensitive_features is None or y_true is None or y_pred is None: raise ValueError("Required parameters not provided") dataset = self._sanitize_data_shape(sensitive_features) model_names = None if isinstance(y_pred, dict): model_names = [] self._y_pred = [] for k, v in y_pred.items(): model_names.append(k) self._y_pred.append(self._convert_to_list(v)) else: self._y_pred = self._convert_to_list(y_pred) if len(np.shape(self._y_pred)) == 1: self._y_pred = [self._y_pred] self._y_true = self._convert_to_list(y_true) if np.shape(self._y_true)[0] != np.shape(self._y_pred)[1]: raise ValueError("Predicted y does not match true y shape") if np.shape(self._y_true)[0] != np.shape(dataset)[0]: raise ValueError("Sensitive features shape does not match true y " "shape") fairness_input = { "true_y": self._y_true, "predicted_ys": self._y_pred, "dataset": dataset, "classification_methods": FairlearnDashboard._classification_methods, "regression_methods": FairlearnDashboard._regression_methods, "probability_methods": FairlearnDashboard._probability_methods, } if model_names is not None: fairness_input['model_names'] = model_names if locale is not None: fairness_input['locale'] = locale if sensitive_feature_names is not None: sensitive_feature_names = self._convert_to_list( sensitive_feature_names) if np.shape(dataset)[1] != np.shape(sensitive_feature_names)[0]: raise Warning("Feature names shape does not match dataset, " "ignoring") fairness_input["features"] = sensitive_feature_names self._load_local_js() if FairlearnDashboard._service is None: try: FairlearnDashboard._service = FlaskHelper(port=port) except Exception as e: FairlearnDashboard._service = None raise e FairlearnDashboard._model_count += 1 model_count = FairlearnDashboard._model_count local_url = f"{FairlearnDashboard._service.env.base_url}/{model_count}" metrics_url = f"{local_url}/metrics" fairness_input['metricsUrl'] = metrics_url # TODO fairness_input['withCredentials'] = False FairlearnDashboard._fairness_inputs[str(model_count)] = fairness_input html = generate_inline_html(fairness_input, local_url) # TODO # FairlearnDashboard._service.env.display(html) display(HTML(html))
def test_local(self): service = FlaskHelper() assert type(service.env) == LocalIPythonEnvironment
def test_public_vm(self, mocker): mocker.patch('rai_core_flask.FlaskHelper._is_local_port_available', return_value=True) service = FlaskHelper(ip="not localhost", with_credentials=False) assert type(service.env) == PublicVMEnvironment
def test_credentialed_vm(self): service = FlaskHelper(with_credentials=True) assert type(service.env) == CredentialedVMEnvironment