flow3rdown commited on
Commit
d02d53b
·
1 Parent(s): 9aac1ca
Files changed (1) hide show
  1. app.py +52 -24
app.py CHANGED
@@ -110,38 +110,66 @@ def single_inference_iit(head_img, head_id, tail_img, tail_id, question_txt, que
110
 
111
 
112
  def single_inference_tti(head_txt, head_id, tail_txt, tail_id, question_img, question_id):
113
- # # (T, T) -> (I, ?)
114
- # head_ent_text, tail_ent_text = ent2description[head_id], ent2description[tail_id]
115
 
116
- # inputs = tokenizer(
117
- # tokenizer.sep_token.join([analogy_ent2token[head_id] + " " + head_ent_text, "[R] ", analogy_ent2token[tail_id] + " " + tail_ent_text]),
118
- # tokenizer.sep_token.join([analogy_ent2token[question_id] + " ", "[R] ", "[MASK]"]),
119
- # truncation="longest_first", max_length=128, padding="longest", return_tensors='pt', add_special_tokens=True)
120
- # sep_idx = [[i for i, ids in enumerate(input_ids) if ids == tokenizer.sep_token_id] for input_ids in inputs['input_ids']]
121
- # inputs['sep_idx'] = torch.tensor(sep_idx)
122
- # inputs['attention_mask'] = inputs['attention_mask'].unsqueeze(1).expand([inputs['input_ids'].size(0), inputs['input_ids'].size(1), inputs['input_ids'].size(1)]).clone()
123
- # for i, idx in enumerate(sep_idx):
124
- # inputs['attention_mask'][i, :idx[2], idx[2]:] = 0
125
 
126
- # # image
127
- # pixel_values = processor(images=[head_img, tail_img], return_tensors='pt')['pixel_values'].squeeze()
128
- # inputs['pixel_values'] = pixel_values.unsqueeze(0)
129
 
130
- # input_ids = inputs['input_ids']
131
 
132
- # model_output = mkgformer.model(**inputs, return_dict=True)
133
- # logits = model_output[0].logits
134
- # bsz = input_ids.shape[0]
135
 
136
- # _, mask_idx = (input_ids == tokenizer.mask_token_id).nonzero(as_tuple=True) # bsz
137
- # mask_logits = logits[torch.arange(bsz), mask_idx][:, analogy_entity_ids] # bsz, 1, entity
138
- # answer = ent2text[list(analogy_ent2token.keys())[mask_logits.argmax().item()]]
139
 
140
  return answer
141
 
142
 
143
  def blended_inference_iti(head_img, head_id, tail_txt, tail_id, question_img, question_id):
144
- return tail_txt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
  def single_tab_iit():
147
  with gr.Column():
@@ -192,7 +220,7 @@ def single_tab_tti():
192
  submit_btn = gr.Button("Submit")
193
  output_text = gr.Textbox(label="Output")
194
 
195
- submit_btn.click(fn=single_inference_iit,
196
  inputs=[head_text, head_ent, tail_text, tail_ent, question_image, question_ent],
197
  outputs=[output_text])
198
 
@@ -223,7 +251,7 @@ def blended_tab_iti():
223
  submit_btn = gr.Button("Submit")
224
  output_text = gr.Textbox(label="Output")
225
 
226
- submit_btn.click(fn=single_inference_iit,
227
  inputs=[head_image, head_ent, tail_txt, tail_ent, question_image, question_ent],
228
  outputs=[output_text])
229
 
 
110
 
111
 
112
  def single_inference_tti(head_txt, head_id, tail_txt, tail_id, question_img, question_id):
113
+ # (T, T) -> (I, ?)
114
+ head_ent_text, tail_ent_text = ent2description[head_id], ent2description[tail_id]
115
 
116
+ inputs = tokenizer(
117
+ tokenizer.sep_token.join([analogy_ent2token[head_id] + " " + head_ent_text, "[R] ", analogy_ent2token[tail_id] + " " + tail_ent_text]),
118
+ tokenizer.sep_token.join([analogy_ent2token[question_id] + " ", "[R] ", "[MASK]"]),
119
+ truncation="longest_first", max_length=128, padding="longest", return_tensors='pt', add_special_tokens=True)
120
+ sep_idx = [[i for i, ids in enumerate(input_ids) if ids == tokenizer.sep_token_id] for input_ids in inputs['input_ids']]
121
+ inputs['sep_idx'] = torch.tensor(sep_idx)
122
+ inputs['attention_mask'] = inputs['attention_mask'].unsqueeze(1).expand([inputs['input_ids'].size(0), inputs['input_ids'].size(1), inputs['input_ids'].size(1)]).clone()
123
+ for i, idx in enumerate(sep_idx):
124
+ inputs['attention_mask'][i, :idx[2], idx[2]:] = 0
125
 
126
+ # image
127
+ pixel_values = processor(images=[question_img], return_tensors='pt')['pixel_values'].squeeze()
128
+ inputs['pixel_values'] = pixel_values.unsqueeze(0)
129
 
130
+ input_ids = inputs['input_ids']
131
 
132
+ model_output = mkgformer.model(**inputs, return_dict=True)
133
+ logits = model_output[0].logits
134
+ bsz = input_ids.shape[0]
135
 
136
+ _, mask_idx = (input_ids == tokenizer.mask_token_id).nonzero(as_tuple=True) # bsz
137
+ mask_logits = logits[torch.arange(bsz), mask_idx][:, analogy_entity_ids] # bsz, 1, entity
138
+ answer = ent2text[list(analogy_ent2token.keys())[mask_logits.argmax().item()]]
139
 
140
  return answer
141
 
142
 
143
  def blended_inference_iti(head_img, head_id, tail_txt, tail_id, question_img, question_id):
144
+ # (I, T) -> (I, ?)
145
+ head_ent_text, tail_ent_text = ent2description[head_id], ent2description[tail_id]
146
+
147
+ inputs = tokenizer(
148
+ tokenizer.sep_token.join([analogy_ent2token[head_id], "[R] ", analogy_ent2token[tail_id] + " " + tail_ent_text]),
149
+ tokenizer.sep_token.join([analogy_ent2token[question_id] + " ", "[R] ", "[MASK]"]),
150
+ truncation="longest_first", max_length=128, padding="longest", return_tensors='pt', add_special_tokens=True)
151
+ sep_idx = [[i for i, ids in enumerate(input_ids) if ids == tokenizer.sep_token_id] for input_ids in inputs['input_ids']]
152
+ inputs['sep_idx'] = torch.tensor(sep_idx)
153
+ inputs['attention_mask'] = inputs['attention_mask'].unsqueeze(1).expand([inputs['input_ids'].size(0), inputs['input_ids'].size(1), inputs['input_ids'].size(1)]).clone()
154
+ for i, idx in enumerate(sep_idx):
155
+ inputs['attention_mask'][i, :idx[2], idx[2]:] = 0
156
+
157
+ # image
158
+ pixel_values = processor(images=[head_img, question_img], return_tensors='pt')['pixel_values'].squeeze()
159
+ inputs['pixel_values'] = pixel_values.unsqueeze(0)
160
+
161
+ input_ids = inputs['input_ids']
162
+
163
+ model_output = mkgformer.model(**inputs, return_dict=True)
164
+ logits = model_output[0].logits
165
+ bsz = input_ids.shape[0]
166
+
167
+ _, mask_idx = (input_ids == tokenizer.mask_token_id).nonzero(as_tuple=True) # bsz
168
+ mask_logits = logits[torch.arange(bsz), mask_idx][:, analogy_entity_ids] # bsz, 1, entity
169
+ answer = ent2text[list(analogy_ent2token.keys())[mask_logits.argmax().item()]]
170
+
171
+ return answer
172
+
173
 
174
  def single_tab_iit():
175
  with gr.Column():
 
220
  submit_btn = gr.Button("Submit")
221
  output_text = gr.Textbox(label="Output")
222
 
223
+ submit_btn.click(fn=single_inference_tti,
224
  inputs=[head_text, head_ent, tail_text, tail_ent, question_image, question_ent],
225
  outputs=[output_text])
226
 
 
251
  submit_btn = gr.Button("Submit")
252
  output_text = gr.Textbox(label="Output")
253
 
254
+ submit_btn.click(fn=blended_inference_iti,
255
  inputs=[head_image, head_ent, tail_txt, tail_ent, question_image, question_ent],
256
  outputs=[output_text])
257