This commit is contained in:
Ali Abid 2021-12-22 08:45:43 +00:00
commit aba6ac1480
13 changed files with 114 additions and 120 deletions

View File

@ -2,3 +2,4 @@ coverage:
range: 0..100 range: 0..100
round: down round: down
precision: 2 precision: 2
comment: false

View File

@ -1,11 +1,14 @@
Metadata-Version: 1.0 Metadata-Version: 2.1
Name: gradio Name: gradio
Version: 2.5.2 Version: 2.5.3
Summary: Python library for easily interacting with trained machine learning models Summary: Python library for easily interacting with trained machine learning models
Home-page: https://github.com/gradio-app/gradio-UI Home-page: https://github.com/gradio-app/gradio-UI
Author: Abubakar Abid Author: Abubakar Abid
Author-email: a12d@stanford.edu Author-email: a12d@stanford.edu
License: Apache License 2.0 License: Apache License 2.0
Description: UNKNOWN
Keywords: machine learning,visualization,reproducibility Keywords: machine learning,visualization,reproducibility
Platform: UNKNOWN Platform: UNKNOWN
License-File: LICENSE
UNKNOWN

View File

@ -1,15 +1,15 @@
Flask-Cors>=3.0.8
Flask-Login
Flask>=1.1.1
analytics-python
ffmpy
flask-cachebuster
markdown2
matplotlib
numpy numpy
pandas
paramiko
pillow
pycryptodome
pydub pydub
matplotlib
pandas
pillow
ffmpy
markdown2
pycryptodome
requests requests
paramiko
analytics-python
Flask>=1.1.1
Flask-Cors>=3.0.8
flask-cachebuster
Flask-Login

View File

