Skip to content

Instantly share code, notes, and snippets.

@kristjan-eljand
Last active April 13, 2021 08:13
Show Gist options
  • Save kristjan-eljand/95baa61c5d9d4338d5203e97e7292332 to your computer and use it in GitHub Desktop.
Save kristjan-eljand/95baa61c5d9d4338d5203e97e7292332 to your computer and use it in GitHub Desktop.
Extractive question answering by providing text in Estonina, using pre-trained English models and translating answer back to Estonian.
# 1. create a pipeline for question answering task
respondent = pipeline("question-answering")
# 2. Translate the text from Estonian to English
context_to_translate = r"""
E-Lab on Eesti Energia IT osakonda kuuluv uurimis- ja arendusüksus.
Üksuse eesmärk on kiirendada innovatsiooni ja aidata kaasa uute ideede
esimeste arendusetappide läbimisele. Kui mõnel äriüksusel on soov innovaatilise
IT-lahenduse loomiseks, aitame teostada kontseptsiooni tõestuse ja arendada
välja prototüübi. Lisaks testib ja demonstreerib E-Lab ka uusi digitehnoloogiaid,
et saada paremini aru nende väärtusest ettevõttele.
"""
translated_context = translate(context_to_translate, EST_TO_ENG)[0]['translation_text']
# 3. Translate the question from Estonian to English
question_to_translate = "Millega E-Lab tegeleb?"
translated_question = translate(question_to_translate, EST_TO_ENG)[0]['translation_text']
# 4. Extract and print the results
result = respondent(question=translated_question, context=translated_context)
print(f"Context in English: {translated_context}")
print(f"Question in English: {translated_question}")
print(f"Answer in English: '{result['answer']}', score: {round(result['score'], 4)}")
# 5. Translate the answer back to Estonian
answer_in_estonian = translate(result['answer'], ENG_TO_EST)[0]['translation_text']
print(f"Answer translated back to Estonian: {answer_in_estonian}")
# Output:
# Context in English:
# E-Lab is a research and development unit belonging
# to the IT department of Eesti Energia. The objective of the
# unit is to accelerate innovation and contribute to the first stages
# of development of new ideas. If some business units wish to create
# an innovative IT solution, we will help to carry out the demonstration
# of the concept and develop a prototype. In addition, E-Lab will test and
# demonstrate new digital technologies to better understand their value to the company.
# Question in English: What does E-Lab do?
# Answer in English:
# test and demonstrate new digital technologies
# to better understand their value to the company', score: 0.1491
# Answer translated back to Estonian:
# testida ja näidata uusi digitaaltehnoloogiaid,
# et paremini mõista nende väärtust ettevõttele
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment