generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create and update emnedding generater file
- Loading branch information
1 parent
c1803a5
commit d662228
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from sentence_transformers import SentenceTransformer | ||
from sklearn.metrics.pairwise import cosine_similarity | ||
|
||
# Load the pre-trained model | ||
model = SentenceTransformer('all-MiniLM-L6-v2') | ||
|
||
def generate_embedding(text): | ||
""" | ||
Generate embedding for a given text using the pre-trained model. | ||
""" | ||
return model.encode(text) | ||
|
||
def calculate_similarity(embedding1, embedding2): | ||
""" | ||
Calculate cosine similarity between two embeddings. | ||
""" | ||
return cosine_similarity([embedding1], [embedding2])[0][0] | ||
|
||
if __name__ == "__main__": | ||
# Example texts | ||
text1 = "a UI with red lines on the side showing a spacing issue" | ||
text2 = "an issue with layout spacing in the UI" | ||
|
||
# Generate embeddings | ||
emb1 = generate_embedding(text1) | ||
emb2 = generate_embedding(text2) | ||
|
||
# Calculate similarity | ||
similarity = calculate_similarity(emb1, emb2) | ||
|
||
# Print the similarity score | ||
print(f"Similarity: {similarity:.2f}") |