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}")