mirror of
https://github.com/gradio-app/gradio.git
synced 2025-02-23 11:39:17 +08:00
added ability to close ports if running from same instance
This commit is contained in:
parent
ce068a6b57
commit
7739987a2e
@ -34,27 +34,22 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"execution_count": 101,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"inp = gradio.inputs.ImageUpload(image_width=299, image_height=299, num_channels=3)\n",
|
||||
"out = gradio.outputs.Label(label_names='imagenet1000', max_label_length=8, num_top_classes=5)\n",
|
||||
"\n",
|
||||
"iface = gradio.Interface(inputs=inp, \n",
|
||||
" outputs=out,\n",
|
||||
" model=model, \n",
|
||||
" model_type='keras')\n",
|
||||
"\n",
|
||||
"# iface = gradio.Interface(inputs=inp, \n",
|
||||
"# outputs=out,\n",
|
||||
"# model=model, \n",
|
||||
"# model_type='keras')"
|
||||
"io = gradio.Interface(inputs=inp, \n",
|
||||
" outputs=out,\n",
|
||||
" model=model, \n",
|
||||
" model_type='keras')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"execution_count": 103,
|
||||
"metadata": {
|
||||
"scrolled": false
|
||||
},
|
||||
@ -63,26 +58,15 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Closing existing server...\n",
|
||||
"NOTE: Gradio is in beta stage, please report all bugs to: a12d@stanford.edu\n",
|
||||
"Model is running locally at: http://localhost:7866/interface.html\n",
|
||||
"Model is running locally at: http://localhost:7882/interface.html\n",
|
||||
"To create a public link, set `share=True` in the argument to `launch()`\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(<gradio.networking.serve_files_in_background.<locals>.HTTPServer at 0x25a6fb73390>,\n",
|
||||
" 'http://localhost:7866/interface.html',\n",
|
||||
" None)"
|
||||
]
|
||||
},
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"iface.launch(inline=False, inbrowser=True, share=False, validate=False)"
|
||||
"io.launch(inline=False, inbrowser=True, share=False, validate=False);"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
@ -143,6 +143,7 @@ class ImageUpload(AbstractInput):
|
||||
array = im.reshape(1, self.image_width, self.image_height, self.num_channels)
|
||||
return array
|
||||
|
||||
|
||||
class CSV(AbstractInput):
|
||||
|
||||
def get_template_path(self):
|
||||
@ -154,5 +155,6 @@ class CSV(AbstractInput):
|
||||
"""
|
||||
return inp
|
||||
|
||||
|
||||
# Automatically adds all subclasses of AbstractInput into a dictionary (keyed by class name) for easy referencing.
|
||||
registry = {cls.__name__.lower(): cls for cls in AbstractInput.__subclasses__()}
|
||||
|
@ -67,6 +67,7 @@ class Interface:
|
||||
self.verbose = verbose
|
||||
self.status = self.STATUS_TYPES['OFF']
|
||||
self.validate_flag = False
|
||||
self.simple_server = None
|
||||
|
||||
@staticmethod
|
||||
def _infer_model_type(model):
|
||||
@ -188,8 +189,17 @@ class Interface:
|
||||
if validate and not self.validate_flag:
|
||||
self.validate()
|
||||
|
||||
output_directory = tempfile.mkdtemp()
|
||||
# If an existing interface is running with this instance, close it.
|
||||
if self.status == self.STATUS_TYPES['RUNNING']:
|
||||
if self.verbose:
|
||||
print("Closing existing server...")
|
||||
if self.simple_server is not None:
|
||||
try:
|
||||
networking.close_server(self.simple_server)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
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(output_directory)
|
||||
path_to_local_server = 'http://localhost:{}/'.format(server_port)
|
||||
@ -205,6 +215,7 @@ class Interface:
|
||||
self.input_interface.__class__.__name__.lower(),
|
||||
self.output_interface.__class__.__name__.lower())
|
||||
self.status = self.STATUS_TYPES['RUNNING']
|
||||
self.simple_server = httpd
|
||||
|
||||
is_colab = False
|
||||
try: # Check if running interactively using ipython.
|
||||
|
@ -157,7 +157,6 @@ def serve_files_in_background(port, directory_to_serve=None):
|
||||
self.base_path = base_path
|
||||
BaseHTTPServer.__init__(self, server_address, RequestHandlerClass)
|
||||
|
||||
|
||||
httpd = HTTPServer(directory_to_serve, (LOCALHOST_NAME, port))
|
||||
|
||||
# Now loop forever
|
||||
@ -166,7 +165,7 @@ def serve_files_in_background(port, directory_to_serve=None):
|
||||
while True:
|
||||
# sys.stdout.flush()
|
||||
httpd.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
except (KeyboardInterrupt, OSError):
|
||||
httpd.server_close()
|
||||
|
||||
thread = threading.Thread(target=serve_forever, daemon=True)
|
||||
@ -181,6 +180,11 @@ def start_simple_server(directory_to_serve=None):
|
||||
return port, httpd
|
||||
|
||||
|
||||
def close_server(server):
|
||||
server.shutdown()
|
||||
server.server_close()
|
||||
|
||||
|
||||
def download_ngrok():
|
||||
try:
|
||||
zip_file_url = NGROK_ZIP_URLS[sys.platform]
|
||||
|
Loading…
Reference in New Issue
Block a user