@ -30,7 +30,7 @@ class Interface:
Interfaces are created with Gradio by constructing a `gradio.Interface()` object or by calling `gradio.Interface.load()`. Interfaces are created with Gradio by constructing a `gradio.Interface()` object or by calling `gradio.Interface.load()`.
""" """
instances = weakref.WeakSet() # stores all currently existing Interface instances instances = weakref.WeakSet() # stores references to all currently existing Interface instances
@classmethod @classmethod
def get_instances(cls): def get_instances(cls):
@ -76,9 +76,8 @@ class Interface:
examples_per_page=10, live=False, layout="unaligned", show_input=True, show_output=True, examples_per_page=10, live=False, layout="unaligned", show_input=True, show_output=True,
capture_session=None, interpretation=None, num_shap=2.0, theme=None, repeat_outputs_per_model=True, capture_session=None, interpretation=None, num_shap=2.0, theme=None, repeat_outputs_per_model=True,
title=None, description=None, article=None, thumbnail=None, title=None, description=None, article=None, thumbnail=None,
css=None, server_port=None, server_name=None, height=500, width=900, css=None, height=500, width=900, allow_screenshot=True, allow_flagging=None, flagging_options=None,
allow_screenshot=True, allow_flagging=None, flagging_options=None, encrypt=False, encrypt=False, show_tips=None, flagging_dir="flagged", analytics_enabled=None, enable_queue=None, api_mode=None):
show_tips=None, flagging_dir="flagged", analytics_enabled=None, enable_queue=None, api_mode=None):
""" """
Parameters: Parameters:
fn (Callable): the function to wrap an interface around. fn (Callable): the function to wrap an interface around.
@ -176,11 +175,6 @@ class Interface:
self.num_shap = num_shap self.num_shap = num_shap
self.examples_per_page = examples_per_page self.examples_per_page = examples_per_page
self.server_name = server_name
self.server_port = server_port
if server_name is not None or server_port is not None:
warnings.warn("The server_name and server_port parameters in the `Interface` class will be deprecated. Please provide them in the `launch()` method instead.")
self.simple_server = None self.simple_server = None
self.allow_screenshot = allow_screenshot self.allow_screenshot = allow_screenshot
# For allow_flagging and analytics_enabled: (1) first check for parameter, (2) check for environment variable, (3) default to True # For allow_flagging and analytics_enabled: (1) first check for parameter, (2) check for environment variable, (3) default to True
@ -578,7 +572,6 @@ class Interface:
show_error (bool): If True, any errors in the interface will be printed in the browser console log show_error (bool): If True, any errors in the interface will be printed in the browser console log
server_port (int): will start gradio app on this port (if available) server_port (int): will start gradio app on this port (if available)
server_name (str): to make app accessible on local network, set this to "0.0.0.0". server_name (str): to make app accessible on local network, set this to "0.0.0.0".
show_error (bool): show prediction errors in console
show_tips (bool): if True, will occasionally show tips about new Gradio features show_tips (bool): if True, will occasionally show tips about new Gradio features
enable_queue (bool): if True, inference requests will be served through a queue instead of with parallel threads. Required for longer inference times (> 1min) to prevent timeout. enable_queue (bool): if True, inference requests will be served through a queue instead of with parallel threads. Required for longer inference times (> 1min) to prevent timeout.
Returns: Returns:
@ -586,7 +579,6 @@ class Interface:
path_to_local_server (str): Locally accessible link path_to_local_server (str): Locally accessible link
share_url (str): Publicly accessible link (if share=True) share_url (str): Publicly accessible link (if share=True)
""" """
# Set up local flask server # Set up local flask server
config = self.get_config_file() config = self.get_config_file()
self.config = config self.config = config
@ -595,6 +587,7 @@ class Interface:
self.auth = auth self.auth = auth
self.auth_message = auth_message self.auth_message = auth_message
self.show_tips = show_tips self.show_tips = show_tips
self.show_error = show_error
# Request key for encryption # Request key for encryption
if self.encrypt: if self.encrypt:
@ -602,19 +595,18 @@ class Interface:
getpass.getpass("Enter key for encryption: ")) getpass.getpass("Enter key for encryption: "))
# Store parameters # Store parameters
server_name = server_name or self.server_name or networking.LOCALHOST_NAME
server_port = server_port or self.server_port or networking.INITIAL_PORT_VALUE
if self.enable_queue is None: if self.enable_queue is None:
self.enable_queue = enable_queue self.enable_queue = enable_queue
# Launch local flask server # Launch local flask server
server_port, path_to_local_server, app, thread = networking.start_server( server_port, path_to_local_server, app, thread, server = networking.start_server(
self, server_name, server_port, self.auth) self, server_name, server_port, self.auth)
self.local_url = path_to_local_server self.local_url = path_to_local_server
self.server_port = server_port self.server_port = server_port
self.status = "RUNNING" self.status = "RUNNING"
self.server = app self.server = server
self.show_error = show_error self.server_app = app
self.server_thread = thread
# Count number of launches # Count number of launches
utils.launch_counter() utils.launch_counter()
@ -709,21 +701,18 @@ class Interface:
return app, path_to_local_server, share_url return app, path_to_local_server, share_url
def close(self): def close(self, verbose=True):
""" """
Closes the Interface that was launched. This will close the server and free the port. Closes the Interface that was launched. This will close the server and free the port.
""" """
try: try:
if self.share_url: self.server.shutdown()
requests.get("{}/shutdown".format(self.share_url)) self.server_thread.join()
print("Closing Gradio server on port {}...".format(self.server_port)) print("Closing server running on port: {}".format(self.server_port))
elif self.local_url: except AttributeError: # can't close if not running
requests.get("{}shutdown".format(self.local_url)) pass
print("Closing Gradio server on port {}...".format(self.server_port)) except OSError: # sometimes OSError is thrown when shutting down
else: pass
pass # server not running
except (requests.ConnectionError, ConnectionResetError):
pass # server is already closed
def integrate(self, comet_ml=None, wandb=None, mlflow=None): def integrate(self, comet_ml=None, wandb=None, mlflow=None):
""" """
@ -764,11 +753,13 @@ class Interface:
utils.integration_analytics(data) utils.integration_analytics(data)
def close_all(): def close_all(verbose=True):
# Tries to close all running interfaces, but method is a little flaky.
for io in Interface.get_instances(): for io in Interface.get_instances():
io.close() io.close(verbose)
def reset_all(): def reset_all():
warnings.warn("The `reset_all()` method has been renamed to `close_all()`. Please use `close_all()` instead.") warnings.warn("The `reset_all()` method has been renamed to `close_all()` "
"and will be deprecated. Please use `close_all()` instead.")
close_all() close_all()

View File

