|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import torch |
|
|
import torch.nn.functional as F |
|
|
from transformers import ( |
|
|
AutoModelForCausalLM, |
|
|
Pipeline, |
|
|
TFAutoModelForCausalLM, |
|
|
) |
|
|
from transformers.pipelines import PIPELINE_REGISTRY |
|
|
|
|
|
|
|
|
class QBTossupPipeline(Pipeline): |
|
|
def _sanitize_parameters(self, **kwargs): |
|
|
|
|
|
return {}, {}, {} |
|
|
|
|
|
def preprocess(self, inputs): |
|
|
prompt = ( |
|
|
"Answer the quiz question revealed so far:\n" |
|
|
f"Question: {inputs['question_text']}\nAnswer:" |
|
|
) |
|
|
return self.tokenizer(prompt, return_tensors="pt", truncation=True) |
|
|
|
|
|
def _forward(self, model_inputs): |
|
|
with torch.no_grad(): |
|
|
return self.model(**model_inputs) |
|
|
|
|
|
def postprocess(self, model_outputs): |
|
|
logits = model_outputs.logits |
|
|
last_logits = logits[0, -1] |
|
|
probs = F.softmax(last_logits, dim=-1) |
|
|
top_id = torch.argmax(probs) |
|
|
answer = self.tokenizer.decode(top_id, skip_special_tokens=True).strip() |
|
|
confidence = probs[top_id].item() |
|
|
buzz = confidence > 0.5 |
|
|
return {"answer": answer, "confidence": confidence, "buzz": buzz} |
|
|
|
|
|
|
|
|
PIPELINE_REGISTRY.register_pipeline( |
|
|
"quizbowl-tossup", |
|
|
pipeline_class=QBTossupPipeline, |
|
|
pt_model=AutoModelForCausalLM, |
|
|
tf_model=TFAutoModelForCausalLM, |
|
|
default={ |
|
|
"pt": ("yujiepan/llama-3-tiny-random", "main"), |
|
|
}, |
|
|
type="text", |
|
|
) |
|
|
|