From 0adcdfd97da3add9789a0fce5d140e2245cc05d4 Mon Sep 17 00:00:00 2001 From: Ali Abid Date: Fri, 29 Jan 2021 07:25:54 -0800 Subject: [PATCH] restore rebuild function --- gradio/inputs.py | 11 +++++++++++ gradio/interface.py | 11 +---------- gradio/networking.py | 2 -- gradio/outputs.py | 10 ++++++++++ 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/gradio/inputs.py b/gradio/inputs.py index 813d2b44e0..a974fd70c3 100644 --- a/gradio/inputs.py +++ b/gradio/inputs.py @@ -713,6 +713,17 @@ class Image(InputComponent): im = processing_utils.resize_and_crop(im, (shape[0], shape[1])) return np.asarray(im).flatten() + def rebuild(self, dir, data): + """ + Default rebuild method to decode a base64 image + """ + im = processing_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 Video(InputComponent): """ Component creates a video file upload that is converted to a file path. diff --git a/gradio/interface.py b/gradio/interface.py index e2b3d35a2c..5996d16b1c 100644 --- a/gradio/interface.py +++ b/gradio/interface.py @@ -172,16 +172,7 @@ class Interface: pass if self.allow_flagging: - if self.title is not None: - dir_name = "_".join(self.title.split(" ")) - else: - dir_name = "_".join([fn.__name__ for fn in self.predict]) - index = 1 - while os.path.exists(self.flagging_dir + "/" + dir_name + - "_{}".format(index)): - index += 1 - self.flagging_dir = self.flagging_dir + "/" + dir_name + \ - "_{}".format(index) + os.makedirs(self.flagging_dir, exist_ok=True) if self.analytics_enabled: try: diff --git a/gradio/networking.py b/gradio/networking.py index dbb0488b1e..d73ca8bc3c 100644 --- a/gradio/networking.py +++ b/gradio/networking.py @@ -210,8 +210,6 @@ def predict_examples(): def flag(): log_feature_analytics('flag') flag_path = os.path.join(app.cwd, app.interface.flagging_dir) - os.makedirs(flag_path, - exist_ok=True) output = {'inputs': [app.interface.input_interfaces[ i].rebuild( flag_path, request.json['data']['input_data'][i]) for i diff --git a/gradio/outputs.py b/gradio/outputs.py index 3e46fa06a9..ef9367df26 100644 --- a/gradio/outputs.py +++ b/gradio/outputs.py @@ -186,6 +186,16 @@ class Image(OutputComponent): raise ValueError("Unknown type: " + dtype + ". Please choose from: 'numpy', 'pil', 'file', 'plot'.") return out_y, coordinates + def rebuild(self, dir, data): + """ + Default rebuild method to decode a base64 image + """ + im = processing_utils.decode_base64_to_image(data) + timestamp = datetime.datetime.now() + filename = 'output_{}_{}.png'.format(self.label, timestamp.strftime("%Y-%m-%d-%H-%M-%S")) + im.save('{}/{}'.format(dir, filename), 'PNG') + return filename + class Video(OutputComponent): ''' Used for video output.