style and js changes

This commit is contained in:
Ali Abid 2020-06-18 13:16:25 -07:00
commit 55f0bef538
11 changed files with 95 additions and 28 deletions

17
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,17 @@
# Contributing to Gradio UI
You can start by forking or cloning the repo (https://github.com/gradio-app/gradio-UI.git) and creating your own branch to work from. All PRs must pass the continuous integration
tests and receive approval from a member of the Gradio UI development team before they will be merged.
### Continuous Integration and Testing
All PRs must pass the continuous integration tests before merging. To test locally, you can run `python3 -m unittest`.
### Submitting PRs
All PRs should be against `master`. Direct commits to master are blocked, and PRs require an approving review
to merge into master. By convention, the Gradio UI maintainers will review PRs when:
* An initial review has been requested
* A maintainer is tagged in the PR comments and asked to complete a review
We ask that you make sure initial CI checks are passing before requesting a review.
One of the Gradio UI maintainers will merge the PR when all the checks are passing.

View File

@ -92,6 +92,9 @@ gradio.Interface(predict, 'textbox', 'label').launch()
![alt text](https://raw.githubusercontent.com/abidlabs/gradio/master/label_interface.png)
### Contributing:
If you would like to contribute and your contribution is small, you can directly open a pull request (PR). If you would like to contribute a larger feature, we recommend first creating an issue with a proposed design for discussion. Please see our contributing guidelines for more info.
### See more:
Find more info on usage here: www.gradio.app.

View File

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

View File

@ -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

Binary file not shown.

Binary file not shown.

View File

@ -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

View File

@ -311,9 +311,10 @@ class ImageIn(AbstractInput):
im = np.array(im).flatten()
im = im * self.scale + self.shift
if self.num_channels is None:
array = im.reshape(self.image_width, self.image_height)
array = im.reshape(1, self.image_width, self.image_height)
else:
array = im.reshape(self.image_width, self.image_height, self.num_channels)
array = im.reshape(1, self.image_width, self.image_height, \
self.num_channels)
return array
def rebuild_flagged(self, dir, msg):

View File

@ -16,8 +16,9 @@ import requests
import random
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"
@ -29,7 +30,9 @@ 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, 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.
@ -41,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]
@ -49,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]
@ -63,6 +69,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 +78,9 @@ class Interface:
self.show_input = show_input
self.show_output = show_output
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 = {
@ -148,6 +159,12 @@ 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 self.capture_session:
self.session = tf.get_default_graph(), \
tf.keras.backend.get_session()
# If an existing interface is running with this instance, close it.
if self.status == "RUNNING":
@ -161,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)
@ -235,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))

View File

@ -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,8 +139,26 @@ 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 len(interface.output_interfaces) / len(interface.predict) == 1:
if interface.context:
if interface.capture_session:
graph, sess = interface.session
with graph.as_default():
with sess.as_default():
prediction = predict_fn(*processed_input,
interface.context)
else:
prediction = predict_fn(*processed_input,
interface.context)
else:
if interface.capture_session:
graph, sess = interface.session
with graph.as_default():
with sess.as_default():
prediction = predict_fn(*processed_input)
else:
prediction = predict_fn(*processed_input)
if len(interface.output_interfaces) / \
len(interface.predict) == 1:
prediction = [prediction]
predictions.extend(prediction)
processed_output = [output_interface.postprocess(predictions[i]) for i, output_interface in enumerate(interface.output_interfaces)]
@ -260,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():
@ -277,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

View File

@ -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',