diff --git a/gradio/inputs.py b/gradio/inputs.py index bcf7862d70..b9fb134555 100644 --- a/gradio/inputs.py +++ b/gradio/inputs.py @@ -122,9 +122,9 @@ class Sketchpad(AbstractInput): """ Default rebuild method to decode a base64 image """ - inp = msg['data']['input'] - im = preprocessing_utils.decode_base64_to_image(inp) - timestamp = time.time()*1000 + + im = preprocessing_utils.decode_base64_to_image(msg) + timestamp = int(time.time()*1000) filename = f'input_{timestamp}.png' im.save(f'{dir}/{filename}', 'PNG') return filename @@ -197,11 +197,10 @@ class Textbox(AbstractInput): """ Default rebuild method for text saves it .txt file """ - inp = msg['data']['input'] timestamp = time.time()*1000 filename = f'input_{timestamp}.png' with open(f'{dir}/{filename}.txt','w') as f: - f.write(inp) + f.write(msg) return filename def get_sample_inputs(self): @@ -251,8 +250,7 @@ class ImageUpload(AbstractInput): """ Default rebuild method to decode a base64 image """ - inp = msg['data']['input'] - im = preprocessing_utils.decode_base64_to_image(inp) + im = preprocessing_utils.decode_base64_to_image(msg) timestamp = time.time()*1000 filename = f'input_{timestamp}.png' im.save(f'{dir}/{filename}', 'PNG') @@ -283,8 +281,7 @@ class CSV(AbstractInput): """ Default rebuild method for csv """ - inp = msg['data']['inp'] - return json.loads(inp) + return json.loads(msg) class Microphone(AbstractInput): @@ -304,8 +301,7 @@ class Microphone(AbstractInput): """ Default rebuild method for csv """ - inp = msg['data']['inp'] - return json.loads(inp) + return json.loads(msg) # Automatically adds all subclasses of AbstractInput into a dictionary (keyed by class name) for easy referencing. diff --git a/gradio/interface.py b/gradio/interface.py index 6f81480766..965b6e239a 100644 --- a/gradio/interface.py +++ b/gradio/interface.py @@ -47,6 +47,7 @@ class Interface: postprocessing_fns=None, verbose=True, saliency=None, + always_flag=False ): """ :param inputs: a string or `AbstractInput` representing the input interface. @@ -99,6 +100,7 @@ class Interface: self.simple_server = None self.hash = random.getrandbits(32) self.saliency = saliency + self.always_flag = always_flag @staticmethod def _infer_model_type(model): diff --git a/gradio/networking.py b/gradio/networking.py index a85ead4951..00d04cdfb4 100644 --- a/gradio/networking.py +++ b/gradio/networking.py @@ -194,6 +194,16 @@ def serve_files_in_background(interface, port, directory_to_serve=None): import numpy as np saliency = interface.saliency(interface, interface.model_obj, raw_input, processed_input, prediction, processed_output) output['saliency'] = saliency.tolist() + if interface.always_flag: + msg = json.loads(data_string) + flag_dir = os.path.join(FLAGGING_DIRECTORY, str(interface.hash)) + os.makedirs(flag_dir, exist_ok=True) + output_flag = {'input': interface.input_interface.rebuild_flagged(flag_dir, msg['data']), + 'output': interface.output_interface.rebuild_flagged(flag_dir, processed_output), + } + with open(os.path.join(flag_dir, FLAGGING_FILENAME), 'a+') as f: + f.write(json.dumps(output_flag)) + f.write("\n") # Prepare return json dictionary. self.wfile.write(json.dumps(output).encode()) @@ -202,10 +212,10 @@ def serve_files_in_background(interface, port, directory_to_serve=None): self._set_headers() data_string = self.rfile.read(int(self.headers["Content-Length"])) msg = json.loads(data_string) - flag_dir = os.path.join(directory_to_serve, FLAGGING_DIRECTORY) - os.makedirs(flag_dir, exist_ok=True) - output = {'input': interface.input_interface.rebuild_flagged(flag_dir, msg), - 'output': interface.output_interface.rebuild_flagged(flag_dir, msg), + flag_dir = os.path.join(FLAGGING_DIRECTORY, str(interface.hash)) + os.makedirs(FLAGGING_DIRECTORY, exist_ok=True) + output = {'input': interface.input_interface.rebuild_flagged(flag_dir, msg['data']['input_data']), + 'output': interface.output_interface.rebuild_flagged(flag_dir, msg['data']['output_data']), 'message': msg['data']['message']} with open(os.path.join(flag_dir, FLAGGING_FILENAME), 'a+') as f: f.write(json.dumps(output)) diff --git a/gradio/outputs.py b/gradio/outputs.py index 241b5a7a3a..1a495e3207 100644 --- a/gradio/outputs.py +++ b/gradio/outputs.py @@ -123,8 +123,7 @@ class Label(AbstractOutput): """ Default rebuild method for label """ - out = msg['data']['output'] - return json.loads(out) + return json.loads(msg) class Textbox(AbstractOutput): @@ -141,8 +140,7 @@ class Textbox(AbstractOutput): """ Default rebuild method for label """ - out = msg['data']['output'] - return json.loads(out) + return json.loads(msg) class Image(AbstractOutput): @@ -159,8 +157,7 @@ class Image(AbstractOutput): """ Default rebuild method to decode a base64 image """ - out = msg['data']['output'] - im = preprocessing_utils.decode_base64_to_image(out) + im = preprocessing_utils.decode_base64_to_image(msg) timestamp = datetime.datetime.now() filename = f'output_{timestamp.strftime("%Y-%m-%d-%H-%M-%S")}.png' im.save(f'{dir}/{filename}', 'PNG') diff --git a/static/flagged/1937261912/data.txt b/static/flagged/1937261912/data.txt new file mode 100644 index 0000000000..5adea54f87 --- /dev/null +++ b/static/flagged/1937261912/data.txt @@ -0,0 +1 @@ +{"input": "input_1564123002454.png", "output": {"label": 0, "confidences": [{"label": 0, "confidence": 0.6294623613357544}, {"label": 9, "confidence": 0.20205073058605194}, {"label": 8, "confidence": 0.1176086813211441}]}} diff --git a/static/flagged/1937261912/input_1564123002454.png b/static/flagged/1937261912/input_1564123002454.png new file mode 100644 index 0000000000..a98077ac9a Binary files /dev/null and b/static/flagged/1937261912/input_1564123002454.png differ diff --git a/static/flagged/415000484input_1564122605293.png b/static/flagged/415000484input_1564122605293.png new file mode 100644 index 0000000000..f31bf010ef Binary files /dev/null and b/static/flagged/415000484input_1564122605293.png differ diff --git a/static/flagged/560356067/data.txt b/static/flagged/560356067/data.txt new file mode 100644 index 0000000000..7d3b5908d6 --- /dev/null +++ b/static/flagged/560356067/data.txt @@ -0,0 +1,2 @@ +{"input": "input_1564122648517.png", "output": {"label": 7, "confidences": [{"label": 7, "confidence": 0.9945800304412842}, {"label": 2, "confidence": 0.004080282989889383}, {"label": 0, "confidence": 0.0005652490654028952}]}} +{"input": "input_1564122787604.png", "output": {"label": 6, "confidences": [{"label": 6, "confidence": 0.18893034756183624}, {"label": 0, "confidence": 0.1815452128648758}, {"label": 5, "confidence": 0.12178582698106766}]}} diff --git a/static/flagged/560356067input_1564122648517.png b/static/flagged/560356067input_1564122648517.png new file mode 100644 index 0000000000..bd88ca2482 Binary files /dev/null and b/static/flagged/560356067input_1564122648517.png differ diff --git a/static/flagged/560356067input_1564122787604.png b/static/flagged/560356067input_1564122787604.png new file mode 100644 index 0000000000..f165dfa39d Binary files /dev/null and b/static/flagged/560356067input_1564122787604.png differ diff --git a/static/flagged/data.txt b/static/flagged/data.txt new file mode 100644 index 0000000000..7c63a137eb --- /dev/null +++ b/static/flagged/data.txt @@ -0,0 +1,3 @@ +{"input": "input_1564121741675.png", "output": {"label": 3, "confidences": [{"label": 3, "confidence": 0.49897417426109314}, {"label": 2, "confidence": 0.49327075481414795}, {"label": 9, "confidence": 0.0031273311469703913}]}, "message": ""} +{"input": "input_1564122304245.png", "output": {"label": 1, "confidences": [{"label": 1, "confidence": 0.9119029641151428}, {"label": 0, "confidence": 0.04364873468875885}, {"label": 6, "confidence": 0.014955705031752586}]}} +{"input": "input_1564122365828.png", "output": {"label": 1, "confidences": [{"label": 1, "confidence": 0.8408133387565613}, {"label": 4, "confidence": 0.07017231732606888}, {"label": 2, "confidence": 0.032813962548971176}]}} diff --git a/static/flagged/input_1564121741675.png b/static/flagged/input_1564121741675.png new file mode 100644 index 0000000000..46adc0de5d Binary files /dev/null and b/static/flagged/input_1564121741675.png differ diff --git a/static/flagged/input_1564122263377.png b/static/flagged/input_1564122263377.png new file mode 100644 index 0000000000..fc2f99583d Binary files /dev/null and b/static/flagged/input_1564122263377.png differ diff --git a/static/flagged/input_1564122304245.png b/static/flagged/input_1564122304245.png new file mode 100644 index 0000000000..cac68ecb7e Binary files /dev/null and b/static/flagged/input_1564122304245.png differ diff --git a/static/flagged/input_1564122365828.png b/static/flagged/input_1564122365828.png new file mode 100644 index 0000000000..720f1354a3 Binary files /dev/null and b/static/flagged/input_1564122365828.png differ