Mehdi Cherti commited on
Commit
640f9c9
1 Parent(s): 972820d

add app file

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import torch
3
+ import torchvision
4
+ import gradio as gr
5
+ from PIL import Image
6
+ import torchvision
7
+ from test_ddgan import load_model, sample
8
+ from model_configs import get_model_config
9
+ from huggingface_hub import hf_hub_download
10
+
11
+ def download(filename):
12
+ return "models/" + filename
13
+
14
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
15
+ models = {
16
+ "diffusion_db_128ch_1timesteps_openclip_vith14": load_model(get_model_config('ddgan_ddb_v2'), download('diffusion_db_128ch_1timesteps_openclip_vith14.th'), device=device),
17
+ "diffusion_db_192ch_2timesteps_openclip_vith14": load_model(get_model_config('ddgan_ddb_v3'), download('diffusion_db_192ch_2timesteps_openclip_vith14.th'), device=device),
18
+ }
19
+ default = "diffusion_db_128ch_1timesteps_openclip_vith14"
20
+
21
+ def gen(md, model_name, md2, text, seed, nb_samples, width, height):
22
+ torch.manual_seed(int(seed))
23
+ model = models[model_name]
24
+ nb_samples = int(nb_samples)
25
+ height = int(height)
26
+ width = int(width)
27
+ with torch.no_grad():
28
+ cond = model.text_encoder([text]*nb_samples)
29
+ if text == "":
30
+ cond[0].normal_()
31
+ cond[1].normal_()
32
+ cond[0][1:] = cond[0][0:1]
33
+ cond[1][1:] = cond[1][0:1]
34
+
35
+ x_init = torch.randn(nb_samples, 3, height, width).to(device)
36
+ fake_sample = sample(model, x_init=x_init, cond=cond)
37
+ fake_sample = (fake_sample + 1) / 2
38
+ grid = torchvision.utils.make_grid(fake_sample, nrow=4)
39
+ grid = grid.permute(1, 2, 0).cpu().numpy()
40
+ grid = (grid*255).astype("uint8")
41
+ return Image.fromarray(grid)
42
+
43
+ text = """
44
+ DDGAN
45
+ """
46
+ iface = gr.Interface(
47
+ fn=gen,
48
+ inputs=[
49
+ gr.Markdown(text),
50
+ # text caption
51
+ gr.Dropdown(list(models.keys()), value=default),
52
+ gr.Markdown("If text caption is empty, random CLIP embeddings will be used as input"),
53
+ gr.Textbox(
54
+ lines=1,
55
+ placeholder="Enter text caption here, or leave empty",
56
+ value="Painting of a hamster king with a crown and a cape in a magical forest."
57
+ ),
58
+ gr.Number(value=0), # seed
59
+ gr.Number(value=4), # nb_samples
60
+ gr.Number(value=256), # width
61
+ gr.Number(value=256),# height
62
+ ],
63
+ outputs="image"
64
+ )
65
+ iface.launch()