Spaces:
Runtime error
Runtime error
Created app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from tqdm import tqdm
|
3 |
+
from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor, Normalize
|
4 |
+
import torchvision
|
5 |
+
import BigGAN_utils.utils as utils
|
6 |
+
import clip
|
7 |
+
import torch.nn.functional as F
|
8 |
+
from DiffAugment_pytorch import DiffAugment
|
9 |
+
import numpy as np
|
10 |
+
from fusedream_utils import FuseDreamBaseGenerator, get_G, save_image
|
11 |
+
import gradio as gr
|
12 |
+
|
13 |
+
|
14 |
+
def Text_To_Img(prompt, INIT_ITERS, OPT_ITERS, NUM_BASIS, MODEL, seed):
|
15 |
+
import sys
|
16 |
+
sys.argv = [''] ### workaround to deal with the argparse in Jupyter
|
17 |
+
|
18 |
+
utils.seed_rng(seed)
|
19 |
+
if MODEL == "biggan-256":
|
20 |
+
G, config = get_G(256)
|
21 |
+
elif MODEL == "biggan-512":
|
22 |
+
G, config = get_G(512)
|
23 |
+
else:
|
24 |
+
raise Exception('Model not supported')
|
25 |
+
|
26 |
+
generator = FuseDreamBaseGenerator(G, config, 10)
|
27 |
+
z_cllt, y_cllt = generator.generate_basis(prompt, init_iters=INIT_ITERS, num_basis=NUM_BASIS)
|
28 |
+
z_cllt_save = torch.cat(z_cllt).cpu().numpy()
|
29 |
+
y_cllt_save = torch.cat(y_cllt).cpu().numpy()
|
30 |
+
img, z, y = generator.optimize_clip_score(z_cllt, y_cllt, sentence, latent_noise=False, augment=True, opt_iters=OPT_ITERS, optimize_y=True)
|
31 |
+
|
32 |
+
score = generator.measureAugCLIP(z, y, sentence, augment=True, num_samples=20)
|
33 |
+
|
34 |
+
return img, score
|
35 |
+
|
36 |
+
|
37 |
+
demo = gr.Interface(
|
38 |
+
fn = Text_To_Img,
|
39 |
+
inputs = [
|
40 |
+
gr.inputs.Textbox(lines=1, placeholder="Enter your text prompt here.", default="", label="Prompt", optional=False),
|
41 |
+
gr.inputs.Slider(100, 10000, step=20, default=500, label="Init_Iters"),
|
42 |
+
gr.inputs.Slider(100, 10000, step=20, default=500, label="Opt_Iters"),
|
43 |
+
gr.inputs.Slider(0, 1000, step=5, default=5, label="Num_Basis"),
|
44 |
+
gr.inputs.Radio(["biggan-256", "biggan-512"], default="biggan-512", label="Model"),
|
45 |
+
gr.inputs.Slider(0, 1000, step=0, default=0, label="Seed")
|
46 |
+
],
|
47 |
+
|
48 |
+
outputs = [
|
49 |
+
gr.outputs.Image(type="auto", label="Generated Image"),
|
50 |
+
gr.outputs.Textbox(type="str", label="AugCLIP Score")
|
51 |
+
],
|
52 |
+
|
53 |
+
#live=True,
|
54 |
+
#examples = [
|
55 |
+
# ["A beautiful morning sunrise in the fields", 1000, 1000, 5, "biggan-512", 0],
|
56 |
+
# ["A black pyramid", 500, 500, 5, "biggan-512", 1729],
|
57 |
+
# ["Two dogs sitting on a bench in a park", 500, 500, 5, "biggan-256", 1234],
|
58 |
+
# ],
|
59 |
+
#theme = "dark",
|
60 |
+
title = "Text to Image Generation",
|
61 |
+
description = "This was achieved using a combination of BIGGAN as the image generator and AugCLIP as the image-prompt correlator. Below are some examples. Try them for yourself!",
|
62 |
+
)
|
63 |
+
|
64 |
+
|
65 |
+
if __name__ == "__main__":
|
66 |
+
app, local_url, share_url = demo.launch(debug = True)
|