def run_until_interrupted(self, thread, path_to_local_server): try: while 1: pass except (KeyboardInterrupt, OSError): print("Keyboard interruption in main thread... closing server.") thread.keep_running = False networking.url_ok(path_to_local_server) # Hit the server one more time to close it
def run_until_interrupted(self, thread, path_to_local_server): try: while True: time.sleep(0.5) except (KeyboardInterrupt, OSError): print("Keyboard interruption in main thread... closing server.") thread.keep_running = False networking.url_ok(path_to_local_server ) # Hit the server one more time to close it if self.enable_queue: queue.close()
def launch(self, inline=None, inbrowser=None, share=False, debug=False, auth=None): """ Parameters: inline (bool): whether to display in the interface inline on python notebooks. inbrowser (bool): whether to automatically launch the interface in a new tab on the default browser. share (bool): whether to create a publicly shareable link from your computer for the interface. debug (bool): if True, and the interface was launched from Google Colab, prints the errors in the cell output. auth (Tuple[str, str]): If provided, username and password required to access interface. Returns: app (flask.Flask): Flask app object path_to_local_server (str): Locally accessible link share_url (str): Publicly accessible link (if share=True) """ # Alert user if a more recent version of the library exists utils.version_check() # Set up local flask server config = self.get_config_file() self.config = config self.auth = auth # Launch local flask server server_port, app, thread = networking.start_server( self, self.server_name, self.server_port, self.auth) path_to_local_server = "http://{}:{}/".format(self.server_name, server_port) self.server_port = server_port self.status = "RUNNING" self.server = app # Count number of launches launch_counter() # If running in a colab or not able to access localhost, automatically create a shareable link is_colab = utils.colab_check() if is_colab or not (networking.url_ok(path_to_local_server)): share = True if debug: print(strings.en["COLAB_DEBUG_TRUE"]) else: print(strings.en["COLAB_DEBUG_FALSE"]) else: print(strings.en["RUNNING_LOCALLY"].format(path_to_local_server)) # Set up shareable link self.share = share if share: print(strings.en["SHARE_LINK_MESSAGE"]) try: share_url = networking.setup_tunnel(server_port) print(strings.en["SHARE_LINK_DISPLAY"].format(share_url)) except RuntimeError: send_error_analytics(self.analytics_enabled) share_url = None else: print(strings.en["PUBLIC_SHARE_TRUE"]) share_url = None # Open a browser tab with the interface. if inbrowser: if share: webbrowser.open(share_url) else: webbrowser.open(path_to_local_server) # Check if running in a Python notebook in which case, display inline if inline is None: inline = utils.ipython_check() if inline: from IPython.display import IFrame, display # Embed the remote interface page if on google colab; otherwise, embed the local page. print(strings.en["INLINE_DISPLAY_BELOW"]) if share: while not networking.url_ok(share_url): time.sleep(1) display(IFrame(share_url, width=1000, height=500)) else: display(IFrame(path_to_local_server, width=1000, height=500)) send_launch_analytics(analytics_enabled=self.analytics_enabled, inbrowser=inbrowser, is_colab=is_colab, share=share, share_url=share_url) show_tip(self) # Run server perpetually under certain circumstances if debug or int(os.getenv('GRADIO_DEBUG', 0)) == 1: while True: sys.stdout.flush() time.sleep(0.1) is_in_interactive_mode = bool( getattr(sys, 'ps1', sys.flags.interactive)) if not is_in_interactive_mode: self.run_until_interrupted(thread, path_to_local_server) return app, path_to_local_server, share_url
def launch(self, inline=None, inbrowser=None, share=False, debug=False): """ Parameters inline (bool): whether to display in the interface inline on python notebooks. inbrowser (bool): whether to automatically launch the interface in a new tab on the default browser. share (bool): whether to create a publicly shareable link from your computer for the interface. debug (bool): if True, and the interface was launched from Google Colab, prints the errors in the cell output. :returns httpd (str): HTTPServer object path_to_local_server (str): Locally accessible link share_url (str): Publicly accessible link (if share=True) """ output_directory = tempfile.mkdtemp() # Set up a port to serve the directory containing the static files with interface. server_port, httpd = networking.start_simple_server( self, output_directory, self.server_name, server_port=self.server_port) path_to_local_server = "http://{}:{}/".format(self.server_name, server_port) networking.build_template(output_directory) self.server_port = server_port self.status = "RUNNING" self.simple_server = httpd try: current_pkg_version = pkg_resources.require("gradio")[0].version latest_pkg_version = requests.get( url=PKG_VERSION_URL).json()["version"] if StrictVersion(latest_pkg_version) > StrictVersion( current_pkg_version): print("IMPORTANT: You are using gradio version {}, " "however version {} " "is available, please upgrade.".format( current_pkg_version, latest_pkg_version)) print('--------') except: # TODO(abidlabs): don't catch all exceptions pass is_colab = utils.colab_check() if not is_colab: print(strings.en["RUNNING_LOCALLY"].format(path_to_local_server)) else: if debug: print( "Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. " "To turn off, set debug=False in launch().") else: print( "Colab notebook detected. To show errors in colab notebook, set debug=True in launch()" ) if share: try: share_url = networking.setup_tunnel(server_port) print("Running on External URL:", share_url) except RuntimeError: utils.error_analytics("RuntimeError") share_url = None if self.verbose: print(strings.en["NGROK_NO_INTERNET"]) else: if is_colab: # For a colab notebook, create a public link even if # share is False. share_url = networking.setup_tunnel(server_port) print("Running on External URL:", share_url) if self.verbose: print(strings.en["COLAB_NO_LOCAL"]) else: # If it's not a colab notebook and share=False, print a message telling them about the share option. if self.verbose: print(strings.en["PUBLIC_SHARE_TRUE"]) share_url = None if inline is None: inline = utils.ipython_check() if inbrowser is None: # if interface won't appear inline, open it in new tab, # otherwise keep it inline inbrowser = not inline else: if inbrowser is None: inbrowser = False if inbrowser and not is_colab: webbrowser.open(path_to_local_server) # Open a browser tab # with the interface. if inline: from IPython.display import IFrame, display if (is_colab): # Embed the remote interface page if on google colab; # otherwise, embed the local page. print("Interface loading below...") while not networking.url_ok(share_url): time.sleep(1) display(IFrame(share_url, width=1000, height=500)) else: display(IFrame(path_to_local_server, width=1000, height=500)) config = self.get_config_file() config["share_url"] = share_url processed_examples = [] if self.examples is not None: for example_set in self.examples: processed_set = [] for iface, example in zip(self.input_interfaces, example_set): processed_set.append(iface.process_example(example)) processed_examples.append(processed_set) config["examples"] = processed_examples networking.set_config(config, output_directory) networking.set_meta_tags(output_directory, self.title, self.description, self.thumbnail) if debug: while True: sys.stdout.flush() time.sleep(0.1) launch_method = 'browser' if inbrowser else 'inline' data = { 'launch_method': launch_method, 'is_google_colab': is_colab, 'is_sharing_on': share, 'share_url': share_url, 'ip_address': ip_address } try: requests.post(analytics_url + 'gradio-launched-analytics/', data=data) except requests.ConnectionError: pass # do not push analytics if no network return httpd, path_to_local_server, share_url
def launch(self, inline=None, inbrowser=None, share=False, debug=False): """ Parameters: inline (bool): whether to display in the interface inline on python notebooks. inbrowser (bool): whether to automatically launch the interface in a new tab on the default browser. share (bool): whether to create a publicly shareable link from your computer for the interface. debug (bool): if True, and the interface was launched from Google Colab, prints the errors in the cell output. Returns: app (flask.Flask): Flask app object path_to_local_server (str): Locally accessible link share_url (str): Publicly accessible link (if share=True) """ config = self.get_config_file() networking.set_config(config) networking.set_meta_tags(self.title, self.description, self.thumbnail) server_port, app, thread = networking.start_server( self, self.server_name, self.server_port) path_to_local_server = "http://{}:{}/".format(self.server_name, server_port) self.status = "RUNNING" self.server = app utils.version_check() is_colab = utils.colab_check() if is_colab: share = True if not is_colab: if not networking.url_ok(path_to_local_server): share = True else: print(strings.en["RUNNING_LOCALLY"].format(path_to_local_server)) else: if debug: print("Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. " "To turn off, set debug=False in launch().") else: print("Colab notebook detected. To show errors in colab notebook, set debug=True in launch()") if share: print("This share link will expire in 6 hours. If you need a " "permanent link, email [email protected]") try: share_url = networking.setup_tunnel(self.launch_port) print("Running on External URL:", share_url) except RuntimeError: data = {'error': 'RuntimeError in launch method'} if self.analytics_enabled: try: requests.post(analytics_url + 'gradio-error-analytics/', data=data) except requests.ConnectionError: pass # do not push analytics if no network share_url = None if self.verbose: print(strings.en["NGROK_NO_INTERNET"]) else: print("To get a public link for a hosted model, " "set Share=True") if self.verbose: print(strings.en["PUBLIC_SHARE_TRUE"]) share_url = None if inline is None: inline = utils.ipython_check() if inbrowser is None: # if interface won't appear inline, open it in new tab, # otherwise keep it inline inbrowser = not inline else: if inbrowser is None: inbrowser = False if inbrowser and not is_colab: webbrowser.open(path_to_local_server) # Open a browser tab # with the interface. if inline: from IPython.display import IFrame, display # Embed the remote interface page if on google colab; # otherwise, embed the local page. print("Interface loading below...") if share: while not networking.url_ok(share_url): time.sleep(1) display(IFrame(share_url, width=1000, height=500)) else: display(IFrame(path_to_local_server, width=1000, height=500)) r = requests.get(path_to_local_server + "enable_sharing/" + (share_url or "None")) if debug: while True: sys.stdout.flush() time.sleep(0.1) launch_method = 'browser' if inbrowser else 'inline' if self.analytics_enabled: data = { 'launch_method': launch_method, 'is_google_colab': is_colab, 'is_sharing_on': share, 'share_url': share_url, 'ip_address': ip_address } try: requests.post(analytics_url + 'gradio-launched-analytics/', data=data) except requests.ConnectionError: pass # do not push analytics if no network is_in_interactive_mode = bool(getattr(sys, 'ps1', sys.flags.interactive)) if not is_in_interactive_mode: self.run_until_interrupted(thread, path_to_local_server) return app, path_to_local_server, share_url
def launch(self, inline=None, inbrowser=None, share=False, debug=False, auth=None, auth_message=None, private_endpoint=None, prevent_thread_lock=False): """ Parameters: inline (bool): whether to display in the interface inline on python notebooks. inbrowser (bool): whether to automatically launch the interface in a new tab on the default browser. share (bool): whether to create a publicly shareable link from your computer for the interface. debug (bool): if True, and the interface was launched from Google Colab, prints the errors in the cell output. auth (Callable, Union[Tuple[str, str], List[Tuple[str, str]]]): If provided, username and password (or list of username-password tuples) required to access interface. Can also provide function that takes username and password and return True if valid login. auth_message (str): If provided, HTML message provided on login page. Returns: app (flask.Flask): Flask app object path_to_local_server (str): Locally accessible link share_url (str): Publicly accessible link (if share=True) """ # Alert user if a more recent version of the library exists utils.version_check() # Set up local flask server config = self.get_config_file() self.config = config if auth and not callable(auth) and not isinstance( auth[0], tuple) and not isinstance(auth[0], list): auth = [auth] self.auth = auth self.auth_message = auth_message # Request key for encryption if self.encrypt: self.encryption_key = encryptor.get_key( getpass("Enter key for encryption: ")) # Launch local flask server server_port, app, thread = networking.start_server( self, self.server_name, self.server_port, self.auth) path_to_local_server = "http://{}:{}/".format(self.server_name, server_port) self.local_url = path_to_local_server self.server_port = server_port self.status = "RUNNING" self.server = app # Count number of launches launch_counter() # If running in a colab or not able to access localhost, automatically create a shareable link is_colab = utils.colab_check() if is_colab or not (networking.url_ok(path_to_local_server)): share = True if is_colab: if debug: print(strings.en["COLAB_DEBUG_TRUE"]) else: print(strings.en["COLAB_DEBUG_FALSE"]) else: print(strings.en["RUNNING_LOCALLY"].format(path_to_local_server)) if is_colab and self.requires_permissions: print(strings.en["MEDIA_PERMISSIONS_IN_COLAB"]) if private_endpoint is not None: share = True # Set up shareable link self.share = share if share: if not private_endpoint: print(strings.en["SHARE_LINK_MESSAGE"]) try: share_url = networking.setup_tunnel(server_port, private_endpoint) self.share_url = share_url print(strings.en["SHARE_LINK_DISPLAY"].format(share_url)) except RuntimeError: send_error_analytics(self.analytics_enabled) share_url = None else: print(strings.en["PUBLIC_SHARE_TRUE"]) share_url = None # Open a browser tab with the interface. if inbrowser: if share: webbrowser.open(share_url) else: webbrowser.open(path_to_local_server) # Check if running in a Python notebook in which case, display inline if inline is None: inline = utils.ipython_check() if inline: try: from IPython.display import IFrame, display # Embed the remote interface page if on google colab; otherwise, embed the local page. print(strings.en["INLINE_DISPLAY_BELOW"]) if share: while not networking.url_ok(share_url): time.sleep(1) display( IFrame(share_url, width=self.width, height=self.height)) else: display( IFrame(path_to_local_server, width=self.width, height=self.height)) except ImportError: pass # IPython is not available so does not print inline. send_launch_analytics(analytics_enabled=self.analytics_enabled, inbrowser=inbrowser, is_colab=is_colab, share=share, share_url=share_url) show_tip(self) # Run server perpetually under certain circumstances if debug or int(os.getenv('GRADIO_DEBUG', 0)) == 1: while True: sys.stdout.flush() time.sleep(0.1) is_in_interactive_mode = bool( getattr(sys, 'ps1', sys.flags.interactive)) if not prevent_thread_lock and not is_in_interactive_mode: self.run_until_interrupted(thread, path_to_local_server) return app, path_to_local_server, share_url
def launch( self, inline: bool = None, inbrowser: bool = None, share: bool = False, debug: bool = False, auth: Optional[Callable | Tuple[str, str] | List[Tuple[str, str]]] = None, auth_message: Optional[str] = None, private_endpoint: Optional[str] = None, prevent_thread_lock: bool = False, show_error: bool = True, server_name: Optional[str] = None, server_port: Optional[int] = None, show_tips: bool = False, enable_queue: bool = False, height: int = 500, width: int = 900, encrypt: bool = False, favicon_path: Optional[str] = None, ssl_keyfile: Optional[str] = None, ssl_certfile: Optional[str] = None, ssl_keyfile_password: Optional[str] = None, ) -> Tuple[FastAPI, str, str]: """ Launches the webserver that serves the UI for the interface. Parameters: inline (bool): whether to display in the interface inline on python notebooks. inbrowser (bool): whether to automatically launch the interface in a new tab on the default browser. share (bool): whether to create a publicly shareable link from your computer for the interface. debug (bool): if True, and the interface was launched from Google Colab, prints the errors in the cell output. auth (Callable, Union[Tuple[str, str], List[Tuple[str, str]]]): If provided, username and password (or list of username-password tuples) required to access interface. Can also provide function that takes username and password and returns True if valid login. auth_message (str): If provided, HTML message provided on login page. private_endpoint (str): If provided, the public URL of the interface will be this endpoint (should generally be unchanged). prevent_thread_lock (bool): If True, the interface will block the main thread while the server is running. show_error (bool): If True, any errors in the interface will be printed in the browser console log server_port (int): will start gradio app on this port (if available). Can be set by environment variable GRADIO_SERVER_PORT. server_name (str): to make app accessible on local network, set this to "0.0.0.0". Can be set by environment variable GRADIO_SERVER_NAME. show_tips (bool): if True, will occasionally show tips about new Gradio features enable_queue (bool): if True, inference requests will be served through a queue instead of with parallel threads. Required for longer inference times (> 1min) to prevent timeout. width (int): The width in pixels of the iframe element containing the interface (used if inline=True) height (int): The height in pixels of the iframe element containing the interface (used if inline=True) encrypt (bool): If True, flagged data will be encrypted by key provided by creator at launch favicon_path (str): If a path to a file (.png, .gif, or .ico) is provided, it will be used as the favicon for the web page. ssl_keyfile (str): If a path to a file is provided, will use this as the private key file to create a local server running on https. ssl_certfile (str): If a path to a file is provided, will use this as the signed certificate for https. Needs to be provided if ssl_keyfile is provided. ssl_keyfile_password (str): If a password is provided, will use this with the ssl certificate for https. Returns: app (FastAPI): FastAPI app object that is running the demo local_url (str): Locally accessible link to the demo share_url (str): Publicly accessible link to the demo (if share=True, otherwise None) """ self.config = self.get_config_file() if (auth and not callable(auth) and not isinstance(auth[0], tuple) and not isinstance(auth[0], list)): auth = [auth] self.auth = auth self.auth_message = auth_message self.show_tips = show_tips self.show_error = show_error self.height = height self.width = width self.favicon_path = favicon_path if hasattr(self, "encrypt") and self.encrypt is None: self.encrypt = encrypt if hasattr(self, "encrypt") and self.encrypt: self.encryption_key = encryptor.get_key( getpass.getpass("Enter key for encryption: ")) if hasattr(self, "enable_queue") and self.enable_queue is None: self.enable_queue = enable_queue config = self.get_config_file() self.config = config if self.is_running: self.server_app.launchable = self print( "Rerunning server... use `close()` to stop if you need to change `launch()` parameters.\n----" ) else: server_port, path_to_local_server, app, server = networking.start_server( self, server_name, server_port, ssl_keyfile, ssl_certfile, ssl_keyfile_password, ) self.local_url = path_to_local_server self.server_port = server_port self.server_app = app self.server = server self.is_running = True utils.launch_counter() # If running in a colab or not able to access localhost, # automatically create a shareable link. is_colab = utils.colab_check() if is_colab or not (networking.url_ok(self.local_url)): share = True if is_colab: if debug: print(strings.en["COLAB_DEBUG_TRUE"]) else: print(strings.en["COLAB_DEBUG_FALSE"]) else: print(strings.en["RUNNING_LOCALLY"].format(self.local_url)) if is_colab and self.requires_permissions: print(strings.en["MEDIA_PERMISSIONS_IN_COLAB"]) if private_endpoint is not None: share = True if share: if self.is_space: raise RuntimeError( "Share is not supported when you are in Spaces") try: if self.share_url is None: share_url = networking.setup_tunnel( self.server_port, private_endpoint) self.share_url = share_url print(strings.en["SHARE_LINK_DISPLAY"].format(self.share_url)) if private_endpoint: print(strings.en["PRIVATE_LINK_MESSAGE"]) else: print(strings.en["SHARE_LINK_MESSAGE"]) except RuntimeError: if self.analytics_enabled: utils.error_analytics(self.ip_address, "Not able to set up tunnel") self.share_url = None share = False print(strings.en["COULD_NOT_GET_SHARE_LINK"]) else: print(strings.en["PUBLIC_SHARE_TRUE"]) self.share_url = None self.share = share if inbrowser: link = self.share_url if share else self.local_url webbrowser.open(link) # Check if running in a Python notebook in which case, display inline if inline is None: inline = utils.ipython_check() and (auth is None) if inline: if auth is not None: print("Warning: authentication is not supported inline. Please" "click the link to access the interface in a new tab.") try: from IPython.display import IFrame, display # type: ignore if share: while not networking.url_ok(self.share_url): time.sleep(1) display( IFrame(self.share_url, width=self.width, height=self.height)) else: display( IFrame(self.local_url, width=self.width, height=self.height)) except ImportError: pass data = { "launch_method": "browser" if inbrowser else "inline", "is_google_colab": is_colab, "is_sharing_on": share, "share_url": self.share_url, "ip_address": self.ip_address, "enable_queue": self.enable_queue, "show_tips": self.show_tips, "server_name": server_name, "server_port": server_port, "is_spaces": self.is_space, "mode": self.mode, } if hasattr(self, "analytics_enabled") and self.analytics_enabled: utils.launch_analytics(data) utils.show_tip(self) # Block main thread if debug==True if debug or int(os.getenv("GRADIO_DEBUG", 0)) == 1: self.block_thread() # Block main thread if running in a script to stop script from exiting is_in_interactive_mode = bool( getattr(sys, "ps1", sys.flags.interactive)) if not prevent_thread_lock and not is_in_interactive_mode: self.block_thread() return self.server_app, self.local_url, self.share_url
def test_url_ok(self): res = networking.url_ok("https://www.gradio.app") self.assertTrue(res)