def test_readme_to_html_correct_parse(self): readme_to_html( "https://github.com/gradio-app/gradio/blob/master/README.md")
def test_readme_to_html_doesnt_crash_on_connection_error(self, mock_get): mock_get.side_effect = requests.ConnectionError() readme_to_html("placeholder")
def __init__( self, fn: Callable | List[Callable], inputs: str | Component | List[str | Component] = None, outputs: str | Component | List[str | Component] = None, verbose: bool = False, examples: Optional[List[Any] | List[List[Any]] | str] = None, cache_examples: bool = False, examples_per_page: int = 10, live: bool = False, layout: str = "unaligned", show_input: bool = True, show_output: bool = True, capture_session: Optional[bool] = None, interpretation: Optional[Callable | str] = None, num_shap: float = 2.0, theme: Optional[str] = None, repeat_outputs_per_model: bool = True, title: Optional[str] = None, description: Optional[str] = None, article: Optional[str] = None, thumbnail: Optional[str] = None, css: Optional[str] = None, height=None, width=None, allow_screenshot: bool = False, allow_flagging: Optional[str] = None, flagging_options: List[str] = None, encrypt=None, show_tips=None, flagging_dir: str = "flagged", analytics_enabled: Optional[bool] = None, server_name=None, server_port=None, enable_queue=None, api_mode=None, flagging_callback: FlaggingCallback = CSVLogger(), ): # TODO: (faruk) Let's remove depreceated parameters in the version 3.0.0 """ Parameters: fn (Union[Callable, List[Callable]]): the function to wrap an interface around. inputs (Union[str, InputComponent, List[Union[str, InputComponent]]]): a single Gradio input component, or list of Gradio input components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. The number of input components should match the number of parameters in fn. outputs (Union[str, OutputComponent, List[Union[str, OutputComponent]]]): a single Gradio output component, or list of Gradio output components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. The number of output components should match the number of values returned by fn. verbose (bool): DEPRECATED. Whether to print detailed information during launch. examples (Union[List[List[Any]], str]): sample inputs for the function; if provided, appears below the UI components and can be used to populate the interface. Should be nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component. A string path to a directory of examples can also be provided. If there are multiple input components and a directory is provided, a log.csv file must be present in the directory to link corresponding inputs. examples_per_page (int): If examples are provided, how many to display per page. live (bool): whether the interface should automatically reload on change. layout (str): Layout of input and output panels. "horizontal" arranges them as two columns of equal height, "unaligned" arranges them as two columns of unequal height, and "vertical" arranges them vertically. capture_session (bool): DEPRECATED. If True, captures the default graph and session (needed for Tensorflow 1.x) interpretation (Union[Callable, str]): function that provides interpretation explaining prediction output. Pass "default" to use simple built-in interpreter, "shap" to use a built-in shapley-based interpreter, or your own custom interpretation function. num_shap (float): a multiplier that determines how many examples are computed for shap-based interpretation. Increasing this value will increase shap runtime, but improve results. Only applies if interpretation is "shap". title (str): a title for the interface; if provided, appears above the input and output components. description (str): a description for the interface; if provided, appears above the input and output components. article (str): an expanded article explaining the interface; if provided, appears below the input and output components. Accepts Markdown and HTML content. thumbnail (str): path to image or src to use as display picture for models listed in gradio.app/hub theme (str): Theme to use - one of "default", "huggingface", "seafoam", "grass", "peach". Add "dark-" prefix, e.g. "dark-peach" for dark theme (or just "dark" for the default dark theme). css (str): custom css or path to custom css file to use with interface. allow_screenshot (bool): DEPRECATED if False, users will not see a button to take a screenshot of the interface. allow_flagging (str): one of "never", "auto", or "manual". If "never" or "auto", users will not see a button to flag an input and output. If "manual", users will see a button to flag. If "auto", every prediction will be automatically flagged. If "manual", samples are flagged when the user clicks flag button. Can be set with environmental variable GRADIO_ALLOW_FLAGGING. flagging_options (List[str]): if provided, allows user to select from the list of options when flagging. Only applies if allow_flagging is "manual". encrypt (bool): DEPRECATED. If True, flagged data will be encrypted by key provided by creator at launch flagging_dir (str): what to name the dir where flagged data is stored. show_tips (bool): DEPRECATED. if True, will occasionally show tips about new Gradio features enable_queue (bool): DEPRECATED. if True, inference requests will be served through a queue instead of with parallel threads. Required for longer inference times (> 1min) to prevent timeout. api_mode (bool): DEPRECATED. If True, will skip preprocessing steps when the Interface is called() as a function (should remain False unless the Interface is loaded from an external repo) server_name (str): DEPRECATED. Name of the server to use for serving the interface - pass in launch() instead. server_port (int): DEPRECATED. Port of the server to use for serving the interface - pass in launch() instead. """ super().__init__(analytics_enabled=analytics_enabled, mode="interface") if inputs is None: inputs = [] if outputs is None: outputs = [] if not isinstance(fn, list): fn = [fn] if not isinstance(inputs, list): inputs = [inputs] if not isinstance(outputs, list): outputs = [outputs] if "state" in inputs or "state" in outputs: state_input_count = len([i for i in inputs if i == "state"]) state_output_count = len([o for o in outputs if o == "state"]) if state_input_count != 1 or state_output_count != 1: raise ValueError( "If using 'state', there must be exactly one state input and one state output." ) default = utils.get_default_args(fn[0])[inputs.index("state")] state_variable = Variable(default_value=default) inputs[inputs.index("state")] = state_variable outputs[outputs.index("state")] = state_variable self.input_components = [get_component_instance(i) for i in inputs] self.output_components = [get_component_instance(o) for o in outputs] for o in self.output_components: o.interactive = ( False # Force output components to be treated as non-interactive ) if repeat_outputs_per_model: self.output_components *= len(fn) if ( interpretation is None or isinstance(interpretation, list) or callable(interpretation) ): self.interpretation = interpretation elif isinstance(interpretation, str): self.interpretation = [ interpretation.lower() for _ in self.input_components ] else: raise ValueError("Invalid value for parameter: interpretation") self.predict = fn self.predict_durations = [[0, 0]] * len(fn) self.function_names = [func.__name__ for func in fn] self.__name__ = ", ".join(self.function_names) if verbose: warnings.warn( "The `verbose` parameter in the `Interface`" "is deprecated and has no effect." ) if allow_screenshot: warnings.warn( "The `allow_screenshot` parameter in the `Interface`" "is deprecated and has no effect." ) self.live = live self.layout = layout self.show_input = show_input self.show_output = show_output self.flag_hash = random.getrandbits(32) self.capture_session = capture_session if capture_session is not None: warnings.warn( "The `capture_session` parameter in the `Interface`" " is deprecated and may be removed in the future." ) try: import tensorflow as tf self.session = tf.get_default_graph(), tf.keras.backend.get_session() except (ImportError, AttributeError): # If they are using TF >= 2.0 or don't have TF, # just ignore this parameter. pass if server_name is not None or server_port is not None: raise DeprecationWarning( "The `server_name` and `server_port` parameters in `Interface`" "are deprecated. Please pass into launch() instead." ) self.session = None self.title = title CLEANER = re.compile("<.*?>") def clean_html(raw_html): cleantext = re.sub(CLEANER, "", raw_html) return cleantext md = MarkdownIt( "js-default", { "linkify": True, "typographer": True, "html": True, }, ).use(footnote_plugin) simple_description = None if description is not None: description = md.render(description) simple_description = clean_html(description) self.simple_description = simple_description self.description = description if article is not None: article = utils.readme_to_html(article) article = md.render(article) self.article = article self.thumbnail = thumbnail theme = theme if theme is not None else os.getenv("GRADIO_THEME", "default") self.is_space = True if os.getenv("SYSTEM") == "spaces" else False DEPRECATED_THEME_MAP = { "darkdefault": "default", "darkhuggingface": "dark-huggingface", "darkpeach": "dark-peach", "darkgrass": "dark-grass", } VALID_THEME_SET = ( "default", "huggingface", "seafoam", "grass", "peach", "dark", "dark-huggingface", "dark-seafoam", "dark-grass", "dark-peach", ) if theme in DEPRECATED_THEME_MAP: warnings.warn( f"'{theme}' theme name is deprecated, using {DEPRECATED_THEME_MAP[theme]} instead." ) theme = DEPRECATED_THEME_MAP[theme] elif theme not in VALID_THEME_SET: raise ValueError( f"Invalid theme name, theme must be one of: {', '.join(VALID_THEME_SET)}" ) self.theme = theme self.height = height self.width = width if self.height is not None or self.width is not None: warnings.warn( "The `height` and `width` parameters in `Interface` " "are deprecated and should be passed into launch()." ) if css is not None and os.path.exists(css): with open(css) as css_file: self.css = css_file.read() else: self.css = css if examples is None or ( isinstance(examples, list) and (len(examples) == 0 or isinstance(examples[0], list)) ): self.examples = examples elif ( isinstance(examples, list) and len(self.input_components) == 1 ): # If there is only one input component, examples can be provided as a regular list instead of a list of lists self.examples = [[e] for e in examples] elif isinstance(examples, str): if not os.path.exists(examples): raise FileNotFoundError( "Could not find examples directory: " + examples ) log_file = os.path.join(examples, "log.csv") if not os.path.exists(log_file): if len(self.input_components) == 1: exampleset = [ [os.path.join(examples, item)] for item in os.listdir(examples) ] else: raise FileNotFoundError( "Could not find log file (required for multiple inputs): " + log_file ) else: with open(log_file) as logs: exampleset = list(csv.reader(logs)) exampleset = exampleset[1:] # remove header for i, example in enumerate(exampleset): for j, (component, cell) in enumerate( zip( self.input_components + self.output_components, example, ) ): exampleset[i][j] = component.restore_flagged( examples, cell, None, ) self.examples = exampleset else: raise ValueError( "Examples argument must either be a directory or a nested " "list, where each sublist represents a set of inputs." ) self.num_shap = num_shap self.examples_per_page = examples_per_page self.simple_server = None self.allow_screenshot = allow_screenshot # For analytics_enabled and allow_flagging: (1) first check for # parameter, (2) check for env variable, (3) default to True/"manual" self.analytics_enabled = ( analytics_enabled if analytics_enabled is not None else os.getenv("GRADIO_ANALYTICS_ENABLED", "True") == "True" ) if allow_flagging is None: allow_flagging = os.getenv("GRADIO_ALLOW_FLAGGING", "manual") if allow_flagging is True: warnings.warn( "The `allow_flagging` parameter in `Interface` now" "takes a string value ('auto', 'manual', or 'never')" ", not a boolean. Setting parameter to: 'manual'." ) self.allow_flagging = "manual" elif allow_flagging == "manual": self.allow_flagging = "manual" elif allow_flagging is False: warnings.warn( "The `allow_flagging` parameter in `Interface` now" "takes a string value ('auto', 'manual', or 'never')" ", not a boolean. Setting parameter to: 'never'." ) self.allow_flagging = "never" elif allow_flagging == "never": self.allow_flagging = "never" elif allow_flagging == "auto": self.allow_flagging = "auto" else: raise ValueError( "Invalid value for `allow_flagging` parameter." "Must be: 'auto', 'manual', or 'never'." ) self.flagging_options = flagging_options self.flagging_callback = flagging_callback self.flagging_dir = flagging_dir self.save_to = None # Used for selenium tests self.share = None self.share_url = None self.local_url = None if show_tips is not None: warnings.warn( "The `show_tips` parameter in the `Interface` is deprecated. Please use the `show_tips` parameter in `launch()` instead" ) self.requires_permissions = any( [component.requires_permissions for component in self.input_components] ) self.enable_queue = enable_queue self.favicon_path = None self.height = height self.width = width if self.height is not None or self.width is not None: warnings.warn( "The `width` and `height` parameters in the `Interface` class" "will be deprecated. Please provide these parameters" "in `launch()` instead" ) self.encrypt = encrypt if self.encrypt is not None: warnings.warn( "The `encrypt` parameter in the `Interface` class" "will be deprecated. Please provide this parameter" "in `launch()` instead" ) if api_mode is not None: warnings.warn("The `api_mode` parameter in the `Interface` is deprecated.") self.api_mode = False data = { "fn": fn, "inputs": inputs, "outputs": outputs, "live": live, "capture_session": capture_session, "ip_address": self.ip_address, "interpretation": interpretation, "allow_flagging": allow_flagging, "allow_screenshot": allow_screenshot, "custom_css": self.css is not None, "theme": self.theme, } if self.analytics_enabled: utils.initiated_analytics(data) utils.version_check() Interface.instances.add(self) param_names = inspect.getfullargspec(self.predict[0])[0] for component, param_name in zip(self.input_components, param_names): if component.label is None: component.label = param_name for i, component in enumerate(self.output_components): if component.label is None: if len(self.output_components) == 1: component.label = "output" else: component.label = "output " + str(i) self.cache_examples = cache_examples if cache_examples: cache_interface_examples(self) if self.allow_flagging != "never": self.flagging_callback.setup( self.input_components + self.output_components, self.flagging_dir ) with self: if self.title: Markdown( "<h1 style='text-align: center; margin-bottom: 1rem'>" + self.title + "</h1>" ) if self.description: Markdown(self.description) with Row(): with Column( css={ "background-color": "rgb(249,250,251)", "padding": "0.5rem", "border-radius": "0.5rem", } ): input_component_column = Column() with input_component_column: for component in self.input_components: component.render() if self.interpretation: interpret_component_column = Column(visible=False) interpretation_set = [] with interpret_component_column: for component in self.input_components: interpretation_set.append(Interpretation(component)) with Row(): clear_btn = Button("Clear") if not self.live: submit_btn = Button("Submit") with Column( css={ "background-color": "rgb(249,250,251)", "padding": "0.5rem", "border-radius": "0.5rem", } ): status_tracker = StatusTracker(cover_container=True) for component in self.output_components: component.render() with Row(): flag_btn = Button("Flag") if self.interpretation: interpretation_btn = Button("Interpret") submit_fn = ( lambda *args: self.run_prediction(args)[0] if len(self.output_components) == 1 else self.run_prediction(args) ) if self.live: for component in self.input_components: component.change( submit_fn, self.input_components, self.output_components ) else: submit_btn.click( submit_fn, self.input_components, self.output_components, queue=self.enable_queue, status_tracker=status_tracker, ) clear_btn.click( lambda: [ component.default_value if hasattr(component, "default_value") else None for component in self.input_components + self.output_components ] + [True] + ([False] if self.interpretation else []), [], self.input_components + self.output_components + [input_component_column] + ([interpret_component_column] if self.interpretation else []), ) if self.examples: examples = Dataset( components=self.input_components, samples=self.examples, type="index", ) def load_example(example_id): processed_examples = [ component.preprocess_example(sample) for component, sample in zip( self.input_components, self.examples[example_id] ) ] if self.cache_examples: processed_examples += load_from_cache(self, example_id) if len(processed_examples) == 1: return processed_examples[0] else: return processed_examples examples._click_no_postprocess( load_example, inputs=[examples], outputs=self.input_components + (self.output_components if self.cache_examples else []), ) flag_btn._click_no_preprocess( lambda *flag_data: self.flagging_callback.flag(flag_data), inputs=self.input_components + self.output_components, outputs=[], ) if self.interpretation: interpretation_btn._click_no_preprocess( lambda *data: self.interpret(data) + [False, True], inputs=self.input_components + self.output_components, outputs=interpretation_set + [input_component_column, interpret_component_column], status_tracker=status_tracker, )
def __init__(self, fn, inputs=None, outputs=None, verbose=False, examples=None, examples_per_page=10, live=False, layout="horizontal", show_input=True, show_output=True, capture_session=False, interpretation=None, num_shap=2.0, theme=None, repeat_outputs_per_model=True, title=None, description=None, article=None, thumbnail=None, css=None, server_port=None, server_name=networking.LOCALHOST_NAME, height=500, width=900, allow_screenshot=True, allow_flagging=True, flagging_options=None, encrypt=False, show_tips=False, embedding=None, flagging_dir="flagged", analytics_enabled=True): """ Parameters: fn (Callable): the function to wrap an interface around. inputs (Union[str, List[Union[str, InputComponent]]]): a single Gradio input component, or list of Gradio input components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. The number of input components should match the number of parameters in fn. outputs (Union[str, List[Union[str, OutputComponent]]]): a single Gradio output component, or list of Gradio output components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. The number of output components should match the number of values returned by fn. verbose (bool): whether to print detailed information during launch. examples (Union[List[List[Any]], str]): sample inputs for the function; if provided, appears below the UI components and can be used to populate the interface. Should be nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component. A string path to a directory of examples can also be provided. If there are multiple input components, a log.csv file must be present in the directory to link corresponding inputs. examples_per_page (int): If examples are provided, how many to display per page. live (bool): whether the interface should automatically reload on change. layout (str): Layout of input and output panels. "horizontal" arranges them as two columns of equal height, "unaligned" arranges them as two columns of unequal height, and "vertical" arranges them vertically. capture_session (bool): if True, captures the default graph and session (needed for Tensorflow 1.x) interpretation (Union[Callable, str]): function that provides interpretation explaining prediction output. Pass "default" to use built-in interpreter. num_shap (float): a multiplier that determines how many examples are computed for shap-based interpretation. Increasing this value will increase shap runtime, but improve results. title (str): a title for the interface; if provided, appears above the input and output components. description (str): a description for the interface; if provided, appears above the input and output components. article (str): an expanded article explaining the interface; if provided, appears below the input and output components. Accepts Markdown and HTML content. thumbnail (str): path to image or src to use as display picture for models listed in gradio.app/hub theme (str): Theme to use - one of "default", "compact" or "huggingface". css (str): custom css or path to custom css file to use with interface. server_port (int): will start gradio app on this port (if available) server_name (str): to make app accessible on local network set to "0.0.0.0". allow_screenshot (bool): if False, users will not see a button to take a screenshot of the interface. allow_flagging (bool): if False, users will not see a button to flag an input and output. flagging_options (List[str]): if not None, provides options a user must select when flagging. encrypt (bool): If True, flagged data will be encrypted by key provided by creator at launch flagging_dir (str): what to name the dir where flagged data is stored. show_tips (bool): if True, will occasionally show tips about new Gradio features """ if not isinstance(fn, list): fn = [fn] if isinstance(inputs, list): self.input_components = [get_input_instance(i) for i in inputs] else: self.input_components = [get_input_instance(inputs)] if isinstance(outputs, list): self.output_components = [get_output_instance(i) for i in outputs] else: self.output_components = [get_output_instance(outputs)] if repeat_outputs_per_model: self.output_components *= len(fn) self.predict = fn self.function_names = [func.__name__ for func in fn] self.__name__ = ", ".join(self.function_names) self.verbose = verbose self.status = "OFF" self.live = live self.layout = layout self.show_input = show_input self.show_output = show_output self.flag_hash = random.getrandbits(32) self.capture_session = capture_session self.interpretation = interpretation self.session = None self.server_name = server_name self.title = title self.description = description if article is not None: article = utils.readme_to_html(article) article = markdown2.markdown(article) self.article = article self.thumbnail = thumbnail self.theme = theme if theme is not None else os.getenv( "GRADIO_THEME", "default") self.height = height self.width = width if css is not None and os.path.exists(css): with open(css) as css_file: self.css = css_file.read() else: self.css = css if examples is None or isinstance(examples, str) or ( isinstance(examples, list) and (len(examples) == 0 or isinstance(examples[0], list))): self.examples = examples else: raise ValueError( "Examples argument must either be a directory or a nested list, where each sublist represents a set of inputs." ) self.num_shap = num_shap self.examples_per_page = examples_per_page self.server_port = server_port self.simple_server = None self.allow_screenshot = allow_screenshot self.allow_flagging = os.getenv("GRADIO_FLAGGING") or allow_flagging self.flagging_options = flagging_options self.flagging_dir = flagging_dir self.encrypt = encrypt Interface.instances.add(self) self.analytics_enabled = analytics_enabled self.save_to = None self.share = None self.share_url = None self.local_url = None self.embedding = embedding self.show_tips = show_tips self.requires_permissions = any([ component.requires_permissions for component in self.input_components ]) data = { 'fn': fn, 'inputs': inputs, 'outputs': outputs, 'live': live, 'capture_session': capture_session, 'ip_address': ip_address, 'interpretation': interpretation, 'embedding': embedding, 'allow_flagging': allow_flagging, 'allow_screenshot': allow_screenshot, } if self.capture_session: try: import tensorflow as tf self.session = tf.get_default_graph(), \ tf.keras.backend.get_session() except (ImportError, AttributeError): # If they are using TF >= 2.0 or don't have TF, # just ignore this. pass if self.allow_flagging: os.makedirs(self.flagging_dir, exist_ok=True) if self.analytics_enabled: try: requests.post(analytics_url + 'gradio-initiated-analytics/', data=data, timeout=3) except (requests.ConnectionError, requests.exceptions.ReadTimeout): pass # do not push analytics if no network
def __init__(self, fn, inputs, outputs, verbose=False, examples=None, examples_per_page=10, live=False, layout="horizontal", show_input=True, show_output=True, capture_session=False, interpretation=None, title=None, description=None, article=None, thumbnail=None, css=None, server_port=7860, server_name=networking.LOCALHOST_NAME, allow_screenshot=True, allow_flagging=True, show_tips=True, embedding=None, flagging_dir="flagged", analytics_enabled=True): """ Parameters: fn (Callable): the function to wrap an interface around. inputs (Union[str, List[Union[str, InputComponent]]]): a single Gradio input component, or list of Gradio input components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. The number of input components should match the number of parameters in fn. outputs (Union[str, List[Union[str, OutputComponent]]]): a single Gradio output component, or list of Gradio output components. Components can either be passed as instantiated objects, or referred to by their string shortcuts. The number of output components should match the number of values returned by fn. verbose (bool): whether to print detailed information during launch. examples (List[List[Any]]): sample inputs for the function; if provided, appears below the UI components and can be used to populate the interface. Should be nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component. examples_per_page (int): If examples are provided, how many to display per page. live (bool): whether the interface should automatically reload on change. layout (str): Layout of input and output panels. "horizontal" arranges them as two columns of equal height, "unaligned" arranges them as two columns of unequal height, and "vertical" arranges them vertically. capture_session (bool): if True, captures the default graph and session (needed for Tensorflow 1.x) interpretation (Union[Callable, str]): function that provides interpretation explaining prediction output. Pass "default" to use built-in interpreter. title (str): a title for the interface; if provided, appears above the input and output components. description (str): a description for the interface; if provided, appears above the input and output components. article (str): an expanded article explaining the interface; if provided, appears below the input and output components. Accepts Markdown and HTML content. thumbnail (str): path to image or src to use as display picture for models listed in gradio.app/hub css (str): custom css or path to custom css file to use with interface. server_port (int): will start gradio app on this port (if available) server_name (str): to make app accessible on local network set to "0.0.0.0". allow_screenshot (bool): if False, users will not see a button to take a screenshot of the interface. allow_flagging (bool): if False, users will not see a button to flag an input and output. flagging_dir (str): what to name the dir where flagged data is stored. show_tips (bool): if True, will occasionally show tips about new Gradio features """ def get_input_instance(iface): if isinstance(iface, str): shortcut = InputComponent.get_all_shortcut_implementations( )[iface] return shortcut[0](**shortcut[1]) elif isinstance(iface, InputComponent): return iface else: raise ValueError("Input interface must be of type `str` or " "`InputComponent`") def get_output_instance(iface): if isinstance(iface, str): shortcut = OutputComponent.get_all_shortcut_implementations( )[iface] return shortcut[0](**shortcut[1]) elif isinstance(iface, OutputComponent): return iface else: raise ValueError("Output interface must be of type `str` or " "`OutputComponent`") if isinstance(inputs, list): self.input_interfaces = [get_input_instance(i) for i in inputs] else: self.input_interfaces = [get_input_instance(inputs)] if isinstance(outputs, list): self.output_interfaces = [get_output_instance(i) for i in outputs] else: self.output_interfaces = [get_output_instance(outputs)] if not isinstance(fn, list): fn = [fn] self.output_interfaces *= len(fn) self.predict = fn self.function_names = [func.__name__ for func in fn] self.verbose = verbose self.status = "OFF" self.live = live self.layout = layout self.show_input = show_input self.show_output = show_output self.flag_hash = random.getrandbits(32) self.capture_session = capture_session self.interpretation = interpretation self.session = None self.server_name = server_name self.title = title self.description = description if article is not None: article = utils.readme_to_html(article) article = markdown2.markdown(article) self.article = article self.thumbnail = thumbnail if css is not None and os.path.exists(css): with open(css) as css_file: self.css = css_file.read() else: self.css = css self.examples = examples self.examples_per_page = examples_per_page self.server_port = server_port self.simple_server = None self.allow_screenshot = allow_screenshot self.allow_flagging = os.getenv("GRADIO_FLAGGING") or allow_flagging self.flagging_dir = flagging_dir Interface.instances.add(self) self.analytics_enabled = analytics_enabled self.save_to = None self.share = None self.embedding = embedding self.show_tips = show_tips data = { 'fn': fn, 'inputs': inputs, 'outputs': outputs, 'live': live, 'capture_session': capture_session, 'ip_address': ip_address, 'interpretation': interpretation, 'embedding': embedding, 'allow_flagging': allow_flagging, 'allow_screenshot': allow_screenshot, } if self.capture_session: try: import tensorflow as tf self.session = tf.get_default_graph(), \ tf.keras.backend.get_session() except (ImportError, AttributeError): # If they are using TF >= 2.0 or don't have TF, # just ignore this. pass if self.allow_flagging: os.makedirs(self.flagging_dir, exist_ok=True) if self.analytics_enabled: try: requests.post(analytics_url + 'gradio-initiated-analytics/', data=data) except requests.ConnectionError: pass # do not push analytics if no network