1inkusFace commited on
Commit
7c71237
·
verified ·
1 Parent(s): dfe5f15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -25
app.py CHANGED
@@ -247,7 +247,7 @@ def uploadNote(prompt,num_inference_steps,guidance_scale,timestamp):
247
  def captioning(img):
248
  prompts_array = [
249
  # "Adjectives describing this scene are:",
250
- "The color scheme of this image is",
251
  # "This scene could be described in detail as",
252
  # "The characters in this scene are",
253
  "The larger details in this scene include",
@@ -258,15 +258,15 @@ def captioning(img):
258
  ]
259
  output_prompt=[]
260
  cap_prompt = (
261
- "Describe this image with a caption."
262
- )
263
  inputsa = processor5(images=img, text=cap_prompt, return_tensors="pt").to('cuda')
264
  generated_ids = model5.generate(
265
  **inputsa,
266
  do_sample=False,
267
  num_beams=5,
268
- max_length=512,
269
- #min_length=1,
270
  top_p=0.9,
271
  repetition_penalty=1.5,
272
  length_penalty=1.0,
@@ -274,12 +274,12 @@ def captioning(img):
274
  )
275
 
276
  generated_text = processor5.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
 
277
  output_prompt.append(generated_text)
278
  print(generated_text)
279
  # Loop through prompts array:
280
  for prompt in prompts_array:
281
  inputs = processor5(images=img, text=prompt, return_tensors="pt").to('cuda')
282
-
283
  generated_ids = model5.generate(
284
  **inputs,
285
  do_sample=False,
@@ -290,21 +290,23 @@ def captioning(img):
290
  repetition_penalty=1.5,
291
  length_penalty=1.0,
292
  temperature=1,
293
- )
294
-
295
  generated_text = processor5.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
296
  response_text = generated_text.replace(prompt, "").strip() #Or could try .split(prompt, 1)[-1].strip()
297
  output_prompt.append(response_text)
298
  print(f"{response_text}\n") # Print only the response text
299
-
300
- # Continue conversation:
301
- inputf = processor5(images=img, text=generated_text + 'So therefore', return_tensors="pt").to('cuda')
302
- generated_ids = model5.generate(**inputf, max_length=768)
303
- generated_text = processor5.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
304
- response_text = generated_text.replace(generated_text, "").strip()
305
- print(response_text)
 
 
306
  output_prompt.append(response_text)
307
- print(output_prompt)
 
308
  return output_prompt
309
 
310
  def flatten_and_stringify(data):
