textsimilarity / app.py
saurabhg2083's picture
app file
bcfecb7
raw
history blame contribute delete
816 Bytes
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.")