File size: 2,936 Bytes
455e58c
 
 
 
4bd1056
455e58c
 
 
 
 
 
 
 
 
 
596896c
 
455e58c
 
 
 
0e5144a
455e58c
d85f21f
455e58c
d85f21f
455e58c
 
 
596896c
 
455e58c
6282954
d85f21f
 
 
 
455e58c
 
 
 
 
 
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
import gradio as gr
import os
from sentence_transformers import SentenceTransformer, util

model = SentenceTransformer('paraphrase-distilroberta-base-v2')

def calculate_embeddings_with_roberta(text):
  embeddings = model.encode(text, convert_to_tensor=True)
  return embeddings

def compare_text(source, target):  
  embeddings_source = model.encode(source, convert_to_tensor=True)
  embeddings_target = model.encode(target, convert_to_tensor=True)
  # Compute cosine-similarities
  cosine_scores = util.cos_sim(embeddings_source, embeddings_target)
  cosine = cosine_scores[0][0].item() 
  return { "Similarity": cosine }, round(cosine,3)

import gradio as gr

with gr.Blocks(css=".gradio-container { background-color: white; background-image: url('file=./qc-logo.png'); background-size: 75px 75px; background-repeat: no-repeat; background-position: 0px 0px; }") as demo:
    gr.Markdown(f"# {' ' * 8}Sentence comparer BERT paraphrase-distilroberta-base-v2")
    with gr.Row():
        source = gr.Textbox(lines=3, label="Source sentence", placeholder="Source sentence")
    with gr.Row():
        target = gr.Textbox(lines=3, label="Target sentence", placeholder="Target sentence")
    btn = gr.Button(value="Compare!", variant="primary")
    with gr.Row():
        label = gr.Label()
        score = gr.Textbox(label="Score", interactive=False)
    btn.click(fn=compare_text, inputs=[source,target], outputs=[label,score])
    gr.Examples([
        ["most advanced conversation intelligence and AI powered coaching platform","most advanced conversation intelligence and AI powered coaching platform"],
        ["Oh, so the quantified platform is one of the most advanced communication intelligence in AI powered coaching systems.","most advanced conversation intelligence and AI powered coaching platform"],
        ["Oh, so the quantified platform is one of the most advanced communication intelligence in AI powered coaching systems. And what does that really mean? So, um, communication coaching is something that is typically delivered one on one between a communication coach who has a, uh, a doctorate or a, um, background and experience in teaching people how to be better communicators and how to express themselves effectively.","most advanced conversation intelligence and AI powered coaching platform"],
        ["And what does that really mean? So, um, communication coaching is something that is typically delivered one on one between a communication coach who has a, uh, a doctorate or a, um, background and experience in teaching people how to be better communicators and how to express themselves effectively.","most advanced conversation intelligence and AI powered coaching platform"],
        ["The new movie is awesome.","most advanced conversation intelligence and AI powered coaching platform"],                        
      ],
      [source, target],
      fn=compare_text
    )

demo.launch(debug=True)