Created
December 25, 2020 23:42
-
-
Save sujee/a22f6fa11065791314d5bfede434b516 to your computer and use it in GitHub Desktop.
qa-allen-nlp.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading models... | |
Loaded model 'transformer-qa' in 31,693.4 milli seconds | |
Loaded model 'bidaf-model' in 1,633.8 milli seconds | |
/Users/sujee/opt/anaconda3/envs/teachbot-nlp/lib/python3.7/site-packages/torch/nn/modules/container.py:435: UserWarning: Setting attributes on ParameterList is not supported. | |
warnings.warn("Setting attributes on ParameterList is not supported.") | |
Loaded model 'bidaf-elmo-model' in 13,811.0 milli seconds | |
quesion: Who stars in The Matrix? | |
model transformer-qa predicted in 794.4 milli seconds | |
answer: Keanu Reeves, Laurence Fishburne, Carrie-Anne Moss, Hugo Weaving, and Joe Pantoliano | |
model bidaf-model predicted in 62.1 milli seconds | |
answer: Keanu Reeves, Laurence Fishburne, Carrie-Anne Moss, Hugo Weaving, and Joe Pantoliano | |
model bidaf-elmo-model predicted in 1,798.9 milli seconds | |
answer: Keanu Reeves, Laurence Fishburne, Carrie-Anne Moss, Hugo Weaving, and Joe Pantoliano | |
------------------- | |
quesion: where do polar bear live? | |
model transformer-qa predicted in 2,211.0 milli seconds | |
answer: throughout the Arctic | |
model bidaf-model predicted in 96.4 milli seconds | |
answer: Arctic | |
model bidaf-elmo-model predicted in 7,125.8 milli seconds | |
answer: throughout the Arctic | |
------------------- | |
quesion: how much does a polar bear weigh? | |
model transformer-qa predicted in 2,435.6 milli seconds | |
answer: 330 to 650 pounds | |
model bidaf-model predicted in 98.5 milli seconds | |
answer: 700 to 1,200 pounds | |
model bidaf-elmo-model predicted in 7,082.7 milli seconds | |
answer: 330 to 650 pounds (150 to 294 kilograms); males weigh 700 to 1,200 pounds | |
------------------- | |
quesion: What do polar bears eat? | |
model transformer-qa predicted in 2,142.3 milli seconds | |
answer: seals | |
model bidaf-model predicted in 86.4 milli seconds | |
answer: hunt seals in wide cracks in the sea ice or at breathing holes | |
model bidaf-elmo-model predicted in 6,455.1 milli seconds | |
answer: hunt seals in wide cracks in the sea ice or at breathing holes | |
------------------- | |
quesion: what is lightning? | |
model transformer-qa predicted in 1,361.8 milli seconds | |
answer: a powerful electrical discharge made during a thunderstorm | |
model bidaf-model predicted in 69.1 milli seconds | |
answer: a powerful electrical discharge made during a thunderstorm | |
model bidaf-elmo-model predicted in 3,173.5 milli seconds | |
answer: a powerful electrical discharge | |
------------------- | |
quesion: How many lightning bolts strike earth | |
model transformer-qa predicted in 1,019.7 milli seconds | |
answer: 50 to 100 lightning bolts strike the Earth every second | |
model bidaf-model predicted in 47.0 milli seconds | |
answer: About 50 to 100 | |
model bidaf-elmo-model predicted in 2,885.2 milli seconds | |
answer: About 50 to 100 | |
------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from allennlp.predictors.predictor import Predictor | |
import allennlp_models.rc | |
import time | |
from pprint import pprint | |
nlp_models = [ | |
{ 'name' : 'transformer-qa', | |
'url': 'https://storage.googleapis.com/allennlp-public-models/transformer-qa-2020-10-03.tar.gz' | |
}, | |
{ 'name' : 'bidaf-model', | |
'url' : 'https://storage.googleapis.com/allennlp-public-models/bidaf-model-2020.03.19.tar.gz', | |
}, | |
{ | |
'name' : 'bidaf-elmo-model', | |
'url' : 'https://storage.googleapis.com/allennlp-public-models/bidaf-elmo-model-2020.03.19.tar.gz' | |
}, | |
# { | |
# 'name' : 'naquanet', | |
# 'url' : 'https://storage.googleapis.com/allennlp-public-models/naqanet-2020.02.19.tar.gz' | |
# }, | |
] | |
## load models | |
print ("Loading models...") | |
for nlp_model in nlp_models: | |
# print ("Loading model :", nlp_model['name']) | |
t1 = time.perf_counter() | |
nlp_model['model'] = Predictor.from_path(nlp_model['url']) | |
t2 = time.perf_counter() | |
print ("Loaded model '{}' in {:,.1f} milli seconds".format (nlp_model['name'], (t2-t1)*1e3)) | |
print () | |
def ask_question (passage, question): | |
print ('quesion: ', question) | |
for nlp_model in nlp_models: | |
t1 = time.perf_counter() | |
result = nlp_model['model'].predict(passage=passage, question=question) | |
t2 = time.perf_counter() | |
print (" model {} predicted in {:,.1f} milli seconds".format (nlp_model['name'], (t2-t1)*1e3)) | |
print (' answer: ', result.get('best_span_str', 'unknown')) | |
print() | |
print('-------------------') | |
## --- end: def predict (passage, question): | |
passage = """The Matrix is a 1999 science fiction action film written and directed by The Wachowskis, starring Keanu Reeves, Laurence Fishburne, Carrie-Anne Moss, Hugo Weaving, and Joe Pantoliano.""" | |
question="Who stars in The Matrix?" | |
ask_question (passage, question) | |
## ------------------------------- | |
passage = """ | |
Polar bears live throughout the Arctic, in areas where they can hunt seals in wide cracks in the sea ice or at breathing holes. The taxonomic name for polar bears is Ursus maritimus, which means sea bear, a fitting name for these champion swimmers. They have been known to swim more than 60 miles (about 100 kilometers) without rest in search of food, using their broad front feet for paddling and their back legs like rudders to steer. Unfortunately, due to loss of ice, the bears are now having to swim longer distances, as much as a few hundred miles, which takes a toll on their energy and fat storage. | |
Despite the long, harsh winter, polar bears don’t hibernate. In fact, most of them (except pregnant females) continue to hunt seals throughout the winter. When the weather is extremely cold and hunting is impossible, bears may seek temporary shelter in show dens until conditions improve. | |
Polar bears can grow up to 5.3 feet (1.6 meters) at the shoulder | |
Females weigh 330 to 650 pounds (150 to 294 kilograms); males weigh 700 to 1,200 pounds (317 to 544 kilograms) | |
""" | |
question = "where do polar bear live?" | |
ask_question(passage, question) | |
question = "how much does a polar bear weigh?" | |
ask_question(passage, question) | |
question = "What do polar bears eat?" | |
ask_question(passage, question) | |
## ------------------------------- | |
passage = """ | |
Lightning is a powerful electrical discharge made during a thunderstorm. The electric current is very hot and causes the air around it to expand very quickly, which in turn makes thunder. Sometimes it happens between clouds. Sometimes (in the rain) it goes from cloud to ground. If it goes from cloud to ground, it can strike a person. Around 2000 people are struck by lightning each year. About 50 to 100 lightning bolts strike the Earth every second. Lightning has hit the Empire State Building as many as 500 times a year. | |
""" | |
question = "what is lightning?" | |
ask_question(passage, question) | |
question = "How many lightning bolts strike earth" | |
ask_question(passage, question) | |
## ------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment