diff --git a/build/lib/gradio/interface.py b/build/lib/gradio/interface.py index 232acc1b3e..ceca42a1c1 100644 --- a/build/lib/gradio/interface.py +++ b/build/lib/gradio/interface.py @@ -17,7 +17,7 @@ import random import time from IPython import get_ipython -LOCALHOST_IP = "127.0.0.1" +LOCALHOST_IP = "0.0.0.0" TRY_NUM_PORTS = 100 PKG_VERSION_URL = "https://gradio.app/api/pkg-version" @@ -29,7 +29,8 @@ class Interface: """ def __init__(self, fn, inputs, outputs, saliency=None, verbose=False, - live=False, show_input=True, show_output=True): + live=False, show_input=True, show_output=True, + load_fn=None, server_name=LOCALHOST_IP): """ :param fn: a function that will process the input panel data from the interface and return the output panel data. :param inputs: a string or `AbstractInput` representing the input interface. @@ -63,6 +64,8 @@ class Interface: fn = [fn] self.output_interfaces *= len(fn) self.predict = fn + self.load_fn = load_fn + self.context = None self.verbose = verbose self.status = "OFF" self.saliency = saliency @@ -70,6 +73,7 @@ class Interface: self.show_input = show_input self.show_output = show_output self.flag_hash = random.getrandbits(32) + self.server_name = server_name def update_config_file(self, output_directory): config = { @@ -148,6 +152,8 @@ class Interface: """ # if validate and not self.validate_flag: # self.validate() + context = self.load_fn() if self.load_fn else None + self.context = context # If an existing interface is running with this instance, close it. if self.status == "RUNNING": @@ -161,8 +167,8 @@ class Interface: 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) - path_to_local_server = "http://localhost:{}/".format(server_port) + server_port, httpd = networking.start_simple_server(self, output_directory, self.server_name) + path_to_local_server = "http://{}:{}/".format(self.server_name, server_port) networking.build_template(output_directory) self.update_config_file(output_directory) diff --git a/build/lib/gradio/networking.py b/build/lib/gradio/networking.py index ac8e2c99bd..d9fd08279e 100644 --- a/build/lib/gradio/networking.py +++ b/build/lib/gradio/networking.py @@ -20,7 +20,7 @@ INITIAL_PORT_VALUE = ( TRY_NUM_PORTS = ( 100 ) # Number of ports to try before giving up and throwing an exception. -LOCALHOST_NAME = "localhost" +LOCALHOST_NAME = "0.0.0.0" GRADIO_API_SERVER = "https://api.gradio.app/v1/tunnel-request" STATIC_TEMPLATE_LIB = pkg_resources.resource_filename("gradio", "templates/") @@ -109,7 +109,7 @@ def get_first_available_port(initial, final): ) -def serve_files_in_background(interface, port, directory_to_serve=None): +def serve_files_in_background(interface, port, directory_to_serve=None, server_name=LOCALHOST_NAME): class HTTPHandler(SimpleHTTPRequestHandler): """This handler uses server.base_path instead of always using os.getcwd()""" @@ -139,7 +139,11 @@ def serve_files_in_background(interface, port, directory_to_serve=None): processed_input = [input_interface.preprocess(raw_input[i]) for i, input_interface in enumerate(interface.input_interfaces)] predictions = [] for predict_fn in interface.predict: - prediction = predict_fn(*processed_input) + if interface.context: + prediction = predict_fn(*processed_input, + interface.context) + else: + prediction = predict_fn(*processed_input) if len(interface.output_interfaces) / len(interface.predict) == 1: prediction = [prediction] predictions.extend(prediction) @@ -260,7 +264,7 @@ def serve_files_in_background(interface, port, directory_to_serve=None): self.base_path = base_path BaseHTTPServer.__init__(self, server_address, RequestHandlerClass) - httpd = HTTPServer(directory_to_serve, (LOCALHOST_NAME, port)) + httpd = HTTPServer(directory_to_serve, (server_name, port)) # Now loop forever def serve_forever(): @@ -277,11 +281,11 @@ def serve_files_in_background(interface, port, directory_to_serve=None): return httpd -def start_simple_server(interface, directory_to_serve=None): +def start_simple_server(interface, directory_to_serve=None, server_name=None): port = get_first_available_port( INITIAL_PORT_VALUE, INITIAL_PORT_VALUE + TRY_NUM_PORTS ) - httpd = serve_files_in_background(interface, port, directory_to_serve) + httpd = serve_files_in_background(interface, port, directory_to_serve, server_name) return port, httpd diff --git a/dist/gradio-0.9.1-py3-none-any.whl b/dist/gradio-0.9.1-py3-none-any.whl deleted file mode 100644 index a36167412d..0000000000 Binary files a/dist/gradio-0.9.1-py3-none-any.whl and /dev/null differ diff --git a/dist/gradio-0.9.1-py3.7.egg b/dist/gradio-0.9.1-py3.7.egg deleted file mode 100644 index 35d678fc3a..0000000000 Binary files a/dist/gradio-0.9.1-py3.7.egg and /dev/null differ diff --git a/gradio.egg-info/PKG-INFO b/gradio.egg-info/PKG-INFO index 0d6571b7e5..c5b37f2a7f 100644 --- a/gradio.egg-info/PKG-INFO +++ b/gradio.egg-info/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 1.0 Name: gradio -Version: 0.9.1 +Version: 0.9.2 Summary: Python library for easily interacting with trained machine learning models Home-page: https://github.com/abidlabs/gradio Author: Abubakar Abid diff --git a/gradio/interface.py b/gradio/interface.py index 5dcfcf4890..c52343e497 100644 --- a/gradio/interface.py +++ b/gradio/interface.py @@ -18,7 +18,7 @@ import time from IPython import get_ipython import tensorflow as tf -LOCALHOST_IP = "127.0.0.1" +LOCALHOST_IP = "0.0.0.0" TRY_NUM_PORTS = 100 PKG_VERSION_URL = "https://gradio.app/api/pkg-version" @@ -30,8 +30,9 @@ class Interface: """ def __init__(self, fn, inputs, outputs, saliency=None, verbose=False, - live=False, show_input=True, show_output=True, - load_fn=None, capture_session=False): + live=False, show_input=True, show_output=True, + load_fn=None, capture_session=False, + server_name=LOCALHOST_IP): """ :param fn: a function that will process the input panel data from the interface and return the output panel data. :param inputs: a string or `AbstractInput` representing the input interface. @@ -43,7 +44,9 @@ class Interface: elif isinstance(iface, gradio.inputs.AbstractInput): return iface else: - raise ValueError("Input interface must be of type `str` or `AbstractInput`") + raise ValueError("Input interface must be of type `str` or " + "`AbstractInput`") + def get_output_instance(iface): if isinstance(iface, str): return gradio.outputs.shortcuts[iface] @@ -51,7 +54,8 @@ class Interface: return iface else: raise ValueError( - "Output interface must be of type `str` or `AbstractOutput`" + "Output interface must be of type `str` or " + "`AbstractOutput`" ) if isinstance(inputs, list): self.input_interfaces = [get_input_instance(i) for i in inputs] @@ -76,6 +80,7 @@ class Interface: self.flag_hash = random.getrandbits(32) self.capture_session = capture_session self.session = None + self.server_name = server_name def update_config_file(self, output_directory): config = { @@ -173,8 +178,8 @@ class Interface: 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) - path_to_local_server = "http://localhost:{}/".format(server_port) + server_port, httpd = networking.start_simple_server(self, output_directory, self.server_name) + path_to_local_server = "http://{}:{}/".format(self.server_name, server_port) networking.build_template(output_directory) self.update_config_file(output_directory) @@ -247,7 +252,8 @@ class Interface: if ( is_colab - ): # Embed the remote interface page if on google colab; otherwise, embed the local page. + ): # Embed the remote interface page if on google colab; + # otherwise, embed the local page. display(IFrame(share_url, width=1000, height=500)) else: display(IFrame(path_to_local_server, width=1000, height=500)) diff --git a/gradio/networking.py b/gradio/networking.py index 8e334c7578..72912e9a9e 100644 --- a/gradio/networking.py +++ b/gradio/networking.py @@ -20,7 +20,7 @@ INITIAL_PORT_VALUE = ( TRY_NUM_PORTS = ( 100 ) # Number of ports to try before giving up and throwing an exception. -LOCALHOST_NAME = "localhost" +LOCALHOST_NAME = "0.0.0.0" GRADIO_API_SERVER = "https://api.gradio.app/v1/tunnel-request" STATIC_TEMPLATE_LIB = pkg_resources.resource_filename("gradio", "templates/") @@ -109,7 +109,7 @@ def get_first_available_port(initial, final): ) -def serve_files_in_background(interface, port, directory_to_serve=None): +def serve_files_in_background(interface, port, directory_to_serve=None, server_name=LOCALHOST_NAME): class HTTPHandler(SimpleHTTPRequestHandler): """This handler uses server.base_path instead of always using os.getcwd()""" @@ -278,7 +278,7 @@ def serve_files_in_background(interface, port, directory_to_serve=None): self.base_path = base_path BaseHTTPServer.__init__(self, server_address, RequestHandlerClass) - httpd = HTTPServer(directory_to_serve, (LOCALHOST_NAME, port)) + httpd = HTTPServer(directory_to_serve, (server_name, port)) # Now loop forever def serve_forever(): @@ -295,11 +295,11 @@ def serve_files_in_background(interface, port, directory_to_serve=None): return httpd -def start_simple_server(interface, directory_to_serve=None): +def start_simple_server(interface, directory_to_serve=None, server_name=None): port = get_first_available_port( INITIAL_PORT_VALUE, INITIAL_PORT_VALUE + TRY_NUM_PORTS ) - httpd = serve_files_in_background(interface, port, directory_to_serve) + httpd = serve_files_in_background(interface, port, directory_to_serve, server_name) return port, httpd diff --git a/setup.py b/setup.py index a2d1a02822..cc2836cd01 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ except ImportError: setup( name='gradio', - version='0.9.1', + version='0.9.2', include_package_data=True, description='Python library for easily interacting with trained machine learning models', author='Abubakar Abid',