Alexander Slessor commited on
Commit
edeeba1
1 Parent(s): b12986e

added error handling

Browse files
Files changed (1) hide show
  1. handler.py +14 -7
handler.py CHANGED
@@ -107,18 +107,25 @@ class EndpointHandler:
107
  self.model = BertForQuestionAnswering.from_pretrained(path).to(device)
108
  self.tokenizer = BertTokenizer.from_pretrained(path)
109
 
110
- def __call__(
111
- self,
112
- data: Dict[str, Any]
113
- ):
114
  """
115
  Args:
116
  data (:obj:):
117
- includes the deserialized image file as PIL.Image
118
  """
119
  try:
120
- question = data.pop("question", data)
121
- context = data.pop("context", data)
 
 
 
 
 
 
 
 
122
 
123
  input_ids = self.tokenizer.encode(question, context)
124
  # print('The input has a total of {:} tokens.'.format(len(input_ids)))
 
107
  self.model = BertForQuestionAnswering.from_pretrained(path).to(device)
108
  self.tokenizer = BertTokenizer.from_pretrained(path)
109
 
110
+ # def __call__(self, data: Dict[str, Any]):
111
+ # def __call__(self, data: dict[str, Any]) -> dict[str, list[Any]]:
112
+ def __call__(self, data: dict[str, Any]):
 
113
  """
114
  Args:
115
  data (:obj:):
116
+ includes the context and question
117
  """
118
  try:
119
+ if 'inputs' not in data:
120
+ raise ValueError('no inputs key in data')
121
+
122
+ i = data.pop("inputs", data)
123
+ question = i.pop("question", False)
124
+ context = i.pop("context", False)
125
+
126
+ if question is False and context is False:
127
+ raise ValueError(
128
+ f'No question and/or context: question: {question} - context: {context}')
129
 
130
  input_ids = self.tokenizer.encode(question, context)
131
  # print('The input has a total of {:} tokens.'.format(len(input_ids)))