Skip to content
This repository was archived by the owner on Oct 31, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion dynalab/tasks/qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
"context": " ".join([str(x) + "_" for x in range(513)]),
"question": "Can you handle this length?",
},
{
"uid": str(uuid.uuid4()),
"context": "The sum of 3 and 2 is 5",
"question": "What is the total?",
"answer": "5",
},
{
"uid": str(uuid.uuid4()),
"context": "The answer to this question is that everyone is doing great today",
"question": "What is the answer?",
"answer": "great today",
},
]


Expand All @@ -49,12 +61,16 @@ def verify_response(self, response, data):
assert "answer" in response and response["answer"] in data["context"]
assert response["signed"] == self.generate_response_signature(response, data)
Nk = 3
if "eval_exact" in response:
Nk += 1
if "eval_f1" in response:
Nk += 1
if "conf" in response:
assert (
response["conf"] >= 0 and response["conf"] <= 1
), "Confidence score should be between 0 and 1"
Nk += 1
assert Nk == len(response), f"response should not contain other extra keys"
assert Nk >= len(response), f"response should not contain other extra keys"

def parse_signature_input(self, response, data):
task = "qa"
Expand Down
11 changes: 9 additions & 2 deletions examples/electra_style_qa/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import torch
from transformers import (AutoConfig, AutoTokenizer, AutoModelForQuestionAnswering, QuestionAnsweringPipeline)
from transformers.data.metrics.squad_metrics import compute_exact, compute_f1

from dynalab.handler.base_handler import BaseDynaHandler
from dynalab.tasks.qa import TaskIO
Expand Down Expand Up @@ -32,8 +33,8 @@ def preprocess(self, data):
"""
example = self._read_data(data)
return {
'context': example['context'],
'question': example['question']
'context': example['context'].strip(),
'question': example['question'].strip()
}

def inference(self, input_data):
Expand All @@ -56,6 +57,12 @@ def postprocess(self, inference_output, data):
response["id"] = example["uid"]
response["answer"] = answer
response["conf"] = conf

if "answer" in example:
human_answer = str(example["answer"]).strip()
response["eval_f1"] = compute_f1(human_answer, answer)
response["eval_exact"] = compute_exact(human_answer, answer)

response = self.taskIO.sign_response(response, example)
return [response]

Expand Down