Upload 2 files
Browse files- app.py +31 -0
- requirements (1).txt +3 -0
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline, AutoModel, AutoTokenizer
|
3 |
+
|
4 |
+
# Load the model and tokenizer
|
5 |
+
model_name = "sentence-transformers/all-MiniLM-L6-v2"
|
6 |
+
model = AutoModel.from_pretrained(model_name)
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# Create a Streamlit app
|
10 |
+
st.title("Sentence Similarity App")
|
11 |
+
|
12 |
+
# Ask the user for input
|
13 |
+
user_input = st.text_area("Enter a sentence:")
|
14 |
+
|
15 |
+
# Define a function to calculate sentence similarity
|
16 |
+
def calculate_similarity(input_text):
|
17 |
+
similarity_pipeline = pipeline("feature-extraction", model=model, tokenizer=tokenizer)
|
18 |
+
user_embedding = similarity_pipeline(input_text)[0]
|
19 |
+
return user_embedding
|
20 |
+
|
21 |
+
if user_input:
|
22 |
+
# Calculate similarity with a reference sentence
|
23 |
+
reference_sentence = "Hugging Face is an AI research organization."
|
24 |
+
user_embedding = calculate_similarity(user_input)
|
25 |
+
reference_embedding = calculate_similarity(reference_sentence)
|
26 |
+
|
27 |
+
# Calculate cosine similarity
|
28 |
+
similarity_score = round(float(user_embedding.dot(reference_embedding.T)), 4)
|
29 |
+
|
30 |
+
# Display the similarity score
|
31 |
+
st.write(f"Similarity Score with Reference Sentence: {similarity_score}")
|
requirements (1).txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
altair<5
|