@ -2,45 +2,44 @@
Defines helper methods useful for setting up ports, launching servers, and handling `ngrok` Defines helper methods useful for setting up ports, launching servers, and handling `ngrok`
""" """
import os import csv
import socket import datetime
import threading
from flask import Flask, request, session, jsonify, abort, send_file, render_template, redirect from flask import Flask, request, session, jsonify, abort, send_file, render_template, redirect
from flask_cachebuster import CacheBuster from flask_cachebuster import CacheBuster
from flask_login import LoginManager, login_user, current_user, login_required from flask_login import LoginManager, login_user, current_user, login_required
from flask_cors import CORS from flask_cors import CORS
import threading from functools import wraps
import pkg_resources import inspect
import datetime import io
import time
import json import json
import urllib.request
from shutil import copyfile
import requests
import sys
import csv
import logging import logging
from gradio.tunneling import create_tunnel import os
import pkg_resources
import requests
import socket
import sys
import threading
import time
import traceback
import urllib.request
from werkzeug.security import safe_join
from werkzeug.serving import make_server
from gradio import encryptor from gradio import encryptor
from gradio import queue from gradio import queue
from functools import wraps from gradio.tunneling import create_tunnel
import io
import inspect
import traceback
from werkzeug.security import safe_join
INITIAL_PORT_VALUE = int(os.getenv( # By default, the http server will try to open on port 7860. If not available, 7861, 7862, etc.
'GRADIO_SERVER_PORT', "7860")) # The http server will try to open on port 7860. If not available, 7861, 7862, etc. INITIAL_PORT_VALUE = int(os.getenv('GRADIO_SERVER_PORT', "7860"))
TRY_NUM_PORTS = int(os.getenv( # Number of ports to try before giving up and throwing an exception.
'GRADIO_NUM_PORTS', "100")) # Number of ports to try before giving up and throwing an exception. TRY_NUM_PORTS = int(os.getenv('GRADIO_NUM_PORTS', "100"))
LOCALHOST_NAME = os.getenv( LOCALHOST_NAME = os.getenv('GRADIO_SERVER_NAME', "127.0.0.1")
'GRADIO_SERVER_NAME', "127.0.0.1")
GRADIO_API_SERVER = "https://api.gradio.app/v1/tunnel-request" GRADIO_API_SERVER = "https://api.gradio.app/v1/tunnel-request"
GRADIO_FEATURE_ANALYTICS_URL = "https://api.gradio.app/gradio-feature-analytics/" GRADIO_FEATURE_ANALYTICS_URL = "https://api.gradio.app/gradio-feature-analytics/"
STATIC_TEMPLATE_LIB = pkg_resources.resource_filename("gradio", "templates/") STATIC_TEMPLATE_LIB = pkg_resources.resource_filename("gradio", "templates/")
STATIC_PATH_LIB = pkg_resources.resource_filename("gradio", "templates/frontend/static") STATIC_PATH_LIB = pkg_resources.resource_filename("gradio", "templates/frontend/static")
VERSION_FILE = pkg_resources.resource_filename("gradio", "version.txt") VERSION_FILE = pkg_resources.resource_filename("gradio", "version.txt")
with open(VERSION_FILE) as version_file: with open(VERSION_FILE) as version_file:
GRADIO_STATIC_ROOT = "https://gradio.s3-us-west-2.amazonaws.com/" + \ GRADIO_STATIC_ROOT = "https://gradio.s3-us-west-2.amazonaws.com/" + \
version_file.read().strip() + "/static/" version_file.read().strip() + "/static/"
@ -426,10 +425,22 @@ def queue_thread(path_to_local_server, test_mode=False):
break break
def start_server(interface, server_name, server_port, auth=None, ssl=None): def start_server(interface, server_name=None, server_port=None, auth=None, ssl=None):
if server_name is None:
server_name = LOCALHOST_NAME
if server_port is None: # if port is not specified, start at 7860 and search for first available port
port = get_first_available_port( port = get_first_available_port(
server_port, server_port + TRY_NUM_PORTS INITIAL_PORT_VALUE, INITIAL_PORT_VALUE + TRY_NUM_PORTS
) )
else:
try:
s = socket.socket() # create a socket object
s.bind((LOCALHOST_NAME, server_port)) # Bind to the port to see if it's available (otherwise, raise OSError)
s.close()
except OSError:
raise OSError("Port {} is in use. If a gradio.Interface is running on the port, you can close() it or gradio.close_all().".format(server_port))
port = server_port
url_host_name = "localhost" if server_name == "0.0.0.0" else server_name url_host_name = "localhost" if server_name == "0.0.0.0" else server_name
path_to_local_server = "http://{}:{}/".format(url_host_name, port) path_to_local_server = "http://{}:{}/".format(url_host_name, port)
if auth is not None: if auth is not None:
@ -451,15 +462,13 @@ def start_server(interface, server_name, server_port, auth=None, ssl=None):
app.queue_thread.start() app.queue_thread.start()
if interface.save_to is not None: if interface.save_to is not None:
interface.save_to["port"] = port interface.save_to["port"] = port
app_kwargs = {"port": port, "host": server_name} app_kwargs = {"app": app, "port": port, "host": server_name}
if ssl: if ssl:
app_kwargs["ssl_context"] = ssl app_kwargs["ssl_context"] = ssl
thread = threading.Thread(target=app.run, server = make_server(**app_kwargs)
kwargs=app_kwargs, thread = threading.Thread(target=server.serve_forever, daemon=True)
daemon=True)
thread.start() thread.start()
return port, path_to_local_server, app, thread, server
return port, path_to_local_server, app, thread
def get_state(): def get_state():
return session.get("state") return session.get("state")

