This file contains 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
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) |
This file contains 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 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"] |
This file contains 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
# 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) |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
# 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] |
This file contains 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
# 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: |
This file contains 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
# 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, |
OlderNewer