Spaces:
Runtime error
Runtime error
File size: 4,151 Bytes
161d647 241747c 161d647 4523071 241747c 161d647 4523071 161d647 241747c 161d647 241747c 161d647 241747c 161d647 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
#!/usr/bin/env python
from __future__ import annotations
import argparse
import functools
import os
import subprocess
import sys
import gradio as gr
import numpy as np
import torch
import torch.nn as nn
from huggingface_hub import hf_hub_download
#if os.environ.get('SYSTEM') == 'spaces':
# subprocess.call('git apply ../patch'.split(), cwd='stylegan2-pytorch')
sys.path.insert(0, 'stylegan2-pytorch')
from model import Generator
TITLE = 'TADNE (This Anime Does Not Exist)'
DESCRIPTION = '''The original TADNE site is https://thisanimedoesnotexist.ai/.
The model used here is the one converted from the model provided in [this site](https://www.gwern.net/Faces) using [this repo](https://github.com/rosinality/stylegan2-pytorch).
Expected execution time on Hugging Face Spaces: 4s
Based off of [hysts/TADNE](https://huggingface.co/spaces/hysts/TADNE)
'''
SAMPLE_IMAGE_DIR = 'https://huggingface.co/spaces/hysts/TADNE/resolve/main/samples'
ARTICLE = f'''## Generated images
- size: 512x512
- truncation: 0.7
- seed: 0-99

'''
TOKEN = os.environ['TOKEN']
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')
return parser.parse_args()
def load_model(device: torch.device) -> nn.Module:
model = Generator(512, 1024, 4, channel_multiplier=2)
path = hf_hub_download('hysts/TADNE',
'models/aydao-anime-danbooru2019s-512-5268480.pt',
use_auth_token=TOKEN)
checkpoint = torch.load(path)
model.load_state_dict(checkpoint['g_ema'])
model.eval()
model.to(device)
model.latent_avg = checkpoint['latent_avg'].to(device)
with torch.inference_mode():
z = torch.zeros((1, model.style_dim)).to(device)
model([z], truncation=0.7, truncation_latent=model.latent_avg)
return model
def generate_z(z_dim: int, seed: int, device: torch.device) -> torch.Tensor:
return torch.from_numpy(np.random.RandomState(seed).randn(
1, z_dim)).to(device).float()
@torch.inference_mode()
def generate_image(seed: str, truncation_psi: float, randomize_noise: bool,
model: nn.Module, device: torch.device) -> np.ndarray:
seed = int(np.clip(seed, 0, np.iinfo(np.uint32).max))
arr=bytearray.fromhex(seed[2:])
z = np.random.RandomState(arr).randn(n_sample, inputSize).astype("float32")
#z = generate_z(model.style_dim, seed, device)
out, _ = model([z],
truncation=truncation_psi,
truncation_latent=model.latent_avg,
randomize_noise=randomize_noise)
out = (out.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
return out[0].cpu().numpy()
def main():
args = parse_args()
device = torch.device(args.device)
model = load_model(device)
func = functools.partial(generate_image, model=model, device=device)
func = functools.update_wrapper(func, generate_image)
gr.Interface(
func,
[
gr.inputs.String(default="0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984", label='Seed'),
gr.inputs.Slider(
0, 2, step=0.05, default=0.7, label='Truncation psi'),
gr.inputs.Checkbox(default=False, label='Randomize Noise'),
],
gr.outputs.Image(type='numpy', label='Output'),
title=TITLE,
description=DESCRIPTION,
article=ARTICLE,
theme=args.theme,
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()
|