View File

@ -269,10 +269,10 @@ And of course, you can also mix `Parallel` and `Series` together whenever that m
### Queuing to Manage Long Inference Times ### Queuing to Manage Long Inference Times
If many people are using your interface or if the inference time of your function is long (> 1min), simply set the `enable_queue` parameter in the `Interface` class to `True` to prevent timeouts. If many people are using your interface or if the inference time of your function is long (> 1min), simply set the `enable_queue` parameter in the `launch` method to `True` to prevent timeouts.
```python ```python
gr.Interface(fn=classify_image, inputs=image, outputs=label, enable_queue=True).launch() gr.Interface(fn=classify_image, inputs=image, outputs=label).launch(enable_queue=True)
``` ```
This sets up a queue of workers to handle the predictions and return the response to the front end. This is strongly recommended if you are planning on uploading your demo to Hugging Face Spaces (as described above) so that you can manage a large number of users simultaneously using your demo. This sets up a queue of workers to handle the predictions and return the response to the front end. This is strongly recommended if you are planning on uploading your demo to Hugging Face Spaces (as described above) so that you can manage a large number of users simultaneously using your demo.

View File

@ -5,7 +5,7 @@ except ImportError:
setup( setup(
name='gradio', name='gradio',
version='2.5.2', version='2.5.3',
include_package_data=True, include_package_data=True,
description='Python library for easily interacting with trained machine learning models', description='Python library for easily interacting with trained machine learning models',
author='Abubakar Abid', author='Abubakar Abid',

View File

@ -10,6 +10,7 @@ import threading
from comet_ml import Experiment from comet_ml import Experiment
import mlflow import mlflow
import wandb import wandb
import socket
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False" os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
@ -24,10 +25,19 @@ def captured_output():
sys.stdout, sys.stderr = old_out, old_err sys.stdout, sys.stderr = old_out, old_err
class TestInterface(unittest.TestCase): class TestInterface(unittest.TestCase):
def test_reset_all(self): def test_close(self):
io = Interface(lambda input: None, "textbox", "label")
_, local_url, _ = io.launch(prevent_thread_lock=True)
response = requests.get(local_url)
self.assertEqual(response.status_code, 200)
io.close()
with self.assertRaises(Exception):
response = requests.get(local_url)
def test_close_all(self):
interface = Interface(lambda input: None, "textbox", "label") interface = Interface(lambda input: None, "textbox", "label")
interface.close = mock.MagicMock() interface.close = mock.MagicMock()
reset_all() close_all()
interface.close.assert_called() interface.close.assert_called()
def test_examples_invalid_input(self): def test_examples_invalid_input(self):

View File

@ -199,7 +199,6 @@ class TestState(unittest.TestCase):
def test_state_value(self): def test_state_value(self):
io = gr.Interface(lambda x: len(x), "text", "label") io = gr.Interface(lambda x: len(x), "text", "label")
io.launch(prevent_thread_lock=True)
app, _, _ = io.launch(prevent_thread_lock=True) app, _, _ = io.launch(prevent_thread_lock=True)
with app.test_request_context(): with app.test_request_context():
networking.set_state("test") networking.set_state("test")

View File

@ -103,18 +103,6 @@ class TestUtils(unittest.TestCase):
def test_readme_to_html_correct_parse(self): def test_readme_to_html_correct_parse(self):
readme_to_html("https://github.com/gradio-app/gradio/blob/master/README.md") readme_to_html("https://github.com/gradio-app/gradio/blob/master/README.md")
def test_launch_counter(self):
with tempfile.NamedTemporaryFile() as tmp:
with mock.patch('gradio.utils.JSON_PATH', tmp.name):
interface = gradio.Interface(lambda x: x, "textbox", "label")
os.remove(tmp.name)
interface.launch(prevent_thread_lock=True)
with open(tmp.name) as j:
self.assertEqual(json.load(j)['launches'], 1)
interface.launch(prevent_thread_lock=True)
with open(tmp.name) as j:
self.assertEqual(json.load(j)['launches'], 2)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -7,19 +7,16 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>Gradio Docs</title> <title>Gradio Docs</title>
<meta name="description" content="Demos for your ML Models."> <meta name="description" content="Browse Gradio documentation and examples">
<meta name="author" content="SitePoint"> <meta name="author" content="Gradio team">
<meta property="og:title" content="Gradio Docs">
<meta name="description" content="Build & share machine learning apps delightfully.">
<meta name="author" content="SitePoint">
<meta property="og:title" content="Gradio">
<meta property="og:type" content="website"> <meta property="og:type" content="website">
<meta property="og:url" content="https://gradio.app/"> <meta property="og:url" content="https://gradio.app/">
<meta property="og:description" content="Build & share machine learning apps delightfully."> <meta property="og:description" content="Browse Gradio documentation and examples">
<meta name="twitter:card" content="summary_large_image"> <meta name="twitter:card" content="summary_large_image">
<meta name="twitter:creator" content="@teamGradio"> <meta name="twitter:creator" content="@teamGradio">
<meta name="twitter:title" content="Gradio"> <meta name="twitter:title" content="Gradio Docs">
<meta name="twitter:description" content="Build & share machine learning apps delightfully."> <meta name="twitter:description" content="Browse Gradio documentation and examples">
<meta name="twitter:image" content="https://gradio.app/static/home/img/social-cheetah.jpg"> <meta name="twitter:image" content="https://gradio.app/static/home/img/social-cheetah.jpg">
<link rel="icon" type="image/png" href="/assets/img/logo.png"> <link rel="icon" type="image/png" href="/assets/img/logo.png">

View File

@ -7,19 +7,16 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>Gradio</title> <title>Gradio</title>
<meta name="description" content="Demos for your ML Models."> <meta name="description" content="Start building machine learning web apps in 5 lines of pure Python!">
<meta name="author" content="SitePoint"> <meta name="author" content="Getting Started with Gradio">
<meta name="description" content="Build & share machine learning apps delightfully.">
<meta name="author" content="SitePoint">
<meta property="og:title" content="Gradio"> <meta property="og:title" content="Gradio">
<meta property="og:type" content="website"> <meta property="og:type" content="website">
<meta property="og:url" content="https://gradio.app/"> <meta property="og:url" content="https://gradio.app/">
<meta property="og:description" content="Build & share machine learning apps delightfully."> <meta property="og:description" content="Start building machine learning web apps in 5 lines of pure Python!">
<meta name="twitter:card" content="summary_large_image"> <meta name="twitter:card" content="summary_large_image">
<meta name="twitter:creator" content="@teamGradio"> <meta name="twitter:creator" content="@teamGradio">
<meta name="twitter:title" content="Gradio"> <meta name="twitter:title" content="Getting Started with Gradio">
<meta name="twitter:description" content="Build & share machine learning apps delightfully."> <meta name="twitter:description" content="Start building machine learning web apps in 5 lines of pure Python!">
<meta name="twitter:image" content="https://gradio.app/static/home/img/social-cheetah.jpg"> <meta name="twitter:image" content="https://gradio.app/static/home/img/social-cheetah.jpg">
<link rel="icon" type="image/png" href="/assets/img/logo.png"> <link rel="icon" type="image/png" href="/assets/img/logo.png">

View File

@ -7,9 +7,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>Gradio</title> <title>Gradio</title>
<meta name="description" content="Build & share machine learning apps delightfully."> <meta name="description" content="Build & share machine learning apps delightfully.">
<meta name="author" content="SitePoint"> <meta name="author" content="Gradio team">
<meta property="og:title" content="Gradio"> <meta property="og:title" content="Gradio">
<meta property="og:type" content="website"> <meta property="og:type" content="website">
<meta property="og:url" content="https://gradio.app/"> <meta property="og:url" content="https://gradio.app/">