Skip to content

Commit

Permalink
added flagging to frontend, backend working for image to label
Browse files Browse the repository at this point in the history
  • Loading branch information
aliabd committed Jul 22, 2020
1 parent dc35b46 commit 41701da
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 15 deletions.
15 changes: 15 additions & 0 deletions gradio/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def get_shortcut_implementations(cls):
"""
return {}

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

class Textbox(AbstractInput):
"""
Expand Down Expand Up @@ -290,6 +295,16 @@ def process_example(self, example):
else:
return example

def rebuild_flagged(self, dir, msg):
"""
Default rebuild method to decode a base64 image
"""
im = preprocessing_utils.decode_base64_to_image(msg)
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
12 changes: 1 addition & 11 deletions gradio/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
ASSOCIATION_PATH_IN_STATIC = "static/apple-app-site-association"
ASSOCIATION_PATH_IN_ROOT = "apple-app-site-association"

FLAGGING_DIRECTORY = 'static/flagged/'
FLAGGING_DIRECTORY = 'flagged/'
FLAGGING_FILENAME = 'data.txt'
analytics.write_key = "uxIFddIEuuUcFLf9VgH2teTEtPlWdkNy"
analytics_url = 'https://api.gradio.app/'
Expand Down Expand Up @@ -175,16 +175,6 @@ def do_POST(self):
if interface.saliency is not None:
saliency = interface.saliency(raw_input, prediction)
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")

self.wfile.write(json.dumps(output).encode())

Expand Down
11 changes: 11 additions & 0 deletions gradio/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ def get_shortcut_implementations(cls):
"""
return {}

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


class Textbox(AbstractOutput):
'''
Expand Down Expand Up @@ -130,6 +136,11 @@ def get_shortcut_implementations(cls):
"label": {},
}

def rebuild_flagged(self, dir, msg):
"""
Default rebuild method for label
"""
return json.loads(msg)

class Image(AbstractOutput):
'''
Expand Down
4 changes: 4 additions & 0 deletions gradio/static/css/gradio.css
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ input.submit {
input.submit:hover {
background-color: #f39c12;
}

.flag.flagged {
background-color: pink;
}
/* label:hover {
background-color: lightgray;
} */
Expand Down
4 changes: 2 additions & 2 deletions gradio/static/js/all_io.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ var io_master_template = {
this.target.find(".output_interfaces").css("opacity", 1);
}
},
flag: function(message) {
flag: function() {
var post_data = {
'data': {
'input_data' : toStringIfObject(this.last_input) ,
'output_data' : toStringIfObject(this.last_output),
'message' : message
'message' : "no-message"
}
}
$.ajax({type: "POST",
Expand Down
16 changes: 14 additions & 2 deletions gradio/static/js/gradio.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function gradio(config, fn, target) {
<div class="screenshot_logo">
<img src="static/img/logo_inline.png">
</div>
</div>
<input class="flag panel_button" type="button" value="FLAG"/>
</div>
</div>`);
let io_master = Object.create(io_master_template);
Expand Down Expand Up @@ -117,6 +117,7 @@ function gradio(config, fn, target) {
output_interface.clear();
}
target.find(".flag").removeClass("flagged");
target.find(".flag").val("FLAG");
target.find(".flag_message").empty();
target.find(".loading").addClass("invisible");
target.find(".loading_time").text("");
Expand Down Expand Up @@ -146,11 +147,22 @@ function gradio(config, fn, target) {
target.find(".submit").click(function() {
io_master.gather();
target.find(".flag").removeClass("flagged");
target.find(".flag").val("FLAG");
})
}
if (!config.show_input) {
target.find(".input_panel").hide();
}
}

target.find(".flag").click(function() {
if (io_master.last_output) {
target.find(".flag").addClass("flagged");
target.find(".flag").val("FLAGGED");
io_master.flag();

// io_master.flag($(".flag_message").val());
}
})

return io_master;
}
Expand Down

0 comments on commit 41701da

Please sign in to comment.