1inkusFace commited on
Commit
690a432
·
verified ·
1 Parent(s): 7d3c40d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -64
app.py CHANGED
@@ -232,6 +232,101 @@ def uploadNote(prompt,num_inference_steps,guidance_scale,timestamp):
232
  f.write(f"Model VAE: sdxl-vae to bfloat safetensor=false before cuda then attn_proc / scale factor 8 \n")
233
  f.write(f"Model UNET: ford442/RealVisXL_V5.0_BF16 \n")
234
  upload_to_ftp(filename)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235
 
236
  @spaces.GPU(duration=40)
237
  def generate_30(
@@ -266,39 +361,45 @@ def generate_30(
266
  sd_image_a = Image.open(latent_file.name).convert('RGB')
267
  sd_image_a.resize((height,width), Image.LANCZOS)
268
  caption=[]
 
269
  caption.append(captioner(sd_image_a))
270
  caption.append(captioner_2(sd_image_a))
271
  caption.append(captioner_3(sd_image_a))
 
272
  if latent_file_2 is not None: # Check if a latent file is provided
273
  sd_image_b = Image.open(latent_file_2.name).convert('RGB')
274
  sd_image_b.resize((height,width), Image.LANCZOS)
275
- caption.append(captioner(sd_image_a))
276
- caption.append(captioner_2(sd_image_a))
277
- caption.append(captioner_3(sd_image_a))
 
278
  else:
279
  sd_image_b = None
280
  if latent_file_3 is not None: # Check if a latent file is provided
281
  sd_image_c = Image.open(latent_file_3.name).convert('RGB')
282
  sd_image_c.resize((height,width), Image.LANCZOS)
283
- caption.append(captioner(sd_image_a))
284
- caption.append(captioner_2(sd_image_a))
285
- caption.append(captioner_3(sd_image_a))
 
286
  else:
287
  sd_image_c = None
288
  if latent_file_4 is not None: # Check if a latent file is provided
289
  sd_image_d = Image.open(latent_file_4.name).convert('RGB')
290
  sd_image_d.resize((height,width), Image.LANCZOS)
291
- caption.append(captioner(sd_image_a))
292
- caption.append(captioner_2(sd_image_a))
293
- caption.append(captioner_3(sd_image_a))
 
294
  else:
295
  sd_image_d = None
296
  if latent_file_5 is not None: # Check if a latent file is provided
297
  sd_image_e = Image.open(latent_file_5.name).convert('RGB')
298
  sd_image_e.resize((height,width), Image.LANCZOS)
299
- caption.append(captioner(sd_image_a))
300
- caption.append(captioner_2(sd_image_a))
301
- caption.append(captioner_3(sd_image_a))
 
302
  else:
303
  sd_image_e = None
304
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
@@ -307,58 +408,9 @@ def generate_30(
307
  print(caption)
308
  print("-- generating further caption --")
309
 
310
-
311
- system_prompt_rewrite = (
312
- "You are an AI assistant that rewrites image prompts to be more descriptive and detailed."
313
- )
314
- user_prompt_rewrite = (
315
- "Rewrite this prompt to be more descriptive and detailed and only return the rewritten text: "
316
- )
317
- user_prompt_rewrite_2 = (
318
- "Rephrase this scene to have more elaborate details: "
319
- )
320
- input_text = f"{system_prompt_rewrite} {user_prompt_rewrite} {prompt}"
321
- input_text_2 = f"{system_prompt_rewrite} {user_prompt_rewrite_2} {prompt}"
322
- print("-- got prompt --")
323
- # Encode the input text and include the attention mask
324
- encoded_inputs = tokenizer(input_text, return_tensors="pt", return_attention_mask=True)
325
- encoded_inputs_2 = tokenizer(input_text_2, return_tensors="pt", return_attention_mask=True)
326
- # Ensure all values are on the correct device
327
- input_ids = encoded_inputs["input_ids"].to(device)
328
- input_ids_2 = encoded_inputs_2["input_ids"].to(device)
329
- attention_mask = encoded_inputs["attention_mask"].to(device)
330
- attention_mask_2 = encoded_inputs_2["attention_mask"].to(device)
331
- print("-- tokenize prompt --")
332
- # Google T5
333
- #input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to("cuda")
334
- outputs = model.generate(
335
- input_ids=input_ids,
336
- attention_mask=attention_mask,
337
- max_new_tokens=512,
338
- temperature=0.2,
339
- top_p=0.9,
340
- do_sample=True,
341
- )
342
- outputs_2 = model.generate(
343
- input_ids=input_ids_2,
344
- attention_mask=attention_mask_2,
345
- max_new_tokens=65,
346
- temperature=0.2,
347
- top_p=0.9,
348
- do_sample=True,
349
- )
350
- # Use the encoded tensor 'text_inputs' here
351
- enhanced_prompt = tokenizer.decode(outputs[0], skip_special_tokens=True)
352
- enhanced_prompt_2 = tokenizer.decode(outputs_2[0], skip_special_tokens=True)
353
- print('-- generated prompt --')
354
- enhanced_prompt = filter_text(enhanced_prompt,prompt)
355
- enhanced_prompt_2 = filter_text(enhanced_prompt_2,prompt)
356
- print('-- filtered prompt --')
357
- print(enhanced_prompt)
358
- print('-- filtered prompt 2 --')
359
- print(enhanced_prompt_2)
360
-
361
-
362
 
363
 
364
  print('-- generating image --')
 
232
  f.write(f"Model VAE: sdxl-vae to bfloat safetensor=false before cuda then attn_proc / scale factor 8 \n")
233
  f.write(f"Model UNET: ford442/RealVisXL_V5.0_BF16 \n")
234
  upload_to_ftp(filename)
235
+
236
+
237
+ def captioning(img):
238
+ prompts_array = [
239
+ "Adjectives describing this scene are:",
240
+ "The color scheme of this image is",
241
+ "This scene could be described in detail as",
242
+ "The characters in this scene are",
243
+ "The larger details in this scene include",
244
+ "The smaller details in this scene include",
245
+ "The feeling this scene seems like",
246
+ "The setting of this scene must be located",
247
+ # Add more prompts here
248
+ ]
249
+
250
+ output_prompt=[]
251
+
252
+ # Initial caption generation without a prompt:
253
+ inputsa = processor5(images=img, return_tensors="pt").to('cuda')
254
+ generated_ids = model5.generate(**inputsa, min_length=42, max_length=42)
255
+ generated_text = processor5.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
256
+ print(generated_text)
257
+
258
+ # Loop through prompts array:
259
+ for prompt in prompts_array:
260
+ inputs = processor5(images=img, text=prompt, return_tensors="pt").to('cuda')
261
+ generated_ids = model5.generate(**inputs, min_length=32, max_length=42) # Adjust max_length if needed
262
+ generated_text = processor5.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
263
+ response_text = generated_text.replace(prompt, "").strip() #Or could try .split(prompt, 1)[-1].strip()
264
+ output_prompt.append(response_text)
265
+ print(f"{response_text}\n") # Print only the response text
266
+
267
+ # Continue conversation:
268
+ inputf = processor5(images=img, text=generated_text + 'So therefore', return_tensors="pt").to('cuda')
269
+ generated_ids = model5.generate(**inputf, max_length=42)
270
+ generated_text = processor5.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
271
+ response_text = generated_text.replace(generated_text, "").strip() # Remove the previous text plus 'So therefore'
272
+ print(response_text)
273
+ output_prompt.append(response_text)
274
+ print(output_prompt)
275
+ return output_prompt
276
+
277
+
278
+ def expand_prompt(prompt):
279
+ system_prompt_rewrite = (
280
+ "You are an AI assistant that rewrites image prompts to be more descriptive and detailed."
281
+ )
282
+ user_prompt_rewrite = (
283
+ "Rewrite this prompt to be more descriptive and detailed and only return the rewritten text: "
284
+ )
285
+ user_prompt_rewrite_2 = (
286
+ "Rephrase this scene to have more elaborate details: "
287
+ )
288
+ input_text = f"{system_prompt_rewrite} {user_prompt_rewrite} {prompt}"
289
+ input_text_2 = f"{system_prompt_rewrite} {user_prompt_rewrite_2} {prompt}"
290
+ print("-- got prompt --")
291
+ # Encode the input text and include the attention mask
292
+ encoded_inputs = txt_tokenizer(input_text, return_tensors="pt", return_attention_mask=True)
293
+ encoded_inputs_2 = txt_tokenizer(input_text_2, return_tensors="pt", return_attention_mask=True)
294
+ # Ensure all values are on the correct device
295
+ input_ids = encoded_inputs["input_ids"].to(device)
296
+ input_ids_2 = encoded_inputs_2["input_ids"].to(device)
297
+ attention_mask = encoded_inputs["attention_mask"].to(device)
298
+ attention_mask_2 = encoded_inputs_2["attention_mask"].to(device)
299
+ print("-- tokenize prompt --")
300
+ # Google T5
301
+ #input_ids = txt_tokenizer(input_text, return_tensors="pt").input_ids.to("cuda")
302
+ outputs = model.generate(
303
+ input_ids=input_ids,
304
+ attention_mask=attention_mask,
305
+ max_new_tokens=512,
306
+ temperature=0.2,
307
+ top_p=0.9,
308
+ do_sample=True,
309
+ )
310
+ outputs_2 = model.generate(
311
+ input_ids=input_ids_2,
312
+ attention_mask=attention_mask_2,
313
+ max_new_tokens=65,
314
+ temperature=0.2,
315
+ top_p=0.9,
316
+ do_sample=True,
317
+ )
318
+ # Use the encoded tensor 'text_inputs' here
319
+ enhanced_prompt = txt_tokenizer.decode(outputs[0], skip_special_tokens=True)
320
+ enhanced_prompt_2 = txt_tokenizer.decode(outputs_2[0], skip_special_tokens=True)
321
+ print('-- generated prompt --')
322
+ enhanced_prompt = filter_text(enhanced_prompt,prompt)
323
+ enhanced_prompt_2 = filter_text(enhanced_prompt_2,prompt)
324
+ print('-- filtered prompt --')
325
+ print(enhanced_prompt)
326
+ print('-- filtered prompt 2 --')
327
+ print(enhanced_prompt_2)
328
+ enh_prompt=[enhanced_prompt,enhanced_prompt_2]
329
+ return enh_prompt
330
 
331
  @spaces.GPU(duration=40)
332
  def generate_30(
 
361
  sd_image_a = Image.open(latent_file.name).convert('RGB')
362
  sd_image_a.resize((height,width), Image.LANCZOS)
363
  caption=[]
364
+ caption_2=[]
365
  caption.append(captioner(sd_image_a))
366
  caption.append(captioner_2(sd_image_a))
367
  caption.append(captioner_3(sd_image_a))
368
+ caption_2.append(captioning(sd_image_a))
369
  if latent_file_2 is not None: # Check if a latent file is provided
370
  sd_image_b = Image.open(latent_file_2.name).convert('RGB')
371
  sd_image_b.resize((height,width), Image.LANCZOS)
372
+ caption.append(captioner(sd_image_b))
373
+ caption.append(captioner_2(sd_image_b))
374
+ caption.append(captioner_3(sd_image_b))
375
+ caption_2.append(captioning(sd_image_b))
376
  else:
377
  sd_image_b = None
378
  if latent_file_3 is not None: # Check if a latent file is provided
379
  sd_image_c = Image.open(latent_file_3.name).convert('RGB')
380
  sd_image_c.resize((height,width), Image.LANCZOS)
381
+ caption.append(captioner(sd_image_c))
382
+ caption.append(captioner_2(sd_image_c))
383
+ caption.append(captioner_3(sd_image_c))
384
+ caption_2.append(captioning(sd_image_c))
385
  else:
386
  sd_image_c = None
387
  if latent_file_4 is not None: # Check if a latent file is provided
388
  sd_image_d = Image.open(latent_file_4.name).convert('RGB')
389
  sd_image_d.resize((height,width), Image.LANCZOS)
390
+ caption.append(captioner(sd_image_d))
391
+ caption.append(captioner_2(sd_image_d))
392
+ caption.append(captioner_3(sd_image_d))
393
+ caption_2.append(captioning(sd_image_d))
394
  else:
395
  sd_image_d = None
396
  if latent_file_5 is not None: # Check if a latent file is provided
397
  sd_image_e = Image.open(latent_file_5.name).convert('RGB')
398
  sd_image_e.resize((height,width), Image.LANCZOS)
399
+ caption.append(captioner(sd_image_e))
400
+ caption.append(captioner_2(sd_image_e))
401
+ caption.append(captioner_3(sd_image_e))
402
+ caption_2.append(captioning(sd_image_e))
403
  else:
404
  sd_image_e = None
405
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
 
408
  print(caption)
409
  print("-- generating further caption --")
410
 
411
+ expand_prompt(prompt)
412
+ expand_prompt(caption)
413
+ expand_prompt(caption_2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
414
 
415
 
416
  print('-- generating image --')