From 490be6bc18daa98d23a493d5a48183688e26bee9 Mon Sep 17 00:00:00 2001 From: dawoodkhan82 Date: Wed, 5 Aug 2020 13:25:40 -0400 Subject: [PATCH 1/4] analytics flag --- gradio/interface.py | 74 +++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/gradio/interface.py b/gradio/interface.py index d8ed3ae0c3..9f7e44cb2f 100644 --- a/gradio/interface.py +++ b/gradio/interface.py @@ -20,7 +20,7 @@ from IPython import get_ipython import sys import weakref import analytics - +import inspect PKG_VERSION_URL = "https://gradio.app/api/pkg-version" analytics.write_key = "uxIFddIEuuUcFLf9VgH2teTEtPlWdkNy" @@ -41,7 +41,8 @@ class Interface: def __init__(self, fn, inputs, outputs, saliency=None, verbose=False, examples=None, live=False, show_input=True, show_output=True, capture_session=False, title=None, description=None, - thumbnail=None, server_name=networking.LOCALHOST_NAME): + thumbnail=None, server_name=networking.LOCALHOST_NAME, + analytics_enabled=True): """ :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. @@ -95,6 +96,7 @@ class Interface: self.server_port = None self.simple_server = None Interface.instances.add(self) + self.analytics_enabled=analytics_enabled data = {'fn': fn, 'inputs': inputs, @@ -113,11 +115,12 @@ class Interface: except (ImportError, AttributeError): # If they are using TF >= 2.0 or don't have TF, just ignore this. pass - try: - requests.post(analytics_url + 'gradio-initiated-analytics/', - data=data) - except requests.ConnectionError: - pass # do not push analytics if no network + 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 def get_config_file(self): config = { @@ -214,11 +217,12 @@ class Interface: prediction = self.predict(processed_input) except Exception as e: data = {'error': e} - try: - requests.post(analytics_url + 'gradio-error-analytics/', - data=data) - except requests.ConnectionError: - pass # do not push analytics if no network + 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 if self.verbose: print("\n----------") print( @@ -230,11 +234,12 @@ class Interface: _ = self.output_interface.postprocess(prediction) except Exception as e: data = {'error': e} - try: - requests.post(analytics_url + 'gradio-error-analytics/', - data=data) - except requests.ConnectionError: - pass # do not push analytics if no network + 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 if self.verbose: print("\n----------") print( @@ -283,11 +288,12 @@ class Interface: is_colab = True except NameError: data = {'error': 'NameError in launch method'} - try: - requests.post(analytics_url + 'gradio-error-analytics/', - data=data) - except requests.ConnectionError: - pass # do not push analytics if no network + 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 pass try: @@ -312,16 +318,19 @@ class Interface: 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 support@gradio.app") try: share_url = networking.setup_tunnel(server_port) print("Running on External URL:", share_url) except RuntimeError: data = {'error': 'RuntimeError in launch method'} - try: - requests.post(analytics_url + 'gradio-error-analytics/', - data=data) - except requests.ConnectionError: - pass # do not push analytics if no network + 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"]) @@ -396,11 +405,12 @@ class Interface: '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 + if self.analytics_enabled: + 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 @classmethod From 687b5ca03a27fed386811f0db0eb6dd6da472baa Mon Sep 17 00:00:00 2001 From: dawoodkhan82 Date: Mon, 10 Aug 2020 12:16:04 -0400 Subject: [PATCH 2/4] remove prediction analytics --- gradio/networking.py | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/gradio/networking.py b/gradio/networking.py index 4cf0072ae9..c2c0620849 100644 --- a/gradio/networking.py +++ b/gradio/networking.py @@ -15,7 +15,6 @@ import urllib.request from shutil import copyfile import requests import sys -import analytics import csv @@ -37,9 +36,6 @@ CONFIG_FILE = "static/config.json" ASSOCIATION_PATH_IN_STATIC = "static/apple-app-site-association" ASSOCIATION_PATH_IN_ROOT = "apple-app-site-association" -analytics.write_key = "uxIFddIEuuUcFLf9VgH2teTEtPlWdkNy" -analytics_url = 'https://api.gradio.app/' - def build_template(temp_dir): """ @@ -127,21 +123,6 @@ def get_first_available_port(initial, final): ) -def send_prediction_analytics(interface): - data = {'title': interface.title, - 'description': interface.description, - 'thumbnail': interface.thumbnail, - 'input_interface': interface.input_interfaces, - 'output_interface': interface.output_interfaces, - } - try: - requests.post( - analytics_url + 'gradio-prediction-analytics/', - data=data) - except requests.ConnectionError: - pass # do not push analytics if no network - - 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()""" @@ -173,10 +154,6 @@ def serve_files_in_background(interface, port, directory_to_serve=None, server_n output = {"data": prediction, "durations": durations} self.wfile.write(json.dumps(output).encode()) - analytics_thread = threading.Thread( - target=send_prediction_analytics, args=[interface]) - analytics_thread.start() - elif self.path == "/api/flag/": self._set_headers() data_string = self.rfile.read( From 6aba316a2c405f25ec81372f6319bfc42af5d948 Mon Sep 17 00:00:00 2001 From: dawoodkhan82 Date: Mon, 10 Aug 2020 12:27:29 -0400 Subject: [PATCH 3/4] disable analytics on tests --- test/test_interface.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/test_interface.py b/test/test_interface.py index 7d1a5c3695..6ed9499b56 100644 --- a/test/test_interface.py +++ b/test/test_interface.py @@ -7,24 +7,28 @@ import gradio.outputs class TestInterface(unittest.TestCase): def test_input_output_mapping(self): - io = gr.Interface(inputs='SketCHPad', outputs='TexT', fn=lambda x: x) + io = gr.Interface(inputs='SketCHPad', outputs='TexT', fn=lambda x: + x, analytics_enabled=False) self.assertIsInstance(io.input_interfaces[0], gradio.inputs.Sketchpad) self.assertIsInstance(io.output_interfaces[0], gradio.outputs.Textbox) def test_input_interface_is_instance(self): inp = gradio.inputs.Image() - io = gr.Interface(inputs=inp, outputs='teXT', fn=lambda x: x) + io = gr.Interface(inputs=inp, outputs='teXT', fn=lambda x: x, + analytics_enabled=False) self.assertEqual(io.input_interfaces[0], inp) def test_output_interface_is_instance(self): out = gradio.outputs.Label() - io = gr.Interface(inputs='SketCHPad', outputs=out, fn=lambda x: x) + io = gr.Interface(inputs='SketCHPad', outputs=out, fn=lambda x: x, + analytics_enabled=False) self.assertEqual(io.output_interfaces[0], out) def test_prediction(self): def model(x): return 2*x - io = gr.Interface(inputs='textbox', outputs='TEXT', fn=model) + io = gr.Interface(inputs='textbox', outputs='TEXT', fn=model, + analytics_enabled=False) self.assertEqual(io.predict[0](11), 22) From 216a962a59436493e88e486f4dc5e6afea3c8f20 Mon Sep 17 00:00:00 2001 From: dawoodkhan82 Date: Tue, 11 Aug 2020 11:59:54 -0400 Subject: [PATCH 4/4] check if local url is ok --- gradio/interface.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gradio/interface.py b/gradio/interface.py index 3f47c57503..4d5ccaf509 100644 --- a/gradio/interface.py +++ b/gradio/interface.py @@ -296,7 +296,10 @@ class Interface: is_colab = utils.colab_check() if not is_colab: - print(strings.en["RUNNING_LOCALLY"].format(path_to_local_server)) + 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. "