Spaces:
Runtime error
Runtime error
File size: 16,463 Bytes
c388795 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# coding=utf8
from transformers import AutoModel, AutoTokenizer, AutoConfig
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import streamlit as st
import gdown
import numpy as np
import pandas as pd
import collections
from string import punctuation
class CONFIG:
#model params
model = 'deepset/xlm-roberta-large-squad2'
max_input_length = 384 #Hyperparameter to be tuned, following the guide from huggingface
doc_stride = 128 #Hyperparameter to be tuned, following the guide from huggingface
model_checkpoint = "pytorch_model.pth"
trained_model_url = 'https://drive.google.com/uc?id=16Vp918RglyLEFEyDlFuRD1HeNZ8SI7P5'
trained_model_output_fp = 'trained_pytorch.pth'
sample_df_fp = "sample_qa.json"
# model class
class ChaiModel(nn.Module):
def __init__(self, model_config):
super(ChaiModel, self).__init__()
self.backbone = AutoModel.from_pretrained(CONFIG.model)
self.linear = nn.Linear(model_config.hidden_size, 2)
def forward(self, input_ids, attention_mask):
model_output = self.backbone(input_ids, attention_mask=attention_mask)
sequence_output = model_output[0] # (batchsize, sequencelength, hidden_dim)
qa_logits = self.linear(sequence_output) # (batchsize, sequencelength, 2)
start_logit, end_logit = qa_logits.split(1, dim=-1) # (batchsize, sequencelength), 1), (batchsize, sequencelength, 1)
start_logits = start_logit.squeeze(-1) # remove last dim (batchsize, sequencelength)
end_logits = end_logit.squeeze(-1) #remove last dim (batchsize, sequencelength)
return start_logits, end_logits # (2,batchsize, sequencelength)
# dataset class
class ChaiDataset(Dataset):
def __init__(self, dataset, is_train=True):
super(ChaiDataset, self).__init__()
self.dataset = dataset #list of features
self.is_train= is_train
def __len__(self):
return len(self.dataset)
def __getitem__(self, index):
features = self.dataset[index]
if self.is_train:
return {
'input_ids': torch.tensor(features['input_ids'], dtype=torch.long),
'attention_mask': torch.tensor(features['attention_mask'], dtype=torch.long),
'offset_mapping':torch.tensor(features['offset_mapping'], dtype=torch.long),
'start_position':torch.tensor(features['start_position'], dtype=torch.long),
'end_position':torch.tensor(features['end_position'], dtype=torch.long)
}
else:
return {
'input_ids': torch.tensor(features['input_ids'], dtype=torch.long),
'attention_mask': torch.tensor(features['attention_mask'], dtype=torch.long),
'offset_mapping':torch.tensor(features['offset_mapping'], dtype=torch.long),
'sequence_ids':features['sequence_ids'],
'id':features['example_id'],
'context':features['context'],
'question':features['question']
}
def break_long_context(df, tokenizer, train=True):
if train:
n_examples = len(df)
full_set = []
for i in range(n_examples):
row = df.iloc[i]
# tokenizer parameters can be found here
# https://huggingface.co/transformers/internal/tokenization_utils.html#transformers.tokenization_utils_base.PreTrainedTokenizerBase
tokenized_examples = tokenizer(row['question'],
row['context'],
padding='max_length',
max_length=CONFIG.max_input_length,
truncation='only_second',
stride=CONFIG.doc_stride,
return_overflowing_tokens=True, #returns the number of over flow
return_offsets_mapping=True #returns the BPE mapping to the original word
)
# tokenized_example keys
#'input_ids', 'attention_mask', 'offset_mapping', 'overflow_to_sample_mapping'
sample_mappings = tokenized_examples.pop("overflow_to_sample_mapping")
offset_mappings = tokenized_examples.pop("offset_mapping")
final_examples = []
n_sub_examples = len(sample_mappings)
for j in range(n_sub_examples):
input_ids = tokenized_examples["input_ids"][j]
attention_mask = tokenized_examples["attention_mask"][j]
sliced_text = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids))
final_example = dict(input_ids = input_ids,
attention_mask = attention_mask,
sliced_text = sliced_text,
offset_mapping=offset_mappings[j],
fold=row['fold'])
# Most of the time cls_index is 0
cls_index = input_ids.index(tokenizer.cls_token_id)
# None, 0, 0, .... None, None, 1, 1,.....
sequence_ids = tokenized_examples.sequence_ids(j)
sample_index = sample_mappings[j]
offset_map = offset_mappings[j]
if np.isnan(row["answer_start"]) : # if no answer, start and end position is cls_index
final_example['start_position'] = cls_index
final_example['end_position'] = cls_index
final_example['tokenized_answer'] = ""
final_example['answer_text'] = ""
else:
start_char = row["answer_start"]
end_char = start_char + len(row["answer_text"])
token_start_index = sequence_ids.index(1)
token_end_index = len(sequence_ids)- 1 - (sequence_ids[::-1].index(1))
if not (offset_map[token_start_index][0]<=start_char and offset_map[token_end_index][1] >= end_char):
final_example['start_position'] = cls_index
final_example['end_position'] = cls_index
final_example['tokenized_answer'] = ""
final_example['answer_text'] = ""
else:
#Move token_start_index to the correct context index
while token_start_index < len(offset_map) and offset_map[token_start_index][0] <= start_char:
token_start_index +=1
final_example['start_position'] = token_start_index -1
while offset_map[token_end_index][1] >= end_char: #Take note that we will want the end_index inclusively, we will need to slice properly later
token_end_index -=1
final_example['end_position'] = token_end_index + 1
tokenized_answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[final_example['start_position']:final_example['end_position']+1]))
final_example['tokenized_answer'] = tokenized_answer
final_example['answer_text'] = row['answer_text']
final_examples.append(final_example)
full_set += final_examples
else:
n_examples = len(df)
full_set = []
for i in range(n_examples):
row = df.iloc[i]
tokenized_examples = tokenizer(row['question'],
row['context'],
padding='max_length',
max_length=CONFIG.max_input_length,
truncation='only_second',
stride=CONFIG.doc_stride,
return_overflowing_tokens=True, #returns the number of over flow
return_offsets_mapping=True #returns the BPE mapping to the original word
)
sample_mappings = tokenized_examples.pop("overflow_to_sample_mapping")
offset_mappings = tokenized_examples.pop("offset_mapping")
n_sub_examples = len(sample_mappings)
final_examples = []
for j in range(n_sub_examples):
input_ids = tokenized_examples["input_ids"][j]
attention_mask = tokenized_examples["attention_mask"][j]
final_example = dict(
input_ids = input_ids,
attention_mask = attention_mask,
offset_mapping=offset_mappings[j],
example_id = row['id'],
context = row['context'],
question = row['question'],
sequence_ids = [0 if value is None else value for value in tokenized_examples.sequence_ids(j)]
)
final_examples.append(final_example)
full_set += final_examples
return full_set
def postprocess_qa_predictions(examples, features, raw_predictions, n_best_size = 20, max_answer_length = 30):
all_start_logits, all_end_logits = raw_predictions
example_id_to_index = {k: i for i, k in enumerate(examples["id"])}
features_per_example = collections.defaultdict(list)
for i, feature in enumerate(features):
features_per_example[example_id_to_index[feature["example_id"]]].append(i)
predictions = collections.OrderedDict()
print(f"Post-processing {len(examples)} example predictions split into {len(features)} features.")
for example_index, example in examples.iterrows():
feature_indices = features_per_example[example_index]
min_null_score = None
valid_answers = []
context = example["context"]
for feature_index in feature_indices:
start_logits = all_start_logits[feature_index]
end_logits = all_end_logits[feature_index]
sequence_ids = features[feature_index]["sequence_ids"]
context_index = 1
features[feature_index]["offset_mapping"] = [
(o if sequence_ids[k] == context_index else None)
for k, o in enumerate(features[feature_index]["offset_mapping"])
]
offset_mapping = features[feature_index]["offset_mapping"]
cls_index = features[feature_index]["input_ids"].index(tokenizer.cls_token_id)
feature_null_score = start_logits[cls_index] + end_logits[cls_index]
if min_null_score is None or min_null_score < feature_null_score:
min_null_score = feature_null_score
start_indexes = np.argsort(start_logits)[-1 : -n_best_size - 1 : -1].tolist()
end_indexes = np.argsort(end_logits)[-1 : -n_best_size - 1 : -1].tolist()
for start_index in start_indexes:
for end_index in end_indexes:
if (
start_index >= len(offset_mapping)
or end_index >= len(offset_mapping)
or offset_mapping[start_index] is None
or offset_mapping[end_index] is None
):
continue
# Don't consider answers with a length that is either < 0 or > max_answer_length.
if end_index < start_index or end_index - start_index + 1 > max_answer_length:
continue
start_char = offset_mapping[start_index][0]
end_char = offset_mapping[end_index][1]
valid_answers.append(
{
"score": start_logits[start_index] + end_logits[end_index],
"text": context[start_char: end_char]
}
)
if len(valid_answers) > 0:
best_answer = sorted(valid_answers, key=lambda x: x["score"], reverse=True)[0]
else:
best_answer = {"text": "", "score": 0.0}
predictions[example["id"]] = best_answer["text"]
return predictions
def download_finetuned_model():
gdown.download(url=CONFIG.trained_model_url, output=CONFIG.trained_model_output_fp, quiet=False)
def get_prediction(context:str, question:str, model, tokenizer) -> str:
# convert to dataframe format to make it consistent with training way
test_df = pd.DataFrame({"id":[1], "context":[context.strip()], "question":[question.strip()]})
test_set = break_long_context(test_df, tokenizer, train=False)
#create dataset and dataloader of batch 1 to prevent OOM
test_dataset = ChaiDataset(test_set, is_train=False)
test_dataloader = DataLoader(test_dataset,
batch_size=1,
shuffle=False,
drop_last=False
)
#main prediction function
start_logits =[]
end_logits=[]
for features in test_dataloader:
input_ids = features['input_ids']
attention_mask = features['attention_mask']
with torch.no_grad():
start_logit, end_logit = model(input_ids, attention_mask) #(batch, 384,1) , (batch, 384,1)
start_logits.append(start_logit.to("cpu").numpy())
end_logits.append(end_logit.to("cpu").numpy())
start_logits, end_logits = np.vstack(start_logits), np.vstack(end_logits)
predictions = postprocess_qa_predictions(test_df, test_set, (start_logits, end_logits))
predictions = list(predictions.items())[0][1]
predictions = predictions.strip(punctuation)
return predictions
@st.cache(allow_output_mutation=True)
def load_model():
gdown.download(url=CONFIG.trained_model_url, output=CONFIG.trained_model_output_fp, quiet=False)
print("Downloaded pretrained model")
config = AutoConfig.from_pretrained(CONFIG.model)
model = ChaiModel(config)
model.load_state_dict(torch.load(CONFIG.trained_model_output_fp, map_location=torch.device('cpu')))
model.eval()
tokenizer = AutoTokenizer.from_pretrained(CONFIG.model)
sample_df = pd.read_json(CONFIG.sample_df_fp)
return model, tokenizer, sample_df
model, tokenizer, sample_df = load_model()
## initialize session_state
if "context" not in st.session_state:
st.session_state["context"] = ""
if "question" not in st.session_state:
st.session_state['question'] = ""
if "answer" not in st.session_state:
st.session_state['answer'] = ""
## Layout
st.sidebar.title("Hindi/Tamil Extractive Question Answering")
st.sidebar.markdown("---")
random_button = st.sidebar.button("Random")
st.sidebar.write("Randomly Generates a Hindi/Tamil Context and Question")
st.sidebar.markdown("---")
answer_button = st.sidebar.button("Answer!")
if random_button:
sample = sample_df.sample(1)
st.session_state['context'] = sample['context'].item()
st.session_state['question'] = sample['question'].item()
st.session_state['answer'] = ""
if answer_button:
# if question or context is empty text
if len(st.session_state['context']) == 0 or len(st.session_state['question']) ==0:
st.session_state['answer'] = " "
else:
st.session_state['answer'] = get_prediction(st.session_state['context'], st.session_state['question'], model, tokenizer)
st.session_state["context"] = st.text_area("Context", value=st.session_state['context'], height=300)
with st.container():
col_1, col_2 = st.columns(2)
with col_1:
st.session_state['question'] = st.text_area("Question", value=st.session_state['question'], height=200)
with col_2:
st.text_area("Answer", value=st.session_state['answer'], height=200)
|