Skip to content

Instantly share code, notes, and snippets.

@ximnet-arshadalwi
Last active Nov 17, 2021
Embed
What would you like to do?
from farm.infer import QAInferencer
def main():
# (1) Define the model, context and question.
model_name = "deepset/electra-base-squad2"
context = r"""
Malaysia has its origins in the Malay kingdoms which, from the 18th century, became subject to the British Empire, along with the
British Straits Settlements protectorate. Peninsular Malaysia was unified as the Malayan Union in 1946. Malaya was restructured as
the Federation of Malaya in 1948 and achieved independence on 31 August 1957. The independent Malaya united with the then British
crown colonies of North Borneo, Sarawak, and Singapore on 16 September 1963 to become Malaysia. In August 1965, Singapore was expelled
from the federation and became a separate independent country.
"""
question = "What was the Malayan Union formed?"
# (2) Make predictions
nlp = QAInferencer.load(model_name, task_type='question_answering')
QA_input = [{"questions": [question],
"text": context}]
res = nlp.inference_from_dicts(dicts=QA_input)[0]
# (3) Output the first predicted answer on-screen
print('\n\nQuestion: ' + question + '\nAnswer: ' + res['predictions'][0]['answers'][0]['answer'])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment