|
|
|
|
|
from __future__ import annotations |
|
import argparse |
|
import functools |
|
import os |
|
import pathlib |
|
import sys |
|
from typing import Callable |
|
|
|
|
|
import gradio as gr |
|
import huggingface_hub |
|
import numpy as np |
|
import PIL.Image |
|
|
|
from io import BytesIO |
|
import ugatit_test |
|
|
|
ORIGINAL_REPO_URL = 'https://github.com/taki0112/UGATIT' |
|
TITLE = 'taki0112/UGATIT' |
|
DESCRIPTION = f"""This is a demo for {ORIGINAL_REPO_URL}. |
|
|
|
""" |
|
ARTICLE = """ |
|
|
|
""" |
|
|
|
|
|
MODEL_REPO = 'hylee/UGATIT_model' |
|
|
|
def parse_args() -> argparse.Namespace: |
|
parser = argparse.ArgumentParser() |
|
parser.add_argument('--device', type=str, default='cpu') |
|
parser.add_argument('--theme', type=str) |
|
parser.add_argument('--live', action='store_true') |
|
parser.add_argument('--share', action='store_true') |
|
parser.add_argument('--port', type=int) |
|
parser.add_argument('--disable-queue', |
|
dest='enable_queue', |
|
action='store_false') |
|
parser.add_argument('--allow-flagging', type=str, default='never') |
|
parser.add_argument('--allow-screenshot', action='store_true') |
|
return parser.parse_args() |
|
|
|
|
|
def load_checkpoint(): |
|
dir = 'checkpoint' |
|
name = 'UGATIT_selfie2anime_lsgan_4resblock_6dis_1_1_10_10_1000_sn_smoothing' |
|
checkpoint_path = huggingface_hub.hf_hub_download(MODEL_REPO, |
|
name+'/checkpoint', |
|
cache_dir=dir+'/'+name, |
|
force_filename='checkpoint') |
|
print(checkpoint_path) |
|
|
|
checkpoint_path = huggingface_hub.hf_hub_download(MODEL_REPO, |
|
name + '/UGATIT.model-1000000.index', |
|
cache_dir=dir+'/'+name, |
|
force_filename='UGATIT.model-1000000.index') |
|
print(checkpoint_path) |
|
|
|
checkpoint_path = huggingface_hub.hf_hub_download(MODEL_REPO, |
|
name + '/UGATIT.model-1000000.meta', |
|
cache_dir=dir+'/'+name, |
|
force_filename='UGATIT.model-1000000.meta') |
|
print(checkpoint_path) |
|
|
|
checkpoint_path = huggingface_hub.hf_hub_download(MODEL_REPO, |
|
name + '/UGATIT.model-1000000.data-00000-of-00001', |
|
cache_dir=dir+'/'+name, |
|
force_filename='UGATIT.model-1000000.data-00000-of-00001') |
|
print(checkpoint_path) |
|
|
|
return dir |
|
|
|
|
|
def run( |
|
image, |
|
checkpoint_dir: str, |
|
) -> tuple[PIL.Image.Image]: |
|
|
|
result = ugatit_test.main_test(image.name, checkpoint_dir) |
|
|
|
return PIL.Image.open(result) |
|
|
|
|
|
def main(): |
|
gr.close_all() |
|
|
|
args = parse_args() |
|
|
|
checkpoint_dir = load_checkpoint() |
|
|
|
func = functools.partial(run, checkpoint_dir=checkpoint_dir) |
|
func = functools.update_wrapper(func, run) |
|
|
|
|
|
gr.Interface( |
|
func, |
|
[ |
|
gr.inputs.Image(type='file', label='Input Image'), |
|
], |
|
[ |
|
gr.outputs.Image( |
|
type='pil', |
|
label='Result'), |
|
], |
|
|
|
theme=args.theme, |
|
title=TITLE, |
|
description=DESCRIPTION, |
|
article=ARTICLE, |
|
allow_screenshot=args.allow_screenshot, |
|
allow_flagging=args.allow_flagging, |
|
live=args.live, |
|
).launch( |
|
enable_queue=args.enable_queue, |
|
server_port=args.port, |
|
share=args.share, |
|
) |
|
|
|
|
|
if __name__ == '__main__': |
|
main() |
|
|