Logan Zoellner commited on
Commit
92bfa46
β€’
1 Parent(s): e42c5f0

just use gradio interface for TADNE

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +28 -120
  3. requirements.txt +0 -5
  4. stylegan2-pytorch +0 -1
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: TADNE
3
  emoji: πŸƒ
4
  colorFrom: green
5
  colorTo: red
 
1
  ---
2
+ title: SafteyWaifu
3
  emoji: πŸƒ
4
  colorFrom: green
5
  colorTo: red
app.py CHANGED
@@ -1,129 +1,37 @@
1
- #!/usr/bin/env python
2
-
3
- from __future__ import annotations
4
-
5
- import argparse
6
- import functools
7
- import os
8
- import subprocess
9
- import sys
10
-
11
  import gradio as gr
12
- import numpy as np
13
- import torch
14
- import torch.nn as nn
15
- from huggingface_hub import hf_hub_download
16
-
17
- #if os.environ.get('SYSTEM') == 'spaces':
18
- # subprocess.call('git apply ../patch'.split(), cwd='stylegan2-pytorch')
19
-
20
- sys.path.insert(0, 'stylegan2-pytorch')
21
-
22
- from model import Generator
23
-
24
- TITLE = 'TADNE (This Anime Does Not Exist)'
25
- DESCRIPTION = '''The original TADNE site is https://thisanimedoesnotexist.ai/.
26
- 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).
27
-
28
- Expected execution time on Hugging Face Spaces: 4s
29
-
30
- Based off of [hysts/TADNE](https://huggingface.co/spaces/hysts/TADNE)
31
- '''
32
- SAMPLE_IMAGE_DIR = 'https://huggingface.co/spaces/hysts/TADNE/resolve/main/samples'
33
- ARTICLE = f'''## Generated images
34
- - size: 512x512
35
- - truncation: 0.7
36
- - seed: 0-99
37
- ![samples]({SAMPLE_IMAGE_DIR}/sample.jpg)
38
-
39
- '''
40
-
41
- TOKEN = os.environ['TOKEN']
42
 
 
 
 
 
 
 
 
 
43
 
44
- def parse_args() -> argparse.Namespace:
45
- parser = argparse.ArgumentParser()
46
- parser.add_argument('--device', type=str, default='cpu')
47
- parser.add_argument('--theme', type=str)
48
- parser.add_argument('--live', action='store_true')
49
- parser.add_argument('--share', action='store_true')
50
- parser.add_argument('--port', type=int)
51
- parser.add_argument('--disable-queue',
52
- dest='enable_queue',
53
- action='store_false')
54
- parser.add_argument('--allow-flagging', type=str, default='never')
55
- return parser.parse_args()
56
 
 
 
57
 
58
- def load_model(device: torch.device) -> nn.Module:
59
- model = Generator(512, 1024, 4, channel_multiplier=2)
60
- path = hf_hub_download('hysts/TADNE',
61
- 'models/aydao-anime-danbooru2019s-512-5268480.pt',
62
- use_auth_token=TOKEN)
63
- checkpoint = torch.load(path)
64
- model.load_state_dict(checkpoint['g_ema'])
65
- model.eval()
66
- model.to(device)
67
- model.latent_avg = checkpoint['latent_avg'].to(device)
68
- with torch.inference_mode():
69
- z = torch.zeros((1, model.style_dim)).to(device)
70
- model([z], truncation=0.7, truncation_latent=model.latent_avg)
71
- return model
72
 
73
-
74
- def generate_z(z_dim: int, seed: int, device: torch.device) -> torch.Tensor:
75
- return torch.from_numpy(np.random.RandomState(seed).randn(
76
- 1, z_dim)).to(device).float()
77
-
78
-
79
- @torch.inference_mode()
80
- def generate_image(seed: str, truncation_psi: float, randomize_noise: bool,
81
- model: nn.Module, device: torch.device) -> np.ndarray:
82
- seed = int(np.clip(seed, 0, np.iinfo(np.uint32).max))
83
-
84
- arr=bytearray.fromhex(seed[2:])
85
- z = np.random.RandomState(arr).randn(n_sample, inputSize).astype("float32")
86
-
87
- #z = generate_z(model.style_dim, seed, device)
88
-
89
- out, _ = model([z],
90
- truncation=truncation_psi,
91
- truncation_latent=model.latent_avg,
92
- randomize_noise=randomize_noise)
93
- out = (out.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
94
- return out[0].cpu().numpy()
95
-
96
-
97
- def main():
98
- args = parse_args()
99
- device = torch.device(args.device)
100
-
101
- model = load_model(device)
102
-
103
- func = functools.partial(generate_image, model=model, device=device)
104
- func = functools.update_wrapper(func, generate_image)
105
-
106
- gr.Interface(
107
- func,
108
- [
109
- gr.inputs.String(default="0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984", label='Seed'),
110
- gr.inputs.Slider(
111
- 0, 2, step=0.05, default=0.7, label='Truncation psi'),
112
- gr.inputs.Checkbox(default=False, label='Randomize Noise'),
113
- ],
114
- gr.outputs.Image(type='numpy', label='Output'),
115
- title=TITLE,
116
- description=DESCRIPTION,
117
- article=ARTICLE,
118
- theme=args.theme,
119
- allow_flagging=args.allow_flagging,
120
- live=args.live,
121
- ).launch(
122
- enable_queue=args.enable_queue,
123
- server_port=args.port,
124
- share=args.share,
125
  )
126
 
 
 
 
 
 
 
 
 
127
 
128
- if __name__ == '__main__':
129
- main()
 
1
+ from asyncio import constants
 
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
+ import requests
4
+ import os
5
+ import random
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ def desc_to_image(desc):
8
+
9
+ random.seed(desc)
10
+ tadneSeed=random.randint(0,2**256)
11
+ psi=0.7
12
+
13
+ iface = gr.Interface.load("spaces/hysts/TADNE")
14
+ print("about to die",iface,dir(iface))
15
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ img=iface(tadneSeed,psi)
18
+ return img
19
 
20
+ demo = gr.Blocks()
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ with demo:
23
+ gr.Markdown("<h1><center>NPC Generator</center></h1>")
24
+ gr.Markdown(
25
+ "based on <a href=https://huggingface.co/spaces/hysts/TADNE> TADNE</a>."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  )
27
 
28
+ with gr.Row():
29
+ desc_txt = gr.Textbox(label="description",lines=7)
30
+ output_image = gr.Image(label="portrait",type="filepath", shape=(256,256))
31
+
32
+ b0 = gr.Button("Randomize name,race and class")
33
+
34
+ b0.click(desc_to_image,desc_txt,output_image)
35
+ #examples=examples
36
 
37
+ demo.launch(enable_queue=True, debug=True)
 
requirements.txt DELETED
@@ -1,5 +0,0 @@
1
- ninja==1.10.2
2
- numpy==1.22.3
3
- Pillow==9.0.1
4
- torch==1.11.0
5
- torchvision==0.12.0
 
 
 
 
 
 
stylegan2-pytorch DELETED
@@ -1 +0,0 @@
1
- Subproject commit bef283a1c24087da704d16c30abc8e36e63efa0e