CodeChris commited on
Commit
8d7c593
·
verified ·
1 Parent(s): c348d43

Add underline button

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -1,6 +1,4 @@
1
  import argparse
2
- import os
3
-
4
  import gradio as gr
5
  import huggingface_hub
6
  import numpy as np
@@ -158,7 +156,8 @@ class Predictor:
158
 
159
  return np.expand_dims(image_array, axis=0)
160
 
161
- def tag_dict_to_sorted_string(self, dict_res: dict, sort_by_confidence, descending, escape_parens):
 
162
  """Custom function: Sort tag dict by confidence/alphabetically"""
163
  if sort_by_confidence:
164
  _sorted_list = sorted(
@@ -171,9 +170,12 @@ class Predictor:
171
  dict_res.items(),
172
  reverse=descending
173
  )
174
- _sorted_string = ", ".join([x[0] for x in _sorted_list])
 
 
 
175
  if escape_parens:
176
- _sorted_string = _sorted_string.replace("(", "\(").replace(")", "\)")
177
  return _sorted_string
178
 
179
  def predict(
@@ -186,6 +188,7 @@ class Predictor:
186
  character_mcut_enabled,
187
  sort_by_confidence_enabled,
188
  sort_descending_enabled,
 
189
  escape_parens_enabled
190
  ):
191
  self.load_model(model_repo)
@@ -227,12 +230,14 @@ class Predictor:
227
  general_res,
228
  sort_by_confidence=sort_by_confidence_enabled,
229
  descending=sort_descending_enabled,
 
230
  escape_parens=escape_parens_enabled
231
  )
232
  sorted_character_strings = self.tag_dict_to_sorted_string(
233
  character_res,
234
  sort_by_confidence=sort_by_confidence_enabled,
235
  descending=sort_descending_enabled,
 
236
  escape_parens=escape_parens_enabled
237
  )
238
 
@@ -323,6 +328,11 @@ def main():
323
  value=False,
324
  label="Descending"
325
  )
 
 
 
 
 
326
  escape_parens_enabled = gr.Checkbox(
327
  value=False,
328
  label="Escape Parens"
@@ -332,7 +342,7 @@ def main():
332
  show_copy_button=True
333
  )
334
  sorted_character_strings = gr.Textbox(
335
- label="Character (string)",
336
  show_copy_button=True
337
  )
338
  rating = gr.Label(label="Rating")
@@ -358,6 +368,7 @@ def main():
358
  character_mcut_enabled,
359
  sort_by_confidence_enabled,
360
  sort_descending_enabled,
 
361
  escape_parens_enabled
362
  ],
363
  outputs=[sorted_general_strings, sorted_character_strings,
@@ -365,7 +376,7 @@ def main():
365
  )
366
 
367
  demo.queue(max_size=10)
368
- demo.launch()
369
 
370
 
371
  if __name__ == "__main__":
 
1
  import argparse
 
 
2
  import gradio as gr
3
  import huggingface_hub
4
  import numpy as np
 
156
 
157
  return np.expand_dims(image_array, axis=0)
158
 
159
+ def tag_dict_to_sorted_string(self, dict_res: dict, sort_by_confidence, descending,
160
+ remove_underlines, escape_parens):
161
  """Custom function: Sort tag dict by confidence/alphabetically"""
162
  if sort_by_confidence:
163
  _sorted_list = sorted(
 
170
  dict_res.items(),
171
  reverse=descending
172
  )
173
+ if remove_underlines:
174
+ _sorted_string = ", ".join([x[0] for x in _sorted_list])
175
+ else: # Add back underlines
176
+ _sorted_string = ", ".join([x[0].replace(" ", "_") for x in _sorted_list])
177
  if escape_parens:
178
+ _sorted_string = _sorted_string.replace("(", "\\(").replace(")", "\\)")
179
  return _sorted_string
180
 
181
  def predict(
 
188
  character_mcut_enabled,
189
  sort_by_confidence_enabled,
190
  sort_descending_enabled,
191
+ remove_underline_enabled,
192
  escape_parens_enabled
193
  ):
194
  self.load_model(model_repo)
 
230
  general_res,
231
  sort_by_confidence=sort_by_confidence_enabled,
232
  descending=sort_descending_enabled,
233
+ remove_underlines=remove_underline_enabled,
234
  escape_parens=escape_parens_enabled
235
  )
236
  sorted_character_strings = self.tag_dict_to_sorted_string(
237
  character_res,
238
  sort_by_confidence=sort_by_confidence_enabled,
239
  descending=sort_descending_enabled,
240
+ remove_underlines=remove_underline_enabled,
241
  escape_parens=escape_parens_enabled
242
  )
243
 
 
328
  value=False,
329
  label="Descending"
330
  )
331
+ with gr.Row():
332
+ remove_underline_enabled = gr.Checkbox(
333
+ value=True,
334
+ label="Remove Tag Underlines"
335
+ )
336
  escape_parens_enabled = gr.Checkbox(
337
  value=False,
338
  label="Escape Parens"
 
342
  show_copy_button=True
343
  )
344
  sorted_character_strings = gr.Textbox(
345
+ label="Characters (string)",
346
  show_copy_button=True
347
  )
348
  rating = gr.Label(label="Rating")
 
368
  character_mcut_enabled,
369
  sort_by_confidence_enabled,
370
  sort_descending_enabled,
371
+ remove_underline_enabled,
372
  escape_parens_enabled
373
  ],
374
  outputs=[sorted_general_strings, sorted_character_strings,
 
376
  )
377
 
378
  demo.queue(max_size=10)
379
+ demo.launch(share=args.share)
380
 
381
 
382
  if __name__ == "__main__":