Skip to content

Instantly share code, notes, and snippets.

View kristjan-eljand's full-sized avatar

Kristjan Eljand kristjan-eljand

  • Eesti Energia
View GitHub Profile
@kristjan-eljand
kristjan-eljand / sleeper_fun.py
Last active March 17, 2021 11:58
Sleeper function that sleeps for 1 sec and then returns its argument
import time #to measure the execution time
def sleeper_function(x):
time.sleep(1)
return x
# Execute the function 4 times and measure the execution time
start = time.time()
results = [sleeper_function(x) for x in range(4)]
print("duration =", time.time() - start)
@kristjan-eljand
kristjan-eljand / import_distilgpt2.py
Last active March 22, 2021 13:22
Import distilgpt2 model and propose sentence beginnings
from transformers import pipeline
# Create text generator and import distilgpt2 model
generator = pipeline('text-generation', model='distilgpt2')
# 4 different Beginnings that I use for text generation
sentence_beginnings = ["In Estonia, our main source of energy is",
"The main task of the technology scout is",
"Renewable energy is important because",
"Electric Vehicles are changing the"]
@kristjan-eljand
kristjan-eljand / single_core_gpt2.py
Last active March 22, 2021 13:38
model inference on single core
# Function that carries out model inference and return generated text
def continue_text(beginning):
text = generator(beginning, max_length=30, num_return_sequences=1)
return text[0]['generated_text']
start = time.time()
# Generate text for all beginnings
results = [continue_text(beginning) for beginning in sentence_beginnings]
print("duration =", time.time() - start)
print(results)
@kristjan-eljand
kristjan-eljand / gpt2_with_ray.py
Created March 22, 2021 14:01
GPT2 inference with Ray on 2 cores
ray.init(num_cpus=2, ignore_reinit_error=True)
# Create remote function
@ray.remote
def continue_text(beginning):
text = generator(beginning, max_length=30, num_return_sequences=1)
return text[0]['generated_text']
start = time.time()
# Send continue_text() to multiple cores and gather the results
@kristjan-eljand
kristjan-eljand / count_per_second.py
Created March 23, 2021 11:33
Counter per scond
import time
# Function that runs the counter for 1 second
def count_per_second(x):
start = time.time()
counter = 0
while time.time() - start < 1:
counter += 1
return counter
@kristjan-eljand
kristjan-eljand / multicore_count_per_second.py
Created March 23, 2021 11:37
Counter per second with ray
import ray
# Start ray cluster with 3 cpu's
ray.init(num_cpus=3)
# @ray.remote decorator enables to use this
# function in distributed setting
@ray.remote
def count_per_second(x):
start = time.time()
counter = 0
@kristjan-eljand
kristjan-eljand / huggingface_translation.py
Last active April 12, 2021 11:43
Translate from Estonian to English by using Huggingface's Inference API
import json
import requests
# We use "Helsinki-NLP/opus-mt-et-en" model for translation
API_URL = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-et-en"
# Register an account in Hugging Face to get your API_TOKEN
# you'll find it under settings
headers = {"Authorization": f"Bearer {API_TOKEN}"}
# Function to run the post request to the API
@kristjan-eljand
kristjan-eljand / est_to_eng_sentiment_analysis.py
Last active April 13, 2021 08:18
Sentiment analysis from Estonian to English using Huggingface transformers
# 1. Create sentiment classifier with pipeline function
# and the name of the task
classifier = pipeline('sentiment-analysis')
# 2. Translate the text from input language to English
input_to_translate = "Tahtsime parimat, aga välja kukkus nagu alati"
translated_input = translate(input_to_translate)[0]['translation_text']
# 3. Using the sentiment classifier is oneliner
result = classifier(translated_input)[0]
@kristjan-eljand
kristjan-eljand / est_to_eng_sentiment_analysis_failure.py
Last active April 13, 2021 08:18
est_to_eng sentiment analysis failed translation example
# Translate the text from input language to english
input_to_translate = "Parim argument demokraatia vastu on viieminutiline vestlus keskmise valijaga"
translated_input = translate(input_to_translate)[0]['translation_text']
# Using the sentiment classifier is oneliner
result = classifier(translated_input)[0]
print(translated_input)
print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
# Output:
@kristjan-eljand
kristjan-eljand / est_to_eng_question_answering.py
Last active April 13, 2021 08:13
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,