File size: 1,162 Bytes
f6670ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import pipeline, AutoModel, AutoTokenizer

# Load the model and tokenizer
model_name = "sentence-transformers/all-MiniLM-L6-v2"
model = AutoModel.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Create a Streamlit app
st.title("Sentence Similarity App")

# Ask the user for input
user_input = st.text_area("Enter a sentence:")

# Define a function to calculate sentence similarity
def calculate_similarity(input_text):
    similarity_pipeline = pipeline("feature-extraction", model=model, tokenizer=tokenizer)
    user_embedding = similarity_pipeline(input_text)[0]
    return user_embedding

if user_input:
    # Calculate similarity with a reference sentence
    reference_sentence = "Hugging Face is an AI research organization."
    user_embedding = calculate_similarity(user_input)
    reference_embedding = calculate_similarity(reference_sentence)

    # Calculate cosine similarity
    similarity_score = round(float(user_embedding.dot(reference_embedding.T)), 4)

    # Display the similarity score
    st.write(f"Similarity Score with Reference Sentence: {similarity_score}")