|
import streamlit as st |
|
from transformers import pipeline, AutoModel, AutoTokenizer |
|
|
|
|
|
model_name = "sentence-transformers/all-MiniLM-L6-v2" |
|
model = AutoModel.from_pretrained(model_name) |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
|
|
|
|
st.title("Sentence Similarity App") |
|
|
|
|
|
user_input = st.text_area("Enter a sentence:") |
|
|
|
|
|
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: |
|
|
|
reference_sentence = "Hugging Face is an AI research organization." |
|
user_embedding = calculate_similarity(user_input) |
|
reference_embedding = calculate_similarity(reference_sentence) |
|
|
|
|
|
similarity_score = round(float(user_embedding.dot(reference_embedding.T)), 4) |
|
|
|
|
|
st.write(f"Similarity Score with Reference Sentence: {similarity_score}") |
|
|