File size: 816 Bytes
bcfecb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import streamlit as st
from sentence_transformers import SentenceTransformer, util

# Load the pre-trained SentenceTransformer model
model = SentenceTransformer('paraphrase-distilroberta-base-v2')

def compute_similarity(para1, para2):
    embedding1 = model.encode(para1, convert_to_tensor=True)
    embedding2 = model.encode(para2, convert_to_tensor=True)
    cosine_sim = util.pytorch_cos_sim(embedding1, embedding2)
    return cosine_sim.item()

st.title("Text Similarity Calculator")

text1 = st.text_area("Enter Text 1")
text2 = st.text_area("Enter Text 2")

if st.button("Calculate Similarity"):
    if text1 and text2:
        similarity_score = compute_similarity(text1, text2)
        st.success(f"Similarity Score: {similarity_score:.4f}")
    else:
        st.warning("Please enter text in both fields.")