hra_qa_bot_v1 / test.py
Mengmeng Liu
initial commit
a5b8812
raw
history blame
2.04 kB
# Load model directly
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
from transformers import Trainer
import torch
tokenizer = AutoTokenizer.from_pretrained("./models/deepset/tinyroberta-squad")
model = AutoModelForQuestionAnswering.from_pretrained("./models/deepset/tinyroberta-squad")
question, text = "Where did robert graduate?", "Robert A. Kauffman is president of Healthcare Risk Advisors (HRA), leading the expansion of the company’s self-insurance and risk transfer solutions for large medical practices, hospitals, and health systems. Rob previously served as senior vice president, secretary, and general counsel of FOJP Service Corporation (“FOJP”) and Hospitals Insurance Company (“HIC”).Rob has built a distinguished career in insurance and risk management. Prior to his roles at FOJP and HIC, he was senior vice president, secretary, general counsel, and chief compliance officer at Harleysville Insurance. He was also a partner at Reed Smith, an international law firm specializing in complex litigation, strategic transactions, and regulatory matters.In addition to his private sector experience, Rob served with distinction as an Assistant U.S. Attorney in the Criminal Division of the United States Attorney’s Office for the Eastern District of Pennsylvania.Mr. Kauffman earned his Bachelor of Arts and Juris Doctor degrees from the University of Pennsylvania."
inputs = tokenizer(question, text, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
answer_start_index = outputs.start_logits.argmax()
answer_end_index = outputs.end_logits.argmax()
predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1]
print(tokenizer.decode(predict_answer_tokens, skip_special_tokens=True))
# target is "nice puppet"
# target_start_index = torch.tensor([14])
# target_end_index = torch.tensor([15])
# outputs = model(**inputs, start_positions=target_start_index, end_positions=target_end_index)
# loss = outputs.loss
# round(loss.item(), 2)