Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_input_output_mapping(self):
io = Interface(inputs='SketCHPad', outputs='textBOX', model=lambda x: x, model_type='function')
self.assertIsInstance(io.input_interface, gradio.inputs.Sketchpad)
self.assertIsInstance(io.output_interface, gradio.outputs.Textbox)
def test_path_exists(self):
inp = inputs.Sketchpad()
path = inputs.BASE_INPUT_INTERFACE_JS_PATH.format(inp.get_name())
self.assertTrue(os.path.exists(os.path.join(PACKAGE_NAME, path)))
def test_set_sample_data(self):
test_array = ["test1", "test2", "test3"]
temp_dir = tempfile.mkdtemp()
inp = inputs.Sketchpad()
out = outputs.Label()
networking.build_template(temp_dir, inp, out)
networking.set_sample_data_in_config_file(temp_dir, test_array)
# We need to come up with a better way so that the config file isn't invalid json unless
# the following parameters are set... (TODO: abidlabs)
networking.set_always_flagged_in_config_file(temp_dir, False)
networking.set_disabled_in_config_file(temp_dir, False)
config_file = os.path.join(temp_dir, 'static/config.json')
with open(config_file) as json_file:
data = json.load(json_file)
self.assertTrue(test_array == data["sample_inputs"])
def test_preprocessing(self):
inp = inputs.Sketchpad()
array = inp.preprocess(BASE64_SKETCH)
self.assertEqual(array.shape, (1, 28, 28))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train[:n], y_train[:n], epochs=2)
print(model.evaluate(x_test, y_test))
return model
model = get_trained_model(n=50000)
# Gradio code #
sketchpad = gradio.inputs.Sketchpad(flatten=True, sample_inputs=x_test[:10])
label = gradio.outputs.Label(show_confidences=False)
io = gradio.Interface(inputs=sketchpad, outputs=label, model=model, model_type="keras", verbose=False,
always_flag=True)
httpd, path_to_local_server, share_url = io.launch(inline=True, share=True, inbrowser=True)
print("URL for MNIST model interface: ", share_url)
with interface.sess.as_default():
output = output.argmax()
input_tensors = [model.inputs[0], K.learning_phase()]
saliency_input = model.layers[0].input
saliency_output = model.layers[-1].output[:, output]
gradients = model.optimizer.get_gradients(saliency_output, saliency_input)
compute_gradients = K.function(inputs=input_tensors, outputs=gradients)
saliency_graph = compute_gradients(processed_input.reshape(-1, 784))
normalized_saliency = (abs(saliency_graph[0]) - abs(saliency_graph[0]).min()) / \
(abs(saliency_graph[0]).max() - abs(saliency_graph[0]).min())
return normalized_saliency.reshape(28, 28)
model = get_trained_model(n=50000)
sketchpad = gradio.inputs.Sketchpad(flatten=True, sample_inputs=x_test[:10])
label = gradio.outputs.Label(show_confidences=False)
io = gradio.Interface(inputs=sketchpad, outputs=label, model=model, model_type="keras", saliency=saliency, always_flag=True)
httpd, path_to_local_server, share_url = io.launch(inline=True, share=True, inbrowser=True)
print("URL for MNIST model interface with saliency:", share_url)