Skip to content

Commit

Permalink
remove console logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali Abid committed Aug 5, 2020
1 parent 53ae797 commit 2943492
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 180 deletions.
60 changes: 46 additions & 14 deletions build/lib/gradio/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import warnings
from abc import ABC, abstractmethod

import base64
import numpy as np
import PIL.Image
import PIL.ImageOps
import scipy.io.wavfile
from gradio import preprocessing_utils, validation_data
from gradio import preprocessing_utils

# Where to find the static resources associated with each template.
# BASE_INPUT_INTERFACE_TEMPLATE_PATH = 'static/js/interfaces/input/{}.js'
Expand All @@ -31,13 +32,6 @@ class AbstractInput(ABC):
def __init__(self, label):
self.label = label

def get_validation_inputs(self):
"""
An interface can optionally implement a method that returns a list of examples inputs that it should be able to
accept and preprocess for validation purposes.
"""
return []

def get_template_context(self):
"""
:return: a dictionary with context variables for the javascript file associated with the context
Expand All @@ -63,6 +57,11 @@ def get_shortcut_implementations(cls):
"""
return {}

def rebuild(self, dir, data):
"""
All interfaces should define a method that rebuilds the flagged input when it's passed back (i.e. rebuilds image from base64)
"""
return data

class Textbox(AbstractInput):
"""
Expand Down Expand Up @@ -253,9 +252,6 @@ def __init__(self, shape=None, image_mode='RGB', label=None):
self.image_mode = image_mode
super().__init__(label)

def get_validation_inputs(self):
return validation_data.BASE64_COLOR_IMAGES

@classmethod
def get_shortcut_implementations(cls):
return {
Expand Down Expand Up @@ -290,6 +286,16 @@ def process_example(self, example):
else:
return example

def rebuild(self, dir, data):
"""
Default rebuild method to decode a base64 image
"""
im = preprocessing_utils.decode_base64_to_image(data)
timestamp = datetime.datetime.now()
filename = f'input_{timestamp.strftime("%Y-%m-%d-%H-%M-%S")}.png'
im.save(f'{dir}/{filename}', 'PNG')
return filename


class Sketchpad(AbstractInput):
"""
Expand Down Expand Up @@ -341,6 +347,16 @@ def preprocess(self, inp):
def process_example(self, example):
return preprocessing_utils.convert_file_to_base64(example)

def rebuild(self, dir, data):
"""
Default rebuild method to decode a base64 image
"""
im = preprocessing_utils.decode_base64_to_image(data)
timestamp = datetime.datetime.now()
filename = f'input_{timestamp.strftime("%Y-%m-%d-%H-%M-%S")}.png'
im.save(f'{dir}/{filename}', 'PNG')
return filename


class Webcam(AbstractInput):
"""
Expand All @@ -359,9 +375,6 @@ def __init__(self, shape=(224, 224), label=None):
self.num_channels = 3
super().__init__(label)

def get_validation_inputs(self):
return validation_data.BASE64_COLOR_IMAGES

@classmethod
def get_shortcut_implementations(cls):
return {
Expand All @@ -378,6 +391,16 @@ def preprocess(self, inp):
im, (self.image_width, self.image_height))
return np.array(im)

def rebuild(self, dir, data):
"""
Default rebuild method to decode a base64 image
"""
im = preprocessing_utils.decode_base64_to_image(data)
timestamp = datetime.datetime.now()
filename = f'input_{timestamp.strftime("%Y-%m-%d-%H-%M-%S")}.png'
im.save('{}/{}'.format(dir, filename), 'PNG')
return filename


class Microphone(AbstractInput):
"""
Expand Down Expand Up @@ -414,6 +437,15 @@ def preprocess(self, inp):
_, signal = scipy.io.wavfile.read(file_obj.name)
return signal

def rebuild(self, dir, data):
inp = data.split(';')[1].split(',')[1]
wav_obj = base64.b64decode(inp)
timestamp = datetime.datetime.now()
filename = f'input_{timestamp.strftime("%Y-%m-%d-%H-%M-%S")}.wav'
with open("{}/{}".format(dir, filename), "wb+") as f:
f.write(wav_obj)
return filename


# Automatically adds all shortcut implementations in AbstractInput into a dictionary.
shortcuts = {}
Expand Down
Loading

0 comments on commit 2943492

Please sign in to comment.