This commit is contained in:
Abubakar Abid 2020-08-11 19:28:19 -05:00
commit f918fa0260
4 changed files with 38 additions and 42 deletions

View File

@ -49,7 +49,8 @@ class Interface:
capture_session=False, title=None, description=None,
thumbnail=None, server_port=None, server_name=networking.LOCALHOST_NAME,
allow_screenshot=True, allow_flagging=True,
flagging_dir="flagged"):
flagging_dir="flagged", analytics_enabled=True):
"""
Parameters:
fn (Callable): the function to wrap an interface around.
@ -123,6 +124,7 @@ class Interface:
self.allow_flagging = allow_flagging
self.flagging_dir = flagging_dir
Interface.instances.add(self)
self.analytics_enabled=analytics_enabled
data = {'fn': fn,
'inputs': inputs,
@ -154,11 +156,12 @@ class Interface:
self.flagging_dir = self.flagging_dir + "/" + dir_name + \
"_{}".format(index)
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 = {
@ -293,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. "
@ -302,11 +308,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:
utils.error_analytics("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"])
@ -374,11 +388,13 @@ 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
is_in_interactive_mode = bool(getattr(sys, 'ps1', sys.flags.interactive))
if not is_in_interactive_mode:

View File

@ -16,7 +16,6 @@ import urllib.request
from shutil import copyfile
import requests
import sys
import analytics
import csv
@ -38,9 +37,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):
"""
@ -128,21 +124,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()"""
@ -174,10 +155,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(

View File

@ -24,8 +24,7 @@ def colab_check():
is_colab = False
try: # Check if running interactively using ipython.
from_ipynb = get_ipython()
if "google.colab" in str(from_ipynb) or "ipykernel" in str(
from_ipynb):
if "google.colab" in str(from_ipynb):
is_colab = True
except NameError:
error_analytics("NameError", analytics_url)

View File

@ -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.Image)
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)