Spaces:
Runtime error
Runtime error
Anonymous941
commited on
Commit
•
fb94a5d
1
Parent(s):
a5a93d6
Update app.py
Browse files
app.py
CHANGED
@@ -3,3 +3,43 @@ from datasets import load_dataset, Image
|
|
3 |
|
4 |
dataset = load_dataset("botmaster/mother-2-battle-sprites", split="train")
|
5 |
gr.Interface.load("models/templates/text-to-image").launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
dataset = load_dataset("botmaster/mother-2-battle-sprites", split="train")
|
5 |
gr.Interface.load("models/templates/text-to-image").launch()
|
6 |
+
|
7 |
+
import torch
|
8 |
+
import nltk
|
9 |
+
import io
|
10 |
+
import base64
|
11 |
+
import shutil
|
12 |
+
from torchvision import transforms
|
13 |
+
|
14 |
+
from pytorch_pretrained_biggan import BigGAN, one_hot_from_names, truncated_noise_sample
|
15 |
+
|
16 |
+
class PreTrainedPipeline():
|
17 |
+
def __init__(self, path=""):
|
18 |
+
"""
|
19 |
+
Initialize model
|
20 |
+
"""
|
21 |
+
nltk.download('wordnet')
|
22 |
+
self.model = BigGAN.from_pretrained(path)
|
23 |
+
self.truncation = 0.1
|
24 |
+
|
25 |
+
def __call__(self, inputs: str):
|
26 |
+
"""
|
27 |
+
Args:
|
28 |
+
inputs (:obj:`str`):
|
29 |
+
a string containing some text
|
30 |
+
Return:
|
31 |
+
A :obj:`PIL.Image` with the raw image representation as PIL.
|
32 |
+
"""
|
33 |
+
class_vector = one_hot_from_names([inputs], batch_size=1)
|
34 |
+
if type(class_vector) == type(None):
|
35 |
+
raise ValueError("Input is not in ImageNet")
|
36 |
+
noise_vector = truncated_noise_sample(truncation=self.truncation, batch_size=1)
|
37 |
+
noise_vector = torch.from_numpy(noise_vector)
|
38 |
+
class_vector = torch.from_numpy(class_vector)
|
39 |
+
with torch.no_grad():
|
40 |
+
output = self.model(noise_vector, class_vector, self.truncation)
|
41 |
+
|
42 |
+
# Scale image
|
43 |
+
img = output[0]
|
44 |
+
img = (img + 1) / 2.0
|
45 |
+
img = transforms.ToPILImage()(img)
|