<jupyter_start><jupyter_text>Embedding generator for textGoal: Generating embedding vectors for the content of messages from Jeopardy game data log using transformers model for sentences.  Imports<jupyter_code>from __future__ import division, print_function, absolute_import, unicode_literals

import io
import numpy as np
import matplotlib.pyplot as plt
from google.colab import files
import pandas as pd
import pickle as pk
from time import time
from typing import Text
import tensorflow as tf
pip install -U sentence-transformers
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('roberta-base-nli-mean-tokens')
# model = SentenceTransformer('bert-base-nli-mean-tokens')
# model = SentenceTransformer('bert-large-nli-mean-tokens')<jupyter_output>100%|██████████| 459M/459M [00:06<00:00, 65.8MB/s]<jupyter_text>Helper functions<jupyter_code>class Timer():
    def __init__(self, message: Text = None):
        if message:
            self.message = message
        else:
            self.message = 'It took {elapsed_time:.2f} {unit}.'

    def __enter__(self):
        self.start = time()
        return None

    def __exit__(self, type, value, traceback):
        elapsed_time = time() - self.start
        if elapsed_time < 60:
            unit = 'seconds'
        elif elapsed_time < 3600:
            unit = 'minutes'
            elapsed_time /= 60.0
        else:
            unit = 'hours'
            elapsed_time /= 3600.0
        print(
            self.message.format(elapsed_time=elapsed_time, unit=unit))<jupyter_output><empty_output><jupyter_text>Loading the data<jupyter_code>uploaded = files.upload()
data = pk.load(io.BytesIO(uploaded['Teams_contents.pk']))
len(data)<jupyter_output><empty_output><jupyter_text>Generating embeddings body<jupyter_code>with Timer():
  embeddings = {}
  for team_id, contents in data.items():
    print('Team', team_id, '...')
    embeddings[team_id] = []
    for content in contents:
      emb = np.array(model.encode(content))
      embeddings[team_id].append(emb)
len(embeddings)
with open('content_embeddings_with_roberta_base.pk', 'wb') as handle:
  pk.dump(embeddings, handle, protocol=pk.HIGHEST_PROTOCOL)
!ls
files.download('content_embeddings_with_roberta_base.pk')<jupyter_output><empty_output>