File size: 1,795 Bytes
0f35f83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
import streamlit as st
from embedding_models.registry import registry as embedding
from similarity_models.registry import registry as similarity
import pandas as pd


def calculate_similarity(text_1, text_2):
    # TODO: pick any N random embedding models
    similarity_scores = []

    # TODO: pick any random similarity model
    similarity_model = similarity.models()["cosine"]
    for name, model in embedding.models().items():
        embedding_1 = model.embed(text_1)
        embedding_2 = model.embed(text_2)

        similarity_scores.append((name, similarity_model.score(embedding_1, embedding_2)))
    return similarity_scores


class BattlegroundTab:

    def __init__(self):
        pass

    def ui(self):
        st.header("Battleground")
        st.write("Battle embedding models with each other! May the best win!")

        col1, col2 = st.columns(2)
        with col1:
            text_1 = st.text_input("Enter first text here!")

        with col2:
            text_2 = st.text_input("Enter second text here!")

        expected_sc = st.slider(
            'How similar do feel these words are',
            min_value=1, max_value=10, step=1, value=5) / 10
        st.write('Expected Similarity Score = ', expected_sc)

        if st.button("Calculate Similarity Score"):
            similarity_scores = calculate_similarity(text_1, text_2)
            df = pd.DataFrame(similarity_scores, columns=['Model', 'Score'])
            df['Loss'] = abs(df['Score'] - expected_sc)
            winner_model = df.loc[df['Loss'].idxmin(), 'Model']
            df['Winner'] = ''
            df.loc[df['Model'] == winner_model, 'Winner'] = '👑'
            df = df.drop(columns=['Loss'])
            markdown_table = df.to_markdown(index=False)
            st.markdown(markdown_table)