Update scripture_compare.py
Browse files- scripture_compare.py +23 -23
scripture_compare.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import urllib.request, json
|
2 |
from transformers.utils import logging
|
3 |
logging.set_verbosity_error()
|
@@ -15,6 +16,8 @@ COMPARISON_VERSION = 'SEP' # Will use the Septuagint "SEP" version for most comp
|
|
15 |
# types
|
16 |
REFERENCES = 'Gen 1'
|
17 |
|
|
|
|
|
18 |
|
19 |
# Candidate helper class that holds a scripture, the comparison text and the resulting match score.
|
20 |
class Candidate:
|
@@ -36,24 +39,10 @@ class Candidate:
|
|
36 |
def __repr__(self):
|
37 |
return str(self)
|
38 |
|
39 |
-
def compare(
|
40 |
-
embeddings1 = model.encode(candidate.standardText, convert_to_tensor=True)
|
41 |
-
#print(embeddings1)
|
42 |
-
embeddings2 = model.encode(candidate.compareText, convert_to_tensor=True)
|
43 |
-
#print(embeddings2)
|
44 |
-
|
45 |
-
cosine_scores = util.cos_sim(embeddings1, embeddings2)
|
46 |
-
#print(cosine_scores)
|
47 |
-
|
48 |
-
candidate.score = cosine_scores[0][0]
|
49 |
-
|
50 |
-
|
51 |
-
if __name__ == '__main__':
|
52 |
-
|
53 |
candidates = []
|
54 |
-
|
55 |
-
|
56 |
-
with urllib.request.urlopen(SEARCH_URL.format(urllib.parse.quote(REFERENCES), STANDARD_VERSION)) as url:
|
57 |
response = json.load(url)
|
58 |
# print(response)
|
59 |
|
@@ -62,9 +51,9 @@ if __name__ == '__main__':
|
|
62 |
candidate = Candidate(result['book'], result['chapter'], result['verse'], result['text'])
|
63 |
candidates.append(candidate)
|
64 |
candidateMap[candidate.reference()] = candidate
|
65 |
-
|
66 |
# Then fetch the comparison text
|
67 |
-
with urllib.request.urlopen(SEARCH_URL.format(urllib.parse.quote(
|
68 |
response = json.load(url)
|
69 |
# print(response)
|
70 |
|
@@ -78,8 +67,19 @@ if __name__ == '__main__':
|
|
78 |
|
79 |
candidate.compareText = result['text']
|
80 |
|
81 |
-
|
82 |
-
|
|
|
83 |
for candidate in candidates:
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio
|
2 |
import urllib.request, json
|
3 |
from transformers.utils import logging
|
4 |
logging.set_verbosity_error()
|
|
|
16 |
# types
|
17 |
REFERENCES = 'Gen 1'
|
18 |
|
19 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
20 |
+
|
21 |
|
22 |
# Candidate helper class that holds a scripture, the comparison text and the resulting match score.
|
23 |
class Candidate:
|
|
|
39 |
def __repr__(self):
|
40 |
return str(self)
|
41 |
|
42 |
+
def compare(reference):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
candidates = []
|
44 |
+
# Connect to the api and get the standard translation
|
45 |
+
with urllib.request.urlopen(SEARCH_URL.format(urllib.parse.quote(reference), STANDARD_VERSION)) as url:
|
|
|
46 |
response = json.load(url)
|
47 |
# print(response)
|
48 |
|
|
|
51 |
candidate = Candidate(result['book'], result['chapter'], result['verse'], result['text'])
|
52 |
candidates.append(candidate)
|
53 |
candidateMap[candidate.reference()] = candidate
|
54 |
+
|
55 |
# Then fetch the comparison text
|
56 |
+
with urllib.request.urlopen(SEARCH_URL.format(urllib.parse.quote(reference), COMPARISON_VERSION)) as url:
|
57 |
response = json.load(url)
|
58 |
# print(response)
|
59 |
|
|
|
67 |
|
68 |
candidate.compareText = result['text']
|
69 |
|
70 |
+
|
71 |
+
standardTexts = []
|
72 |
+
compareTexts = []
|
73 |
for candidate in candidates:
|
74 |
+
standardTexts.append(candidate.standardText)
|
75 |
+
compareTexts.append(candidate.compareText)
|
76 |
+
|
77 |
+
embeddings1 = model.encode(standardTexts, convert_to_tensor=True)
|
78 |
+
embeddings2 = model.encode(compareTexts, convert_to_tensor=True)
|
79 |
+
cosine_scores = util.cos_sim(embeddings1, embeddings2)
|
80 |
+
|
81 |
+
for i in range(len(candidates)):
|
82 |
+
candidates[i].score = cosine_scores[i][i]
|
83 |
+
print(candidates[i])
|
84 |
+
|
85 |
+
return candidates
|