File size: 1,607 Bytes
6ae27e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from sentence_transformers import SentenceTransformer
import pandas as pd
import jax.numpy as jnp

from typing import List
import config

# We download the models we will be using.
# If you do not want to use all, you can comment the unused ones.
distilroberta_model = SentenceTransformer(config.MODELS_ID['distilroberta'])
mpnet_model = SentenceTransformer(config.MODELS_ID['mpnet'])
minilm_l6_model = SentenceTransformer(config.MODELS_ID['minilm_l6'])

# Defining cosine similarity using flax.
def cos_sim(a, b):
    return jnp.matmul(a, jnp.transpose(b))/(jnp.linalg.norm(a)*jnp.linalg.norm(b))


# We get similarity between embeddings.
def text_similarity(anchor: str, inputs: List[str], model: str = 'distilroberta'):

    # Creating embeddings
    if model == 'distilroberta':
        anchor_emb = distilroberta_model.encode(anchor)[None, :]
        inputs_emb = distilroberta_model.encode([input for input in inputs])
    elif model == 'mpnet':
        anchor_emb = mpnet_model.encode(anchor)[None, :]
        inputs_emb = mpnet_model.encode([input for input in inputs])
    elif model == 'minilm_l6':
        anchor_emb = minilm_l6_model.encode(anchor)[None, :]
        inputs_emb = minilm_l6_model.encode([input for input in inputs])

    # Obtaining similarity
    similarity = list(jnp.squeeze(cos_sim(anchor_emb, inputs_emb)))

    # Returning a Pandas' dataframe
    d = {'inputs': [input for input in inputs],
         'score': [round(similarity[i],3) for i in range(len(similarity))]}
    df = pd.DataFrame(d, columns=['inputs', 'score'])

    return df.sort_values('score', ascending=False)