gojiteji commited on
Commit
384dde1
1 Parent(s): 57e2f92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -7
app.py CHANGED
@@ -3,6 +3,7 @@ from jax import pmap
3
  from flax.training.common_utils import shard
4
  import jax
5
  import jax.numpy as jnp
 
6
 
7
  from pathlib import Path
8
  from PIL import Image
@@ -44,12 +45,7 @@ def sd2_inference(pipeline, prompts, params, seed = 42, num_inference_steps = 50
44
  images = images.reshape((images.shape[0] * images.shape[1], ) + images.shape[-3:])
45
  images = pipeline.numpy_to_pil(images)
46
  return images
47
- def image_grid(imgs, rows, cols, down_sample = 1 ):
48
- w,h = imgs[0].size
49
- grid = Image.new('RGB', size=(cols*w, rows*h))
50
- for i, img in enumerate(imgs): grid.paste(img, box=(i%cols*w, i//cols*h))
51
- grid = grid.resize( (grid.size[0]//down_sample, grid.size[1]//down_sample) )
52
- return grid
53
 
54
 
55
  HF_ACCESS_TOKEN = os.environ["HFAUTH"]
@@ -64,4 +60,60 @@ pipeline, params = FlaxStableDiffusionPipeline.from_pretrained(
64
  )
65
 
66
  prompts = ["apple"] * 1
67
- images = sd2_inference(pipeline, prompts, params, seed = 42, num_inference_steps = 1 )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from flax.training.common_utils import shard
4
  import jax
5
  import jax.numpy as jnp
6
+ import gradio as gr
7
 
8
  from pathlib import Path
9
  from PIL import Image
 
45
  images = images.reshape((images.shape[0] * images.shape[1], ) + images.shape[-3:])
46
  images = pipeline.numpy_to_pil(images)
47
  return images
48
+
 
 
 
 
 
49
 
50
 
51
  HF_ACCESS_TOKEN = os.environ["HFAUTH"]
 
60
  )
61
 
62
  prompts = ["apple"] * 1
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+ def generate_image(dense_class_vector=None, int_index=None, noise_seed_vector=None, truncation=0.4):
73
+ seed = int(noise_seed_vector.sum().item()) if noise_seed_vector is not None else None
74
+ noise_vector = truncated_noise_sample(truncation=truncation, batch_size=1, seed=seed)
75
+ noise_vector = torch.from_numpy(noise_vector)
76
+ if int_index is not None:
77
+ class_vector = one_hot_from_int([int_index], batch_size=1)
78
+ class_vector = torch.from_numpy(class_vector)
79
+ dense_class_vector = gan_model.embeddings(class_vector)
80
+ else:
81
+ if isinstance(dense_class_vector, np.ndarray):
82
+ dense_class_vector = torch.tensor(dense_class_vector)
83
+ dense_class_vector = dense_class_vector.view(1, 128)
84
+
85
+ input_vector = torch.cat([noise_vector, dense_class_vector], dim=1)
86
+
87
+ # Generate an image
88
+ with torch.no_grad():
89
+ output = gan_model.generator(input_vector, truncation)
90
+ output = output.cpu().numpy()
91
+ output = output.transpose((0, 2, 3, 1))
92
+ output = ((output + 1.0) / 2.0) * 256
93
+ output.clip(0, 255, out=output)
94
+ output = np.asarray(np.uint8(output[0]), dtype=np.uint8)
95
+ return output
96
+
97
+
98
+
99
+ def text_to_image(text):
100
+ images = sd2_inference(pipeline, [text], params, seed = 42, num_inference_steps = 5 )
101
+ img = images[0]
102
+ return img
103
+
104
+ examples = ["apple",
105
+ "banana",
106
+ "chocolate"]
107
+
108
+ if __name__ == '__main__':
109
+ interFace = gr.Interface(fn=text_to_image,
110
+ inputs=gr.inputs.Textbox(placeholder="Enter the text to Encode to an image", label="Text "
111
+ "query",
112
+ lines=1),
113
+ outputs=gr.outputs.Image(type="auto", label="Generated Image"),
114
+ verbose=True,
115
+ examples=examples,
116
+ title="Generate Image from Text",
117
+ description="",
118
+ theme="huggingface")
119
+ interFace.launch()