sergiomar73's picture
Update app.py
0e5144a
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)