narugo commited on
Commit
dae87e0
·
1 Parent(s): 67a423d

dev(narugo): add percentile value

Browse files
Files changed (1) hide show
  1. app.py +31 -6
app.py CHANGED
@@ -1,7 +1,10 @@
1
  import os
 
2
 
3
  import gradio as gr
4
- from huggingface_hub import HfFileSystem
 
 
5
  from imgutils.generic import classify_predict_score
6
  from natsort import natsorted
7
 
@@ -17,14 +20,32 @@ _MODELS = natsorted([
17
  LABELS = ["worst", "low", "normal", "good", "great", "best", "masterpiece"]
18
 
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def _fn_predict(image, model):
21
  scores = classify_predict_score(
22
  image=image,
23
  repo_id=_REPOSITORY,
24
  model_name=model,
25
  )
26
- final_score = sum(i * scores[label] for i, label in enumerate(LABELS))
27
- return final_score, scores
 
 
28
 
29
 
30
  if __name__ == '__main__':
@@ -36,13 +57,17 @@ if __name__ == '__main__':
36
  gr_submit = gr.Button(value='Submit', variant='primary')
37
 
38
  with gr.Column():
39
- gr_score = gr.Text(label='Aesthetic Score (0~6)', value='')
40
- gr_output = gr.Label(label='Aesthetic Classes')
 
 
 
 
41
 
42
  gr_submit.click(
43
  _fn_predict,
44
  inputs=[gr_input_image, gr_model],
45
- outputs=[gr_score, gr_output],
46
  )
47
 
48
  demo.queue(os.cpu_count()).launch()
 
1
  import os
2
+ from functools import lru_cache
3
 
4
  import gradio as gr
5
+ import numpy as np
6
+ import pandas as pd
7
+ from huggingface_hub import HfFileSystem, hf_hub_download
8
  from imgutils.generic import classify_predict_score
9
  from natsort import natsorted
10
 
 
20
  LABELS = ["worst", "low", "normal", "good", "great", "best", "masterpiece"]
21
 
22
 
23
+ @lru_cache()
24
+ def _get_mark_table(model):
25
+ df = pd.read_csv(hf_hub_download(
26
+ repo_id=_REPOSITORY,
27
+ repo_type='model',
28
+ filename=f'{model}/samples.csv',
29
+ ))
30
+ df = df.sort_values(['score'])
31
+ df['cnt'] = list(range(len(df)))
32
+ df['final_score'] = df['cnt'] / len(df)
33
+
34
+ x = np.concatenate([[0.0], df['score'], [6.0]])
35
+ y = np.concatenate([[0.0], df['final_score'], [1.0]])
36
+ return x, y
37
+
38
+
39
  def _fn_predict(image, model):
40
  scores = classify_predict_score(
41
  image=image,
42
  repo_id=_REPOSITORY,
43
  model_name=model,
44
  )
45
+ weighted_mean = sum(i * scores[label] for i, label in enumerate(LABELS))
46
+ x, y = _get_mark_table(model)
47
+ percentile = y[np.searchsorted(x, np.clip(weighted_mean, a_min=0.0, a_max=6.0))]
48
+ return weighted_mean, percentile, scores
49
 
50
 
51
  if __name__ == '__main__':
 
57
  gr_submit = gr.Button(value='Submit', variant='primary')
58
 
59
  with gr.Column():
60
+ with gr.Row():
61
+ gr_score = gr.Text(label='Aesthetic Score (0~6)', value='')
62
+ gr_percentile = gr.Text(label='Percentile (0.0-1.0)', value='')
63
+
64
+ with gr.Row():
65
+ gr_output = gr.Label(label='Aesthetic Classes')
66
 
67
  gr_submit.click(
68
  _fn_predict,
69
  inputs=[gr_input_image, gr_model],
70
+ outputs=[gr_score, gr_percentile, gr_output],
71
  )
72
 
73
  demo.queue(os.cpu_count()).launch()