Yash911 commited on
Commit
54c3682
·
1 Parent(s): 605f7a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +161 -7
app.py CHANGED
@@ -72,15 +72,169 @@ API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-
72
  headers = {"Authorization": "Bearer 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"}
73
 
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  def query_model_with_image(image_description):
76
- payload = {
77
- "inputs": image_description
78
- }
79
- response = requests.post(API_URL, headers=headers, json=payload)
80
- image_bytes = response.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
- image = Image.open(io.BytesIO(image_bytes))
83
- return image
84
 
85
  def upload_to_cloudinary(image, prompt_text):
86
  image_data = io.BytesIO()
 
72
  headers = {"Authorization": "Bearer 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"}
73
 
74
 
75
+ #@title Computer ko aang lagani ho to hi show code click karke ched chad karna
76
+
77
+ !pip install git+https://github.com/openai/glide-text2im
78
+
79
+ from PIL import Image
80
+ from IPython.display import display
81
+ import torch as th
82
+
83
+ from glide_text2im.download import load_checkpoint
84
+ from glide_text2im.model_creation import (
85
+ create_model_and_diffusion,
86
+ model_and_diffusion_defaults,
87
+ model_and_diffusion_defaults_upsampler
88
+ )
89
+
90
+ # This notebook supports both CPU and GPU.
91
+ # On CPU, generating one sample may take on the order of 20 minutes.
92
+ # On a GPU, it should be under a minute.
93
+
94
+ has_cuda = th.cuda.is_available()
95
+ device = th.device('cpu' if not has_cuda else 'cuda')
96
+
97
+ # Create base model.
98
+ options = model_and_diffusion_defaults()
99
+ options['use_fp16'] = has_cuda
100
+ options['timestep_respacing'] = '100' # use 100 diffusion steps for fast sampling
101
+ model, diffusion = create_model_and_diffusion(**options)
102
+ model.eval()
103
+ if has_cuda:
104
+ model.convert_to_fp16()
105
+ model.to(device)
106
+ model.load_state_dict(load_checkpoint('base', device))
107
+ print('total base parameters', sum(x.numel() for x in model.parameters()))
108
+
109
+ # Create upsampler model.
110
+ options_up = model_and_diffusion_defaults_upsampler()
111
+ options_up['use_fp16'] = has_cuda
112
+ options_up['timestep_respacing'] = 'fast27' # use 27 diffusion steps for very fast sampling
113
+ model_up, diffusion_up = create_model_and_diffusion(**options_up)
114
+ model_up.eval()
115
+ if has_cuda:
116
+ model_up.convert_to_fp16()
117
+ model_up.to(device)
118
+ model_up.load_state_dict(load_checkpoint('upsample', device))
119
+ print('total upsampler parameters', sum(x.numel() for x in model_up.parameters()))
120
+
121
+ def show_images(batch: th.Tensor):
122
+ """ Display a batch of images inline. """
123
+ scaled = ((batch + 1)*127.5).round().clamp(0,255).to(th.uint8).cpu()
124
+ reshaped = scaled.permute(2, 0, 3, 1).reshape([batch.shape[2], -1, 3])
125
+ display(Image.fromarray(reshaped.numpy()))
126
+
127
  def query_model_with_image(image_description):
128
+ # Sampling parameters
129
+ # image_description = "dog in the field" #@param {type:"string"}
130
+ # image_description = ""
131
+ batch_size = 1 #@param {type:"integer"}
132
+ guidance_scale = 8.0
133
+
134
+ # Tune this parameter to control the sharpness of 256x256 images.
135
+ # A value of 1.0 is sharper, but sometimes results in grainy artifacts.
136
+ upsample_temp = 0.997
137
+
138
+ ##############################
139
+ # Sample from the base model #
140
+ ##############################
141
+
142
+ # Create the text tokens to feed to the model.
143
+ tokens = model.tokenizer.encode(image_description)
144
+ tokens, mask = model.tokenizer.padded_tokens_and_mask(
145
+ tokens, options['text_ctx']
146
+ )
147
+
148
+ # Create the classifier-free guidance tokens (empty)
149
+ full_batch_size = batch_size * 2
150
+ uncond_tokens, uncond_mask = model.tokenizer.padded_tokens_and_mask(
151
+ [], options['text_ctx']
152
+ )
153
+
154
+ # Pack the tokens together into model kwargs.
155
+ model_kwargs = dict(
156
+ tokens=th.tensor(
157
+ [tokens] * batch_size + [uncond_tokens] * batch_size, device=device
158
+ ),
159
+ mask=th.tensor(
160
+ [mask] * batch_size + [uncond_mask] * batch_size,
161
+ dtype=th.bool,
162
+ device=device,
163
+ ),
164
+ )
165
+
166
+ # Create a classifier-free guidance sampling function
167
+ def model_fn(x_t, ts, **kwargs):
168
+ half = x_t[: len(x_t) // 2]
169
+ combined = th.cat([half, half], dim=0)
170
+ model_out = model(combined, ts, **kwargs)
171
+ eps, rest = model_out[:, :3], model_out[:, 3:]
172
+ cond_eps, uncond_eps = th.split(eps, len(eps) // 2, dim=0)
173
+ half_eps = uncond_eps + guidance_scale * (cond_eps - uncond_eps)
174
+ eps = th.cat([half_eps, half_eps], dim=0)
175
+ return th.cat([eps, rest], dim=1)
176
+
177
+ # Sample from the base model.
178
+ model.del_cache()
179
+ samples = diffusion.p_sample_loop(
180
+ model_fn,
181
+ (full_batch_size, 3, options["image_size"], options["image_size"]),
182
+ device=device,
183
+ clip_denoised=True,
184
+ progress=True,
185
+ model_kwargs=model_kwargs,
186
+ cond_fn=None,
187
+ )[:batch_size]
188
+ model.del_cache()
189
+
190
+ # Show the output
191
+ show_images(samples)
192
+
193
+
194
+ ##############################
195
+ # Upsample the 64x64 samples #
196
+ ##############################
197
+
198
+ tokens = model_up.tokenizer.encode(image_description)
199
+ tokens, mask = model_up.tokenizer.padded_tokens_and_mask(
200
+ tokens, options_up['text_ctx']
201
+ )
202
+
203
+ # Create the model conditioning dict.
204
+ model_kwargs = dict(
205
+ # Low-res image to upsample.
206
+ low_res=((samples+1)*127.5).round()/127.5 - 1,
207
+
208
+ # Text tokens
209
+ tokens=th.tensor(
210
+ [tokens] * batch_size, device=device
211
+ ),
212
+ mask=th.tensor(
213
+ [mask] * batch_size,
214
+ dtype=th.bool,
215
+ device=device,
216
+ ),
217
+ )
218
+
219
+ # Sample from the base model.
220
+ model_up.del_cache()
221
+ up_shape = (batch_size, 3, options_up["image_size"], options_up["image_size"])
222
+ image = diffusion_up.ddim_sample_loop(
223
+ model_up,
224
+ up_shape,
225
+ noise=th.randn(up_shape, device=device) * upsample_temp,
226
+ device=device,
227
+ clip_denoised=True,
228
+ progress=True,
229
+ model_kwargs=model_kwargs,
230
+ cond_fn=None,
231
+ )[:batch_size]
232
+ model_up.del_cache()
233
+
234
+ # Show the output
235
+ show_images(image)
236
+ return image
237
 
 
 
238
 
239
  def upload_to_cloudinary(image, prompt_text):
240
  image_data = io.BytesIO()