Merge pull request #48 from gradio-app/abidlabs/test_launch

Abidlabs/test launch
This commit is contained in:
Abubakar Abid 2020-08-30 16:21:27 -05:00 committed by GitHub
commit 945fee01a4
17 changed files with 165 additions and 80 deletions

View File

@ -3,13 +3,15 @@
import gradio as gr
from difflib import Differ
def diff_texts(text1, text2):
d = Differ()
return [
(token[2:], token[0]) for token in d.compare(text1, text2)
]
gr.Interface(
io = gr.Interface(
diff_texts,
[
gr.inputs.Textbox(lines=3, default="The quick brown fox jumped over the lazy dogs."),
@ -20,4 +22,7 @@ gr.Interface(
"-": "pink",
" ": "none",
})
).launch()
)
io.test_launch()
io.launch()

View File

@ -2,24 +2,26 @@
import tensorflow as tf
import gradio
import os
from tensorflow.keras.layers import *
import gradio as gr
from urllib.request import urlretrieve
urlretrieve("https://gr-models.s3-us-west-2.amazonaws.com/mnist-model.h5", "mnist-model.h5")
model = tf.keras.models.load_model("mnist-model.h5")
def recognize_digit(image):
image = image.reshape(1, -1)
prediction = model.predict(image).tolist()[0]
return {str(i): prediction[i] for i in range(10)}
gr.Interface(
io = gr.Interface(
recognize_digit,
"sketchpad",
gradio.outputs.Label(num_top_classes=3),
live=True,
capture_session=True,
).launch()
)
io.test_launch()
io.launch()

View File

@ -7,11 +7,14 @@ import random
def filter_records(records, gender):
return records[records['gender'] == gender]
gr.Interface(filter_records,
io = gr.Interface(filter_records,
[
gr.inputs.Dataframe(headers=["name", "age", "gender"], datatype=["str", "number", "str"], row_count=5),
gr.inputs.Dropdown(["M", "F", "O"])
],
"dataframe",
description="Enter gender as 'M', 'F', or 'O' for other."
).launch()
)
io.test_launch()
io.launch()

View File

@ -2,10 +2,10 @@
import gradio as gr
import numpy as np
import random
notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
def generate_tone(note, octave, duration):
sr = 48000
a4_freq, tones_from_a4 = 440, 12 * (octave - 4) + (note - 9)
@ -13,13 +13,16 @@ def generate_tone(note, octave, duration):
duration = int(duration)
audio = np.linspace(0, duration, duration * sr)
audio = (20000 * np.sin(audio * (2 * np.pi * frequency))).astype(np.int16)
return (sr, audio)
return sr, audio
gr.Interface(
io = gr.Interface(
generate_tone,
[
gr.inputs.Dropdown(notes, type="index"),
gr.inputs.Slider(4, 6, step=1),
gr.inputs.Textbox(type="number", default=1, label="Duration in seconds")
], "audio").launch()
], "audio")
io.test_launch()
io.launch()

View File