@@ -437,8 +439,7 @@ def generate_30(
437
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
438
  filename= f'rv_IPb_{timestamp}.png'
439
  print("-- using image file --")
440
- captions = caption.append(flatten_and_stringify(prompt))
441
- captions = flatten_and_stringify(captions)
442
  captions = " ".join(captions)
443
  print(captions)
444
  print("-- not generating further caption --")
@@ -450,7 +451,7 @@ def generate_30(
450
  torch.cuda.empty_cache()
451
  torch.cuda.reset_peak_memory_stats()
452
  #expanded = expand_prompt(captions)
453
- new_prompt = captions #prompt+' '+expanded
454
  print("-- ------------ --")
455
  print("-- FINAL PROMPT --")
456
  print(new_prompt)
@@ -563,8 +564,7 @@ def generate_60(
563
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
564
  filename= f'rv_IPb_{timestamp}.png'
565
  print("-- using image file --")
566
- captions =caption.append(prompt)
567
- captions = flatten_and_stringify(captions)
568
  captions = " ".join(captions)
569
  print(captions)
570
  print("-- not generating further caption --")
@@ -576,7 +576,7 @@ def generate_60(
576
  torch.cuda.empty_cache()
577
  torch.cuda.reset_peak_memory_stats()
578
  #expanded = expand_prompt(captions)
579
- new_prompt = captions #prompt+' '+expanded
580
  print("-- ------------ --")
581
  print("-- FINAL PROMPT --")
582
  print(new_prompt)
@@ -689,8 +689,7 @@ def generate_90(
689
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
690
  filename= f'rv_IPb_{timestamp}.png'
691
  print("-- using image file --")
692
- captions =caption.append(prompt)
693
- captions = flatten_and_stringify(captions)
694
  captions = " ".join(captions)
695
  print(captions)
696
  print("-- not generating further caption --")
@@ -702,7 +701,7 @@ def generate_90(
702
  torch.cuda.empty_cache()
703
  torch.cuda.reset_peak_memory_stats()
704
  #expanded = expand_prompt(captions)
705
- new_prompt = captions #prompt+' '+expanded
706
  print("-- ------------ --")
707
  print("-- FINAL PROMPT --")
708
  print(new_prompt)
 
247
  def captioning(img):
248
  prompts_array = [
249
  # "Adjectives describing this scene are:",
250
+ # "The color scheme of this image is",
251
  # "This scene could be described in detail as",
252
  # "The characters in this scene are",
253
  "The larger details in this scene include",
 
258
  ]
259
  output_prompt=[]
260
  cap_prompt = (
261
+ "Describe this image with a caption to be used for image generation."
262
+ )
263
  inputsa = processor5(images=img, text=cap_prompt, return_tensors="pt").to('cuda')
264
  generated_ids = model5.generate(
265
  **inputsa,
266
  do_sample=False,
267
  num_beams=5,
268
+ max_length=768,
269
+ min_length=64,
270
  top_p=0.9,
271
  repetition_penalty=1.5,
272
  length_penalty=1.0,
 
274
  )
275
 
276
  generated_text = processor5.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
277
+ generated_text = generated_text.replace(cap_prompt, "").strip() #Or could try .split(prompt, 1)[-1].strip()
278
  output_prompt.append(generated_text)
279
  print(generated_text)
280
  # Loop through prompts array:
281
  for prompt in prompts_array:
282
  inputs = processor5(images=img, text=prompt, return_tensors="pt").to('cuda')
 
283
  generated_ids = model5.generate(
284
  **inputs,
285
  do_sample=False,
 
290
  repetition_penalty=1.5,
291
  length_penalty=1.0,
292
  temperature=1,
293
+ )
 
294
  generated_text = processor5.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
295
  response_text = generated_text.replace(prompt, "").strip() #Or could try .split(prompt, 1)[-1].strip()
296
  output_prompt.append(response_text)
297
  print(f"{response_text}\n") # Print only the response text
298
+ inputf = processor5(
299
+ # images=img,
300
+ text=generated_text + 'So therefore',
301
+ return_tensors="pt"
302
+ ).to('cuda')
303
+ generated_ids = model5.generate(**inputf, max_length=256)
304
+ generated_texta = processor5.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
305
+ response_text = generated_texta.replace(generated_text, "").strip()
306
+ #print(response_text)
307
  output_prompt.append(response_text)
308
+ #print(output_prompt)
309
+ output_prompt = " ".join(output_prompt)
310
  return output_prompt
311
 
312
  def flatten_and_stringify(data):
 
439
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
440
  filename= f'rv_IPb_{timestamp}.png'
441
  print("-- using image file --")
442
+ prompt = " ".join(prompt)
 
443
  captions = " ".join(captions)
444
  print(captions)
445
  print("-- not generating further caption --")
 
451
  torch.cuda.empty_cache()
452
  torch.cuda.reset_peak_memory_stats()
453
  #expanded = expand_prompt(captions)
454
+ new_prompt = prompt + ' ' + captions
455
  print("-- ------------ --")
456
  print("-- FINAL PROMPT --")
457
  print(new_prompt)
 
564
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
565
  filename= f'rv_IPb_{timestamp}.png'
566
  print("-- using image file --")
567
+ prompt = " ".join(prompt)
 
568
  captions = " ".join(captions)
569
  print(captions)
570
  print("-- not generating further caption --")
 
576
  torch.cuda.empty_cache()
577
  torch.cuda.reset_peak_memory_stats()
578
  #expanded = expand_prompt(captions)
579
+ new_prompt = prompt + ' ' + captions
580
  print("-- ------------ --")
581
  print("-- FINAL PROMPT --")
582
  print(new_prompt)
 
689
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
690
  filename= f'rv_IPb_{timestamp}.png'
691
  print("-- using image file --")
692
+ prompt = " ".join(prompt)
 
693
  captions = " ".join(captions)
694
  print(captions)
695
  print("-- not generating further caption --")
 
701
  torch.cuda.empty_cache()
702
  torch.cuda.reset_peak_memory_stats()
703
  #expanded = expand_prompt(captions)
704
+ new_prompt = prompt + ' ' + captions
705
  print("-- ------------ --")
706
  print("-- FINAL PROMPT --")
707
  print(new_prompt)