Spaces:
Sleeping
Sleeping
unfinished app.py
#1
by
spuun
- opened
- app.py +11 -69
- onnx_.py +0 -59
- requirements.txt +1 -3
app.py
CHANGED
|
@@ -1,75 +1,17 @@
|
|
| 1 |
-
from transformers import pipeline
|
| 2 |
-
from imgutils.data import rgb_encode, load_image
|
| 3 |
-
from onnx_ import _open_onnx_model
|
| 4 |
-
from PIL import Image
|
| 5 |
import gradio as gr
|
| 6 |
-
import
|
| 7 |
-
import os
|
| 8 |
-
import requests
|
| 9 |
import torch
|
| 10 |
-
import json
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
std = np.asarray([std_]).reshape((-1, 1, 1))
|
| 20 |
-
data = (data - mean) / std
|
| 21 |
-
|
| 22 |
-
return data.astype(np.float32)
|
| 23 |
-
|
| 24 |
-
nsfw_tf = pipeline(model="carbon225/vit-base-patch16-224-hentai")
|
| 25 |
-
|
| 26 |
-
if not os.path.exists("timm.onnx"):
|
| 27 |
-
open("timm.onnx", "wb").write(
|
| 28 |
-
requests.get(
|
| 29 |
-
"https://huggingface.co/deepghs/anime_rating/resolve/main/caformer_s36_plus/model.onnx"
|
| 30 |
-
).content
|
| 31 |
-
)
|
| 32 |
-
open("timmcfg.json", "wb").write(
|
| 33 |
-
requests.get(
|
| 34 |
-
"https://huggingface.co/deepghs/anime_rating/resolve/main/caformer_s36_plus/meta.json"
|
| 35 |
-
).content
|
| 36 |
-
)
|
| 37 |
-
else:
|
| 38 |
-
print("Model already exists, skipping redownload")
|
| 39 |
-
|
| 40 |
-
with open("timmcfg.json") as file:
|
| 41 |
-
tm_cfg = json.load(file)
|
| 42 |
-
|
| 43 |
-
nsfw_tm = _open_onnx_model("timm.onnx")
|
| 44 |
|
| 45 |
def launch(img):
|
| 46 |
-
|
| 47 |
-
img = img.convert('RGB')
|
| 48 |
-
tm_image = load_image(img, mode='RGB')
|
| 49 |
-
tm_input_ = _img_encode(tm_image, size=(256, 256))[None, ...]
|
| 50 |
-
tm_items, = nsfw_tm.run(['output'], {'input': tm_input_})
|
| 51 |
-
tm_output = sorted(list(zip(tm_cfg["labels"], map(lambda x: x.item(), tm_items[0]))), key=lambda x: x[1], reverse=True)[0][0]
|
| 52 |
-
|
| 53 |
-
match tm_output:
|
| 54 |
-
case "safe":
|
| 55 |
-
weight -= 1
|
| 56 |
-
case "r15":
|
| 57 |
-
weight += 2
|
| 58 |
-
case "r18":
|
| 59 |
-
weight += 2
|
| 60 |
-
|
| 61 |
-
tf_output = nsfw_tf(img)[0]["label"]
|
| 62 |
-
|
| 63 |
-
match tf_output:
|
| 64 |
-
case "safe":
|
| 65 |
-
weight -= 1
|
| 66 |
-
case "suggestive":
|
| 67 |
-
weight += 1
|
| 68 |
-
case "r18":
|
| 69 |
-
weight += 2
|
| 70 |
-
|
| 71 |
-
print(sorted(list(zip(tm_cfg["labels"], map(lambda x: x.item(), tm_items[0]))), key=lambda x: x[1], reverse=True), tf_output)
|
| 72 |
-
return weight > 0
|
| 73 |
-
|
| 74 |
-
app = gr.Interface(fn=launch, inputs="pil", outputs="text")
|
| 75 |
-
app.launch()
|
|
|
|
| 1 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification, pipeline
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
import timm
|
|
|
|
|
|
|
| 4 |
import torch
|
|
|
|
| 5 |
|
| 6 |
+
nsfw_tf = pipeline("image-classification",
|
| 7 |
+
model=AutoModelForImageClassification.from_pretrained(
|
| 8 |
+
"carbon225/vit-base-patch16-224-hentai"),
|
| 9 |
+
feature_extractor=AutoFeatureExtractor.from_pretrained(
|
| 10 |
+
"carbon225/vit-base-patch16-224-hentai"))
|
| 11 |
|
| 12 |
+
nsfw_tm = timm.create_model('deepghs/anime_rating', pretrained=True).eval()
|
| 13 |
+
tm_config = timm.data.resolve_model_data_config(model)
|
| 14 |
+
tm_trans = timm.data.create_transform(**tm_config, is_training=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def launch(img):
|
| 17 |
+
tm_output = nsfw_tm(transforms(img).unsqueeze(0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onnx_.py
DELETED
|
@@ -1,59 +0,0 @@
|
|
| 1 |
-
import logging
|
| 2 |
-
import os
|
| 3 |
-
import shutil
|
| 4 |
-
from functools import lru_cache
|
| 5 |
-
from typing import Optional
|
| 6 |
-
|
| 7 |
-
from hbutils.system import pip_install
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def _ensure_onnxruntime():
|
| 11 |
-
try:
|
| 12 |
-
import onnxruntime
|
| 13 |
-
except (ImportError, ModuleNotFoundError):
|
| 14 |
-
logging.warning('Onnx runtime not installed, preparing to install ...')
|
| 15 |
-
if shutil.which('nvidia-smi'):
|
| 16 |
-
logging.info('Installing onnxruntime-gpu ...')
|
| 17 |
-
pip_install(['onnxruntime-gpu'], silent=True)
|
| 18 |
-
else:
|
| 19 |
-
logging.info('Installing onnxruntime (cpu) ...')
|
| 20 |
-
pip_install(['onnxruntime'], silent=True)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
_ensure_onnxruntime()
|
| 24 |
-
from onnxruntime import get_available_providers, get_all_providers, InferenceSession, SessionOptions, \
|
| 25 |
-
GraphOptimizationLevel
|
| 26 |
-
|
| 27 |
-
alias = {
|
| 28 |
-
'gpu': "CUDAExecutionProvider",
|
| 29 |
-
"trt": "TensorrtExecutionProvider",
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def get_onnx_provider(provider: Optional[str] = None):
|
| 34 |
-
if not provider:
|
| 35 |
-
if "CUDAExecutionProvider" in get_available_providers():
|
| 36 |
-
return "CUDAExecutionProvider"
|
| 37 |
-
else:
|
| 38 |
-
return "CPUExecutionProvider"
|
| 39 |
-
elif provider.lower() in alias:
|
| 40 |
-
return alias[provider.lower()]
|
| 41 |
-
else:
|
| 42 |
-
for p in get_all_providers():
|
| 43 |
-
if provider.lower() == p.lower() or f'{provider}ExecutionProvider'.lower() == p.lower():
|
| 44 |
-
return p
|
| 45 |
-
|
| 46 |
-
raise ValueError(f'One of the {get_all_providers()!r} expected, '
|
| 47 |
-
f'but unsupported provider {provider!r} found.')
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
@lru_cache()
|
| 51 |
-
def _open_onnx_model(ckpt: str, provider: str = None) -> InferenceSession:
|
| 52 |
-
options = SessionOptions()
|
| 53 |
-
options.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALL
|
| 54 |
-
provider = provider or get_onnx_provider()
|
| 55 |
-
if provider == "CPUExecutionProvider":
|
| 56 |
-
options.intra_op_num_threads = os.cpu_count()
|
| 57 |
-
|
| 58 |
-
logging.info(f'Model {ckpt!r} loaded with provider {provider!r}')
|
| 59 |
-
return InferenceSession(ckpt, options, [provider])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
torch
|
| 2 |
transformers
|
| 3 |
-
|
| 4 |
-
dghs-imgutils
|
| 5 |
-
hbutils
|
|
|
|
| 1 |
torch
|
| 2 |
transformers
|
| 3 |
+
timm
|
|
|
|
|
|