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 lines789101112131415161718192021222324253 unmodified lines29303132333435363712 unmodified lines50515253545556575859606162636465666768697071727374757677787980818218 unmodified lines1011021031041051061071081091101111121131141151161171186 unmodified linesimport osimport pathlibimport subprocessimport sysimport urllibimport zipfilefrom typing import Callable# 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 linesTOKEN = os.environ['TOKEN']ZIP_PATH = 'data.zip'TAG_PATH = 'tags.txt'MODEL_PATH = 'model-resnet_custom_v3.h5'def parse_args() -> argparse.Namespace:12 unmodified linesreturn 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 = 36paths = []for index in range(n_images):path = huggingface_hub.hf_hub_download(dataset_repo,f'{index:02d}.jpg',repo_type='dataset',cache_dir=image_dir.as_posix(),use_auth_token=TOKEN)paths.append(pathlib.Path(path))return pathsdef 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 predict(image: PIL.Image.Image, score_threshold: float, model,labels: list[str]) -> dict[str, float]:_, height, width, _ = model.input_shapeimage = np.asarray(image)image = tf.image.resize(image,18 unmodified linesargs = parse_args()image_paths = download_sample_images()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()]func = functools.partial(predict, model=model, labels=labels)func = functools.update_wrapper(func, predict)6 unmodified lines7891011121314151617183 unmodified lines22232425262728293012 unmodified lines434445464748495051525354555657585960616263646566676869707172737475767778798018 unmodified lines991001011021031041051061071081091106 unmodified linesimport osimport pathlibimport subprocessimport tarfile# workaround for https://github.com/gradio-app/gradio/issues/483command = 'pip install -U gradio==2.7.0'subprocess.call(command.split())import deepdanbooru as ddimport gradio as grimport huggingface_hub3 unmodified linesTOKEN = os.environ['TOKEN']MODEL_REPO = 'hysts/DeepDanbooru'MODEL_FILENAME = 'model-resnet_custom_v3.h5'LABEL_FILENAME = 'tags.txt'def parse_args() -> argparse.Namespace:12 unmodified linesreturn parser.parse_args()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,'images.tar.gz',repo_type='dataset',use_auth_token=TOKEN)with tarfile.open(path) as f:f.extractall()return sorted(image_dir.glob('*'))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 modeldef 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 labelsdef predict(image: PIL.Image.Image, score_threshold: float,model: tf.keras.Model, labels: list[str]) -> dict[str, float]:_, height, width, _ = model.input_shapeimage = np.asarray(image)image = tf.image.resize(image,18 unmodified linesargs = parse_args()image_paths = load_sample_image_paths()examples = [[path.as_posix(), args.score_threshold]for path in image_paths]model = load_model()labels = load_labels()func = functools.partial(predict, model=model, labels=labels)func = functools.update_wrapper(func, predict)
12pillow>=9.0.0tensorflow>=2.7.0123pillow>=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