Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,25 +2,12 @@ import os, re, cv2
|
|
| 2 |
from typing import Mapping, Tuple, Dict
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
from PIL import Image
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
from onnxruntime import InferenceSession
|
| 9 |
|
| 10 |
-
from functools import wraps
|
| 11 |
-
|
| 12 |
-
def memoize(func):
|
| 13 |
-
cache = {}
|
| 14 |
-
cache_maxsize = 25
|
| 15 |
-
@wraps(func)
|
| 16 |
-
def memoized_func(*args):
|
| 17 |
-
if args not in cache:
|
| 18 |
-
cache[args] = func(*args)
|
| 19 |
-
if len(cache) > cache_maxsize:
|
| 20 |
-
cache.popitem(last=False)
|
| 21 |
-
return cache[args]
|
| 22 |
-
|
| 23 |
-
return memoized_func
|
| 24 |
# noinspection PyUnresolvedReferences
|
| 25 |
def make_square(img, target_size):
|
| 26 |
old_size = img.shape[:2]
|
|
@@ -64,7 +51,7 @@ class WaifuDiffusionInterrogator:
|
|
| 64 |
|
| 65 |
self.__initialized = False
|
| 66 |
self._model, self._tags = None, None
|
| 67 |
-
|
| 68 |
def _init(self) -> None:
|
| 69 |
if self.__initialized:
|
| 70 |
return
|
|
@@ -111,8 +98,14 @@ class WaifuDiffusionInterrogator:
|
|
| 111 |
full_tags['confidence'] = confidence[0]
|
| 112 |
|
| 113 |
return full_tags
|
| 114 |
-
|
| 115 |
def interrogate(self, image: Image) -> Tuple[Dict[str, float], Dict[str, float]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
full_tags = self._calculation(image)
|
| 117 |
|
| 118 |
# first 4 items are for rating (general, sensitive, questionable, explicit)
|
|
@@ -120,6 +113,9 @@ class WaifuDiffusionInterrogator:
|
|
| 120 |
|
| 121 |
# rest are regular tags
|
| 122 |
tags = dict(full_tags[full_tags['category'] != 9][['name', 'confidence']].values)
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
return ratings, tags
|
| 125 |
|
|
@@ -132,7 +128,7 @@ WAIFU_MODELS: Mapping[str, WaifuDiffusionInterrogator] = {
|
|
| 132 |
}
|
| 133 |
RE_SPECIAL = re.compile(r'([\\()])')
|
| 134 |
|
| 135 |
-
|
| 136 |
def image_to_wd14_tags(image: Image.Image, model_name: str, threshold: float,
|
| 137 |
use_spaces: bool, use_escape: bool, include_ranks=False, score_descend=True) \
|
| 138 |
-> Tuple[Mapping[str, float], str, Mapping[str, float]]:
|
|
|
|
| 2 |
from typing import Mapping, Tuple, Dict
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
+
import io
|
| 6 |
import pandas as pd
|
| 7 |
from PIL import Image
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
from onnxruntime import InferenceSession
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# noinspection PyUnresolvedReferences
|
| 12 |
def make_square(img, target_size):
|
| 13 |
old_size = img.shape[:2]
|
|
|
|
| 51 |
|
| 52 |
self.__initialized = False
|
| 53 |
self._model, self._tags = None, None
|
| 54 |
+
self.cache = {}
|
| 55 |
def _init(self) -> None:
|
| 56 |
if self.__initialized:
|
| 57 |
return
|
|
|
|
| 98 |
full_tags['confidence'] = confidence[0]
|
| 99 |
|
| 100 |
return full_tags
|
|
|
|
| 101 |
def interrogate(self, image: Image) -> Tuple[Dict[str, float], Dict[str, float]]:
|
| 102 |
+
|
| 103 |
+
imgByteArr = io.BytesIO()
|
| 104 |
+
image.save(imgByteArr, format=image.format)
|
| 105 |
+
imgByteArr = imgByteArr.getvalue()
|
| 106 |
+
if imgByteArr in cache:
|
| 107 |
+
return cache[imgByteArr]
|
| 108 |
+
|
| 109 |
full_tags = self._calculation(image)
|
| 110 |
|
| 111 |
# first 4 items are for rating (general, sensitive, questionable, explicit)
|
|
|
|
| 113 |
|
| 114 |
# rest are regular tags
|
| 115 |
tags = dict(full_tags[full_tags['category'] != 9][['name', 'confidence']].values)
|
| 116 |
+
self.cache[imgByteArr] = (ratings, tags)
|
| 117 |
+
if len(cache) > 25:
|
| 118 |
+
cache.popitem(last=False)
|
| 119 |
|
| 120 |
return ratings, tags
|
| 121 |
|
|
|
|
| 128 |
}
|
| 129 |
RE_SPECIAL = re.compile(r'([\\()])')
|
| 130 |
|
| 131 |
+
|
| 132 |
def image_to_wd14_tags(image: Image.Image, model_name: str, threshold: float,
|
| 133 |
use_spaces: bool, use_escape: bool, include_ranks=False, score_descend=True) \
|
| 134 |
-> Tuple[Mapping[str, float], str, Mapping[str, float]]:
|