Spaces:
Running
Running
Update
Browse filesFiles changed (4)
- .gitmodulesDeleted-3
- DeepDanbooruDeleted-1
- app.py+32-40
- requirements.txt+1
123[submodule "DeepDanbooru"] path = DeepDanbooru url = https://github.com/KichangKim/DeepDanbooru1Subproject commit 92ba0b56be5eed0037e3f067bb9867f5ac6916476 unmodified lines78910111213101112131415192021221617183 unmodified lines22232432333425262728293012 unmodified lines4344455354555657585960464748495062515264536667545556575870717273747559606162636465666768697071727374757879767778798018 unmodified lines991001011041021031041051081091101111121131141151061071081091106 unmodified linesimport osimport pathlibimport subprocessimport sysimport urllibimport zipfilefrom typing import Callableimport tarfile
# workaround for https://github.com/gradio-app/gradio/issues/483command = 'pip install -U gradio==2.7.0'subprocess.call(command.split())
command = 'pip install -r DeepDanbooru/requirements.txt'subprocess.call(command.split())sys.path.insert(0, 'DeepDanbooru')
import deepdanbooru as ddimport gradio as grimport huggingface_hub3 unmodified lines
TOKEN = os.environ['TOKEN']
ZIP_PATH = 'data.zip'TAG_PATH = 'tags.txt'MODEL_PATH = 'model-resnet_custom_v3.h5'MODEL_REPO = 'hysts/DeepDanbooru'MODEL_FILENAME = 'model-resnet_custom_v3.h5'LABEL_FILENAME = 'tags.txt'
def parse_args() -> argparse.Namespace:12 unmodified lines return parser.parse_args()
def download_sample_images() -> list[pathlib.Path]: image_dir = pathlib.Path('samples') image_dir.mkdir(exist_ok=True)
dataset_repo = 'hysts/sample-images-TADNE' n_images = 36 paths = [] for index in range(n_images):def load_sample_image_paths() -> list[pathlib.Path]: image_dir = pathlib.Path('images') if not image_dir.exists(): dataset_repo = 'hysts/sample-images-TADNE' path = huggingface_hub.hf_hub_download(dataset_repo, f'{index:02d}.jpg', 'images.tar.gz', repo_type='dataset', cache_dir=image_dir.as_posix(), use_auth_token=TOKEN) paths.append(pathlib.Path(path)) return paths with tarfile.open(path) as f: f.extractall() return sorted(image_dir.glob('*'))
def download_model_data() -> None: url = 'https://github.com/KichangKim/DeepDanbooru/releases/download/v3-20200915-sgd-e30/deepdanbooru-v3-20200915-sgd-e30.zip' urllib.request.urlretrieve(url, ZIP_PATH) with zipfile.ZipFile(ZIP_PATH) as f: f.extract(TAG_PATH) f.extract(MODEL_PATH)def load_model() -> tf.keras.Model: path = huggingface_hub.hf_hub_download(MODEL_REPO, MODEL_FILENAME, use_auth_token=TOKEN) model = tf.keras.models.load_model(path) return model
def load_labels() -> list[str]: path = huggingface_hub.hf_hub_download(MODEL_REPO, LABEL_FILENAME, use_auth_token=TOKEN) with open(path) as f: labels = [line.strip() for line in f.readlines()] return labels
def predict(image: PIL.Image.Image, score_threshold: float, model, labels: list[str]) -> dict[str, float]:def predict(image: PIL.Image.Image, score_threshold: float, model: tf.keras.Model, labels: list[str]) -> dict[str, float]: _, height, width, _ = model.input_shape image = np.asarray(image) image = tf.image.resize(image,18 unmodified lines
args = parse_args()
image_paths = download_sample_images() image_paths = load_sample_image_paths() examples = [[path.as_posix(), args.score_threshold] for path in image_paths]
zip_path = pathlib.Path(ZIP_PATH) if not zip_path.exists(): download_model_data()
model = tf.keras.models.load_model(MODEL_PATH)
with open(TAG_PATH) as f: labels = [line.strip() for line in f.readlines()] model = load_model() labels = load_labels()
func = functools.partial(predict, model=model, labels=labels) func = functools.update_wrapper(func, predict)123pillow>=9.0.0tensorflow>=2.7.0git+https://github.com/KichangKim/DeepDanbooru@v3-20200915-sgd-e30#egg=deepdanbooru.gitmodulesDeleted-3
DeepDanbooruDeleted-1
app.py+32-40
requirements.txt+1