This commit is contained in:
Abubakar Abid 2020-08-15 03:46:08 -05:00
commit 70b11979c8
9 changed files with 74 additions and 24 deletions

43
build/lib/gradio/cors.py Normal file
View File

@ -0,0 +1,43 @@
import SimpleHTTPServer
class CORSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def send_head(self):
"""Common code for GET and HEAD commands.
This sends the response code and MIME headers.
Return value is either a file object (which has to be copied
to the outputfile by the caller unless the command was HEAD,
and must be closed by the caller under all circumstances), or
None, in which case the caller has nothing further to do.
"""
path = self.translate_path(self.path)
f = None
if os.path.isdir(path):
if not self.path.endswith('/'):
# redirect browser - doing basically what apache does
self.send_response(301)
self.send_header("Location", self.path + "/")
self.end_headers()
return None
for index in "index.html", "index.htm":
index = os.path.join(path, index)
if os.path.exists(index):
path = index
break
else:
return self.list_directory(path)
ctype = self.guess_type(path)
try:
# Always read in binary mode. Opening files in text mode may cause
# newline translations, making the actual size of the content
# transmitted *less* than the content-length!
f = open(path, 'rb')
except IOError:
self.send_error(404, "File not found")
return None
self.send_response(200)
self.send_header("Content-type", ctype)
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
return f

View File

@ -9,6 +9,8 @@ def get_params(func):
params_doc = []
documented_params = {"self"}
for param_line in params_str.split("\n")[1:]:
if param_line.strip() == "Returns":
break
space_index = param_line.index(" ")
colon_index = param_line.index(":")
name = param_line[:space_index]
@ -37,6 +39,8 @@ def document(cls_set):
inp = {}
inp["name"] = cls.__name__
doc = inspect.getdoc(cls)
if doc.startswith("DEPRECATED"):
continue
inp["doc"] = "\n".join(doc.split("\n")[:-1])
inp["type"] = doc.split("\n")[-1].split("type: ")[-1]
inp["params"], inp["params_doc"] = get_params(cls.__init__)

View File

@ -206,7 +206,7 @@ class Radio(InputComponent):
class Dropdown(InputComponent):
"""
Component creates a dropdown of which only one can be selected. Provides string representing selected choice as an argument to the wrapped function.
Input type: str
Input type: Union[str, int]
"""
def __init__(self, choices, type="value", label=None):

View File

@ -59,8 +59,6 @@ class Interface:
verbose (bool): whether to print detailed information during launch.
examples (List[List[Any]]): sample inputs for the function; if provided, appears below the UI components and can be used to populate the interface. Should be nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component.
live (bool): whether the interface should automatically reload on change.
show_input (bool): if False, removes the input from the interface and underlays it in the output.
show_output (bool): if False, removes the output from the interface and overlays it in the input.
capture_session (bool): if True, captures the default graph and session (needed for Tensorflow 1.x)
title (str): a title for the interface; if provided, appears above the input and output components.
description (str): a description for the interface; if provided, appears above the input and output components.
@ -258,15 +256,11 @@ class Interface:
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
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)

View File

@ -132,6 +132,12 @@ def serve_files_in_background(interface, port, directory_to_serve=None, server_n
self.send_header("Content-type", "application/json")
self.end_headers()
def end_headers(self):
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST')
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
return super(HTTPHandler, self).end_headers()
def translate_path(self, path):
path = SimpleHTTPRequestHandler.translate_path(self, path)
relpath = os.path.relpath(path, os.getcwd())

View File

@ -9,6 +9,8 @@ def get_params(func):
params_doc = []
documented_params = {"self"}
for param_line in params_str.split("\n")[1:]:
if param_line.strip() == "Returns":
break
space_index = param_line.index(" ")
colon_index = param_line.index(":")
name = param_line[:space_index]
@ -37,6 +39,8 @@ def document(cls_set):
inp = {}
inp["name"] = cls.__name__
doc = inspect.getdoc(cls)
if doc.startswith("DEPRECATED"):
continue
inp["doc"] = "\n".join(doc.split("\n")[:-1])
inp["type"] = doc.split("\n")[-1].split("type: ")[-1]
inp["params"], inp["params_doc"] = get_params(cls.__init__)

View File

@ -206,7 +206,7 @@ class Radio(InputComponent):
class Dropdown(InputComponent):
"""
Component creates a dropdown of which only one can be selected. Provides string representing selected choice as an argument to the wrapped function.
Input type: str
Input type: Union[str, int]
"""
def __init__(self, choices, type="value", label=None):

View File

@ -59,8 +59,6 @@ class Interface:
verbose (bool): whether to print detailed information during launch.
examples (List[List[Any]]): sample inputs for the function; if provided, appears below the UI components and can be used to populate the interface. Should be nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component.
live (bool): whether the interface should automatically reload on change.
show_input (bool): if False, removes the input from the interface and underlays it in the output.
show_output (bool): if False, removes the output from the interface and overlays it in the input.
capture_session (bool): if True, captures the default graph and session (needed for Tensorflow 1.x)
title (str): a title for the interface; if provided, appears above the input and output components.
description (str): a description for the interface; if provided, appears above the input and output components.
@ -258,15 +256,11 @@ class Interface:
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
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)

View File

@ -132,6 +132,11 @@ def serve_files_in_background(interface, port, directory_to_serve=None, server_n
self.send_header("Content-type", "application/json")
self.end_headers()
def end_headers(self):
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST')
return super(HTTPHandler, self).end_headers()
def translate_path(self, path):
path = SimpleHTTPRequestHandler.translate_path(self, path)
relpath = os.path.relpath(path, os.getcwd())