Upload anime_aesthetic/app.py with huggingface_hub
Browse files- anime_aesthetic/app.py +84 -0
anime_aesthetic/app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 11 |
+
hf_fs = HfFileSystem()
|
| 12 |
+
|
| 13 |
+
_REPOSITORY = 'deepghs/anime_aesthetic'
|
| 14 |
+
_DEFAULT_MODEL = 'swinv2pv3_v0_448_ls0.2_x'
|
| 15 |
+
_MODELS = natsorted([
|
| 16 |
+
os.path.dirname(os.path.relpath(file, _REPOSITORY))
|
| 17 |
+
for file in hf_fs.glob(f'{_REPOSITORY}/*/model.onnx')
|
| 18 |
+
])
|
| 19 |
+
|
| 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 _get_percentile(x, y, v):
|
| 40 |
+
idx = np.searchsorted(x, np.clip(v, a_min=0.0, a_max=6.0))
|
| 41 |
+
if idx < x.shape[0] - 1:
|
| 42 |
+
x0, y0 = x[idx], y[idx]
|
| 43 |
+
x1, y1 = x[idx + 1], y[idx + 1]
|
| 44 |
+
return np.clip((v - x0) / (x1 - x0) * (y1 - y0) + y0, a_min=0.0, a_max=1.0)
|
| 45 |
+
|
| 46 |
+
else:
|
| 47 |
+
return y[idx]
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def _fn_predict(image, model):
|
| 51 |
+
scores = classify_predict_score(
|
| 52 |
+
image=image,
|
| 53 |
+
repo_id=_REPOSITORY,
|
| 54 |
+
model_name=model,
|
| 55 |
+
)
|
| 56 |
+
weighted_mean = sum(i * scores[label] for i, label in enumerate(LABELS))
|
| 57 |
+
x, y = _get_mark_table(model)
|
| 58 |
+
percentile = _get_percentile(x, y, weighted_mean)
|
| 59 |
+
return weighted_mean, percentile, scores
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
if __name__ == '__main__':
|
| 63 |
+
with gr.Blocks() as demo:
|
| 64 |
+
with gr.Row():
|
| 65 |
+
with gr.Column():
|
| 66 |
+
gr_input_image = gr.Image(type='pil', label='Original Image')
|
| 67 |
+
gr_model = gr.Dropdown(_MODELS, value=_DEFAULT_MODEL, label='Model')
|
| 68 |
+
gr_submit = gr.Button(value='Submit', variant='primary')
|
| 69 |
+
|
| 70 |
+
with gr.Column():
|
| 71 |
+
with gr.Row():
|
| 72 |
+
gr_score = gr.Text(label='Aesthetic Score (0~6)', value='')
|
| 73 |
+
gr_percentile = gr.Text(label='Percentile (0.0-1.0)', value='')
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
gr_output = gr.Label(label='Aesthetic Classes')
|
| 77 |
+
|
| 78 |
+
gr_submit.click(
|
| 79 |
+
_fn_predict,
|
| 80 |
+
inputs=[gr_input_image, gr_model],
|
| 81 |
+
outputs=[gr_score, gr_percentile, gr_output],
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
demo.queue(os.cpu_count()).launch()
|