@ -1,15 +1,13 @@
# Demo: (Image) -> (Image)
import gradio as gr
from time import time
from PIL import Image
def image_mod(image):
return image.rotate(45)
gr.Interface(image_mod,
io = gr.Interface(image_mod,
gr.inputs.Image(type="pil"),
gr.outputs.Image(type="pil"),
examples=[
@ -18,4 +16,7 @@ gr.Interface(image_mod,
["images/lion.jpg"],
],
live=True,
).launch(share=True)
)
io.test_launch()
io.launch()

View File

@ -1,13 +1,17 @@
# Demo: (Dataframe) -> (Dataframe)
import gradio as gr
import numpy as np
import random
def transpose(matrix):
return matrix.T
gr.Interface(transpose,
gr.inputs.Dataframe(type="numpy", datatype="number", row_count=5, col_count=3),
"numpy"
).launch()
io = gr.Interface(
transpose,
gr.inputs.Dataframe(type="numpy", datatype="number", row_count=5, col_count=3),
"numpy"
)
io.test_launch()
io.launch()

View File

@ -2,10 +2,14 @@
import gradio as gr
import numpy as np
import random
def reverse_audio(audio):
sr, data = audio
return (sr, np.flipud(data))
gr.Interface(reverse_audio, "microphone", "audio").launch()
io = gr.Interface(reverse_audio, "microphone", "audio")
io.test_launch()
io.launch()

View File

@ -1,25 +1,28 @@
# Demo: (Slider, Dropdown, Radio, CheckboxGroup, Checkbox) -> (Textbox)
import gradio as gr
import numpy as np
def sentence_builder(quantity, animal, place, activity_list, morning):
return f"""The {quantity} {animal}s went to the {place} where they {" and ".join(activity_list)} until the {"morning" if morning else "night"}"""
gr.Interface(sentence_builder,
[
gr.inputs.Slider(2, 20),
gr.inputs.Dropdown(["cat", "dog", "bird"]),
gr.inputs.Radio(["park", "zoo", "road"]),
gr.inputs.CheckboxGroup(["ran", "swam", "ate", "slept"]),
gr.inputs.Checkbox(label="Is it the morning?"),
],
"text",
examples=[
[2, "cat", "park", ["ran", "swam"], True],
[4, "dog", "zoo", ["ate", "swam"], False],
[10, "bird", "road", ["ran"], False],
[8, "cat", "zoo", ["ate"], True],
],
).launch()
io = gr.Interface(
sentence_builder,
[
gr.inputs.Slider(2, 20),
gr.inputs.Dropdown(["cat", "dog", "bird"]),
gr.inputs.Radio(["park", "zoo", "road"]),
gr.inputs.CheckboxGroup(["ran", "swam", "ate", "slept"]),
gr.inputs.Checkbox(label="Is it the morning?"),
],
"text",
examples=[
[2, "cat", "park", ["ran", "swam"], True],
[4, "dog", "zoo", ["ate", "swam"], False],
[10, "bird", "road", ["ran"], False],
[8, "cat", "zoo", ["ate"], True],
])
io.test_launch()
io.launch()

View File

@ -4,7 +4,6 @@ import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
from scipy import signal
from scipy.io import wavfile
def spectrogram(audio):
@ -15,4 +14,7 @@ def spectrogram(audio):
return plt
gr.Interface(spectrogram, "audio", "plot").launch()
io = gr.Interface(spectrogram, "audio", "plot
io.test_launch()
io.launch()

View File

@ -1,7 +1,6 @@
# Demo: (Radio, CheckboxGroup, Slider, Checkbox, Dropdown) -> (Image)
import gradio as gr
import random
import matplotlib.pyplot as plt
import numpy as np
@ -21,15 +20,15 @@ def stock_forecast(final_year, companies, noise, show_legend, point_style):
return plt
gr.Interface(stock_forecast,
[
gr.inputs.Radio([2025, 2030, 2035, 2040],
label="Project to:"),
gr.inputs.CheckboxGroup(
["Google", "Microsoft", "Gradio"]),
gr.inputs.Slider(1, 100),
"checkbox",
gr.inputs.Dropdown(["cross", "line", "circle"], label="Style"),
],
gr.outputs.Image(plot=True, label="forecast")
).launch()
io = gr.Interface(
stock_forecast,
[
gr.inputs.Radio([2025, 2030, 2035, 2040], label="Project to:"),
gr.inputs.CheckboxGroup(["Google", "Microsoft", "Gradio"]),
gr.inputs.Slider(1, 100),
"checkbox",
gr.inputs.Dropdown(["cross", "line", "circle"], label="Style")],
gr.outputs.Image(plot=True, label="forecast"))
io.test_launch()
io.launch()

View File

@ -6,6 +6,7 @@ import gradio as gr
nlp = spacy.load("en_core_web_sm")
def text_analysis(text):
doc = nlp(text)
html = displacy.render(doc, style="dep", page=True)
@ -21,10 +22,14 @@ def text_analysis(text):
return pos_tokens, pos_count, html
gr.Interface(
io = gr.Interface(
text_analysis,
gr.inputs.Textbox(placeholder="Enter sentence here..."),
[
"highlight", "key_values", "html"
]
).launch()
)
io.test_launch()
io.launch()

View File

@ -1,9 +1,13 @@
# Demo: (Image) -> (Image)
import gradio as gr
import numpy as np
def snap(image):
return np.flipud(image)
gr.Interface(snap, gr.inputs.Image(shape=(100,100), image_mode="L", source="webcam"), "image").launch()
io = gr.Interface(snap, gr.inputs.Image(shape=(100,100), image_mode="L", source="webcam"), "image")
io.test_launch()
io.launch()

View File

@ -3,6 +3,7 @@
import gradio as gr
from zipfile import ZipFile
def zip_to_json(file_obj):
files = []
with ZipFile(file_obj.name) as zfile:
@ -14,4 +15,8 @@ def zip_to_json(file_obj):
})
return files
gr.Interface(zip_to_json, "file", "json").launch()
io = gr.Interface(zip_to_json, "file", "json")
io.test_launch()
io.launch()

