Wendy-Fly commited on
Commit
656cc39
·
verified ·
1 Parent(s): 429af14

Upload generate_prompt.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. generate_prompt.py +26 -26
generate_prompt.py CHANGED
@@ -68,7 +68,7 @@ for batch_idx in tqdm(range(begin, end, batch_size)):
68
  sd_ans = []
69
  # while True:
70
  for idx, i in enumerate(batch):
71
- save_ = [{
72
  "role": "user",
73
  "content": [
74
  {
@@ -79,7 +79,7 @@ for batch_idx in tqdm(range(begin, end, batch_size)):
79
  "text": "Please help me write a prompt for image editing on this picture. The requirements are as follows: complex editing instructions should include two to five simple editing instructions involving spatial relationships (simple editing instructions such as ADD: add an object to the left of a certain object, DELETE: delete a certain object, MODIFY: change a certain object into another object). We hope that the editing instructions can have simple reasoning and can also include some abstract concept-based editing (such as making the atmosphere more romantic, or making the diet healthier, or making the boy more handsome and the girl more beautiful, etc.). Please give me clear editing instructions and also consider whether such editing instructions are reasonable."},
80
  ],
81
  "result":""
82
- }]
83
  #idx_real = batch_idx * batch_size + idx
84
  messages = batch[idx]
85
  save_[0]['content'][0]['image'] = messages['content'][0]['image']
@@ -88,30 +88,30 @@ for batch_idx in tqdm(range(begin, end, batch_size)):
88
  data_list.append(messages)
89
  save_list.append(save_)
90
  #print(len(data_list))
91
- text = processor.apply_chat_template([messages], tokenize=False, add_generation_prompt=True)
92
- #print(len(text))
93
- image_inputs, video_inputs = process_vision_info([messages])
94
- inputs = processor(
95
- text=[text],
96
- images=image_inputs,
97
- videos=video_inputs,
98
- padding=True,
99
- return_tensors="pt",
100
- )
101
- inputs = inputs.to(model.device)
102
-
103
- # Inference: Generation of the output
104
- generated_ids = model.generate(**inputs, max_new_tokens=128)
105
- #print(generated_ids.shape)
106
- generated_ids_trimmed = [
107
- out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
108
- ]
109
- output_text = processor.batch_decode(
110
- generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
111
- )
112
- #print(output_text)
113
- save_[0]['result'] = output_text
114
- save_data.append(save_)
115
 
116
  if batch_idx % 4 ==0:
117
  write_json(json_path,save_data)
 
68
  sd_ans = []
69
  # while True:
70
  for idx, i in enumerate(batch):
71
+ save_ = {
72
  "role": "user",
73
  "content": [
74
  {
 
79
  "text": "Please help me write a prompt for image editing on this picture. The requirements are as follows: complex editing instructions should include two to five simple editing instructions involving spatial relationships (simple editing instructions such as ADD: add an object to the left of a certain object, DELETE: delete a certain object, MODIFY: change a certain object into another object). We hope that the editing instructions can have simple reasoning and can also include some abstract concept-based editing (such as making the atmosphere more romantic, or making the diet healthier, or making the boy more handsome and the girl more beautiful, etc.). Please give me clear editing instructions and also consider whether such editing instructions are reasonable."},
80
  ],
81
  "result":""
82
+ }
83
  #idx_real = batch_idx * batch_size + idx
84
  messages = batch[idx]
85
  save_[0]['content'][0]['image'] = messages['content'][0]['image']
 
88
  data_list.append(messages)
89
  save_list.append(save_)
90
  #print(len(data_list))
91
+ text = processor.apply_chat_template([data_list], tokenize=False, add_generation_prompt=True)
92
+ #print(len(text))
93
+ image_inputs, video_inputs = process_vision_info([data_list])
94
+ inputs = processor(
95
+ text=[text],
96
+ images=image_inputs,
97
+ videos=video_inputs,
98
+ padding=True,
99
+ return_tensors="pt",
100
+ )
101
+ inputs = inputs.to(model.device)
102
+
103
+ # Inference: Generation of the output
104
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
105
+ #print(generated_ids.shape)
106
+ generated_ids_trimmed = [
107
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
108
+ ]
109
+ output_text = processor.batch_decode(
110
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
111
+ )
112
+ #print(output_text)
113
+ save_[0]['result'] = output_text
114
+ save_data.append(save_)
115
 
116
  if batch_idx % 4 ==0:
117
  write_json(json_path,save_data)