NoCrypt commited on
Commit
088d239
1 Parent(s): e7f49cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -2
app.py CHANGED
@@ -14,6 +14,7 @@ import huggingface_hub
14
  import numpy as np
15
  import PIL.Image
16
  import tensorflow as tf
 
17
 
18
  TITLE = 'NoCrypt/DeepDanbooru_string'
19
  DESCRIPTION = 'Cloned from: https://huggingface.co/spaces/hysts/DeepDanbooru'
@@ -68,9 +69,13 @@ def load_labels() -> list[str]:
68
  labels = [line.strip() for line in f.readlines()]
69
  return labels
70
 
 
 
 
71
 
72
  def predict(image: PIL.Image.Image, score_threshold: float,
73
  model: tf.keras.Model, labels: list[str]) -> dict[str, float]:
 
74
  _, height, width, _ = model.input_shape
75
  image = np.asarray(image)
76
  image = tf.image.resize(image,
@@ -89,7 +94,41 @@ def predict(image: PIL.Image.Image, score_threshold: float,
89
  res[label] = prob
90
  b = dict(sorted(res.items(),key=lambda item:item[1], reverse=True))
91
  a = ', '.join(list(b.keys())).replace('_',' ').replace('(','\(').replace(')','\)')
92
- return (a,res)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
 
95
  def main():
@@ -112,7 +151,12 @@ def main():
112
  default=args.score_threshold,
113
  label='Score Threshold'),
114
  ],
115
- [gr.outputs.Textbox(label='Output String'), gr.outputs.Label(label='Output Labels')],
 
 
 
 
 
116
  examples=[
117
  ['miku.jpg',0.5],
118
  ['miku2.jpg',0.5]
 
14
  import numpy as np
15
  import PIL.Image
16
  import tensorflow as tf
17
+ import piexif
18
 
19
  TITLE = 'NoCrypt/DeepDanbooru_string'
20
  DESCRIPTION = 'Cloned from: https://huggingface.co/spaces/hysts/DeepDanbooru'
 
69
  labels = [line.strip() for line in f.readlines()]
70
  return labels
71
 
72
+ def plaintext_to_html(text):
73
+ text = "<p>" + "<br>\n".join([f"{html.escape(x)}" for x in text.split('\n')]) + "</p>"
74
+ return text
75
 
76
  def predict(image: PIL.Image.Image, score_threshold: float,
77
  model: tf.keras.Model, labels: list[str]) -> dict[str, float]:
78
+ rawimage = image
79
  _, height, width, _ = model.input_shape
80
  image = np.asarray(image)
81
  image = tf.image.resize(image,
 
94
  res[label] = prob
95
  b = dict(sorted(res.items(),key=lambda item:item[1], reverse=True))
96
  a = ', '.join(list(b.keys())).replace('_',' ').replace('(','\(').replace(')','\)')
97
+
98
+ items = rawimage.info
99
+ geninfo = ''
100
+
101
+ if "exif" in rawimage.info:
102
+ exif = piexif.load(rawimage.info["exif"])
103
+ exif_comment = (exif or {}).get("Exif", {}).get(piexif.ExifIFD.UserComment, b'')
104
+ try:
105
+ exif_comment = piexif.helper.UserComment.load(exif_comment)
106
+ except ValueError:
107
+ exif_comment = exif_comment.decode('utf8', errors="ignore")
108
+
109
+ items['exif comment'] = exif_comment
110
+ geninfo = exif_comment
111
+
112
+ for field in ['jfif', 'jfif_version', 'jfif_unit', 'jfif_density', 'dpi', 'exif',
113
+ 'loop', 'background', 'timestamp', 'duration']:
114
+ items.pop(field, None)
115
+
116
+ geninfo = items.get('parameters', geninfo)
117
+
118
+ info = ''
119
+ for key, text in items.items():
120
+ info += f"""
121
+ <div>
122
+ <p><b>{plaintext_to_html(str(key))}</b></p>
123
+ <p>{plaintext_to_html(str(text))}</p>
124
+ </div>
125
+ """.strip()+"\n"
126
+
127
+ if len(info) == 0:
128
+ message = ""
129
+ info = f"<div><p>{message}<p></div>"
130
+
131
+ return (a,res,geninfo,info)
132
 
133
 
134
  def main():
 
151
  default=args.score_threshold,
152
  label='Score Threshold'),
153
  ],
154
+ [
155
+ gr.outputs.Textbox(label='Output String'),
156
+ gr.outputs.Label(label='Output Labels'),
157
+ gr.outputs.HTML(visible=len(geninfo)!=0),
158
+ gr.outputs.HTML(visible=len(info)!=0)
159
+ ],
160
  examples=[
161
  ['miku.jpg',0.5],
162
  ['miku2.jpg',0.5]