View File

@ -3,10 +3,15 @@
import gradio as gr
from zipfile import ZipFile
def zip_two_files(file1, file2):
with ZipFile('tmp.zip', 'w') as zipObj:
zipObj.write(file1.name, "file1")
zipObj.write(file2.name, "file2")
return "tmp.zip"
gr.Interface(zip_two_files, ["file", "file"], "file").launch()
io = gr.Interface(zip_two_files, ["file", "file"], "file")
io.test_launch()
io.launch()

View File

@ -15,17 +15,19 @@ import base64
import numpy as np
import PIL
import scipy.io.wavfile
from gradio import processing_utils
from gradio import processing_utils, test_data
import pandas as pd
import math
import tempfile
class InputComponent(Component):
"""
Input Component. All input components subclass this.
"""
pass
class Textbox(InputComponent):
"""
Component creates a textbox for user to enter input. Provides a string (or number is `type` is "float") as an argument to the wrapped function.
@ -33,7 +35,7 @@ class Textbox(InputComponent):
"""
def __init__(self, lines=1, placeholder=None, default=None, numeric=False, type="str", label=None):
'''
"""
Parameters:
lines (int): number of line rows to provide in textarea.
placeholder (str): placeholder hint to provide behind textarea.
@ -41,7 +43,7 @@ class Textbox(InputComponent):
numeric (bool): DEPRECATED. Whether the input should be parsed as a number instead of a string.
type (str): Type of value to be returned by component. "str" returns a string, "number" returns a float value.
label (str): component name in interface.
'''
"""
self.lines = lines
self.placeholder = placeholder
self.default = default
@ -50,6 +52,13 @@ class Textbox(InputComponent):
self.type = "number"
else:
self.type = type
if default is None:
self.test_input = {
"str": "the quick brown fox jumped over the lazy dog",
"number": 786.92,
}[type]
else:
self.test_input = default
super().__init__(label)
def get_template_context(self):
@ -77,7 +86,6 @@ class Textbox(InputComponent):
raise ValueError("Unknown type: " + self.type + ". Please choose from: 'str', 'number'.")
class Slider(InputComponent):
"""
Component creates a slider that ranges from `minimum` to `maximum`. Provides a number as an argument to the wrapped function.
@ -101,6 +109,7 @@ class Slider(InputComponent):
step = 10 ** power
self.step = step
self.default = minimum if default is None else default
self.test_input = self.default
super().__init__(label)
def get_template_context(self):
@ -126,10 +135,11 @@ class Checkbox(InputComponent):
"""
def __init__(self, label=None):
'''
"""
Parameters:
label (str): component name in interface.
'''
"""
self.test_input = True
super().__init__(label)
@classmethod
@ -154,6 +164,7 @@ class CheckboxGroup(InputComponent):
'''
self.choices = choices
self.type = type
self.test_input = self.choices
super().__init__(label)
def get_template_context(self):
@ -186,6 +197,7 @@ class Radio(InputComponent):
'''
self.choices = choices
self.type = type
self.test_input = self.choices[0]
super().__init__(label)
def get_template_context(self):
@ -202,6 +214,7 @@ class Radio(InputComponent):
else:
raise ValueError("Unknown type: " + self.type + ". Please choose from: 'value', 'index'.")
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.
@ -217,6 +230,7 @@ class Dropdown(InputComponent):
'''
self.choices = choices
self.type = type
self.test_input = self.choices[0]
super().__init__(label)
def get_template_context(self):
@ -248,7 +262,7 @@ class Image(InputComponent):
invert_colors (bool): whether to invert the image as a preprocessing step.
source (str): Source of image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "canvas" defaults to a white image that can be edited and drawn upon with tools.
tool (str): Tools used for editing. "editor" allows a full screen editor, "select" provides a cropping and zoom tool.
type (str): Type of value to be returned by component. "numpy" returns a numpy array with shape (width, height, 3), "pil" returns a PIL image object, "file" returns a temporary file object whose path can be retrieved by file_obj.name.
type (str): Type of value to be returned by component. "numpy" returns a numpy array with shape (width, height, 3) and values from 0 to 255, "pil" returns a PIL image object, "file" returns a temporary file object whose path can be retrieved by file_obj.name.
label (str): component name in interface.
'''
self.shape = shape
@ -257,6 +271,7 @@ class Image(InputComponent):
self.tool = tool
self.type = type
self.invert_colors = invert_colors
self.test_input = test_data.BASE64_IMAGE
super().__init__(label)
@classmethod
@ -293,6 +308,8 @@ class Image(InputComponent):
file_obj = tempfile.NamedTemporaryFile()
im.save(file_obj.name)
return file_obj
else:
raise ValueError("Unknown type: " + self.type + ". Please choose from: 'numpy', 'pil', 'file'.")
def process_example(self, example):
if os.path.exists(example):
@ -318,14 +335,15 @@ class Audio(InputComponent):
"""
def __init__(self, source="upload", type="numpy", label=None):
'''
"""
Parameters:
source (str): Source of audio. "upload" creates a box where user can drop an audio file, "microphone" creates a microphone input.
type (str): Type of value to be returned by component. "numpy" returns a 2-set tuple with an integer sample_rate and the data numpy.array of shape (samples, 2), "file" returns a temporary file object whose path can be retrieved by file_obj.name, "mfcc" returns the mfcc coefficients of the input audio.
label (str): component name in interface.
'''
"""
self.source = source
self.type = type
self.test_input = test_data.BASE64_AUDIO
super().__init__(label)
def get_template_context(self):
@ -367,6 +385,7 @@ class File(InputComponent):
label (str): component name in interface.
'''
self.type = type
self.test_input = None
super().__init__(label)
@classmethod
@ -391,7 +410,7 @@ class Dataframe(InputComponent):
"""
def __init__(self, headers=None, row_count=3, col_count=3, datatype="str", type="pandas", label=None):
'''
"""
Parameters:
headers (List[str]): Header names to dataframe.
row_count (int): Limit number of rows for input.
@ -399,14 +418,17 @@ class Dataframe(InputComponent):
datatype (Union[str, List[str]]): Datatype of values in sheet. Can be provided per column as a list of strings, or for the entire sheet as a single string. Valid datatypes are "str", "number", "bool", and "date".
type (str): Type of value to be returned by component. "pandas" for pandas dataframe, "numpy" for numpy array, or "array" for a Python array.
label (str): component name in interface.
'''
"""
self.headers = headers
self.datatype = datatype
self.row_count = row_count
self.col_count = len(headers) if headers else col_count
self.type = type
super().__init__(label)
sample_values = {"str": "abc", "number": 786, "bool": True, "date": "02/08/1993"}
column_dtypes = [datatype]*self.col_count if isinstance(datatype, str) else datatype
self.test_input = [[sample_values[c] for c in column_dtypes] for _ in range(row_count)]
super().__init__(label)
def get_template_context(self):
return {

View File

@ -193,19 +193,19 @@ class Interface:
return config
def process(self, raw_input):
def process(self, raw_input, predict_fn=None):
"""
:param raw_input: a list of raw inputs to process and apply the
prediction(s) on.
:param predict_fn: which function to process. If not provided, all of the model functions are used.
:return:
processed output: a list of processed outputs to return as the
prediction(s).
duration: a list of time deltas measuring inference time for each
prediction fn.
"""
processed_input = [input_interface.preprocess(
raw_input[i]) for i, input_interface in
enumerate(self.input_interfaces)]
processed_input = [input_interface.preprocess(raw_input[i])
for i, input_interface in enumerate(self.input_interfaces)]
predictions = []
durations = []
for predict_fn in self.predict:
@ -253,6 +253,22 @@ class Interface:
thread.keep_running = False
networking.url_ok(path_to_local_server)
def test_launch(self):
for predict_fn in self.predict:
print("Test launching: {}()...".format(predict_fn.__name__), end=' ')
raw_input = []
for input_interface in self.input_interfaces:
if input_interface.test_input is None: # If no test input is defined for that input interface
print("SKIPPED")
break
else: # If a test input is defined for each interface object
raw_input.append(input_interface.test_input)
else:
self.process(raw_input)
print("PASSED")
continue
def launch(self, inline=None, inbrowser=None, share=False, debug=False):
"""
Parameters

2
gradio/test_data.py Normal file

File diff suppressed because one or more lines are too long