Spaces:
Sleeping
Sleeping
Sharathhebbar24
commited on
Commit
•
30933bd
1
Parent(s):
9a8697c
Upload 2 files
Browse files- app.py +77 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModel
|
4 |
+
from sentence_transformers import util
|
5 |
+
class SentenceSimiliarity():
|
6 |
+
|
7 |
+
def __init__(self, sentence1, sentence2):
|
8 |
+
self.sentence1 = sentence1
|
9 |
+
self.sentence2 = sentence2
|
10 |
+
self.model_name = "bert-base-uncased"
|
11 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
|
12 |
+
self.model = AutoModel.from_pretrained(self.model_name)
|
13 |
+
|
14 |
+
def tokenize(self):
|
15 |
+
tokenized1 = self.tokenizer(
|
16 |
+
self.sentence1,
|
17 |
+
return_tensors='pt',
|
18 |
+
padding=True,
|
19 |
+
truncation=True
|
20 |
+
)
|
21 |
+
tokenized2 = self.tokenizer(
|
22 |
+
self.sentence2,
|
23 |
+
return_tensors='pt',
|
24 |
+
padding=True,
|
25 |
+
truncation=True
|
26 |
+
)
|
27 |
+
return tokenized1, tokenized2
|
28 |
+
|
29 |
+
def get_embeddings(self):
|
30 |
+
tokenized1, tokenized2 = self.tokenize()
|
31 |
+
with torch.no_grad():
|
32 |
+
embeddings1 = self.model(**tokenized1).last_hidden_state.mean(dim=1)
|
33 |
+
embeddings2 = self.model(**tokenized2).last_hidden_state.mean(dim=1)
|
34 |
+
return embeddings1, embeddings2
|
35 |
+
|
36 |
+
def get_similarity_scores(self):
|
37 |
+
embeddings1, embeddings2 = self.get_embeddings()
|
38 |
+
scores = util.cos_sim(embeddings1, embeddings2)
|
39 |
+
return scores
|
40 |
+
|
41 |
+
|
42 |
+
def results(self):
|
43 |
+
scores = self.get_similarity_scores()
|
44 |
+
statement = f"The sentence has {scores.item() * 100:.2f}% similarity"
|
45 |
+
return statement
|
46 |
+
|
47 |
+
|
48 |
+
class UI():
|
49 |
+
|
50 |
+
def __init__(self):
|
51 |
+
st.title("Sentence Similiarity Checker")
|
52 |
+
st.caption("You can use this for checking similarity between resume and job description")
|
53 |
+
|
54 |
+
def get(self):
|
55 |
+
self.sentence1 = st.text_area(
|
56 |
+
label="Sentence 1",
|
57 |
+
help="This is a parent text the next text will be compared with this text"
|
58 |
+
)
|
59 |
+
self.sentence2 = st.text_area(
|
60 |
+
label="Sentence 2",
|
61 |
+
help="This is a child text"
|
62 |
+
)
|
63 |
+
self.button = st.button(
|
64 |
+
label="Check",
|
65 |
+
help='Check Sentence Similarity'
|
66 |
+
)
|
67 |
+
|
68 |
+
def result(self):
|
69 |
+
self.get()
|
70 |
+
ss = SentenceSimiliarity(self.sentence1, self.sentence2)
|
71 |
+
|
72 |
+
if self.button:
|
73 |
+
st.text(ss.results())
|
74 |
+
# print(ss.results())
|
75 |
+
|
76 |
+
ui = UI()
|
77 |
+
ui.result()
|
requirements.txt
ADDED
File without changes
|