levimack commited on
Commit
b2202c6
1 Parent(s): a43aa93

Update scripture_compare.py

Browse files
Files changed (1) hide show
  1. scripture_compare.py +32 -29
scripture_compare.py CHANGED
@@ -1,12 +1,11 @@
1
  import gradio as gr
2
  import urllib.request, json
3
- from transformers.utils import logging
4
- logging.set_verbosity_error()
5
-
6
  from sentence_transformers import SentenceTransformer
7
  from sentence_transformers import util
 
 
8
 
9
- # Search URL for Mackabee Ministries scriptures. Will fetch scriptures as a json object.
10
  SEARCH_URL = 'https://dd4-biblical.appspot.com/_api/scriptures/v1/search?searchText={}&lang=en&version={}'
11
 
12
  model = SentenceTransformer("all-MiniLM-L6-v2")
@@ -17,18 +16,18 @@ class Candidate:
17
  compareText = ''
18
  score = 0
19
 
20
- def __init__(self, book, chapter, verse, standardText):
21
  self.book = book
22
  self.chapter = chapter
23
  self.verse = verse
24
- self.standardText = standardText
25
 
26
  def reference(self):
27
  return '{} {}:{}'.format(self.book, self.chapter, self.verse)
28
 
29
  def to_tabbed(self):
30
  return '{}\t\t {:.4f}\t\t {}\t\t {}'.format(self.reference(), self.score, self.standardText, self.compareText)
31
-
32
  def to_csv(self):
33
  return '{},{:.4f},"{}","{}"'.format(self.reference(), self.score, self.standardText, self.compareText)
34
 
@@ -38,11 +37,12 @@ class Candidate:
38
  def __repr__(self):
39
  return self.to_csv()
40
 
41
- def compare(reference, standardVersion, compareVersion):
 
42
  candidates = []
43
- candidateMap = {}
44
  # Connect to the api and get the standard translation
45
- with urllib.request.urlopen(SEARCH_URL.format(urllib.parse.quote(reference), standardVersion)) as url:
46
  response = json.load(url)
47
  # print(response)
48
 
@@ -50,34 +50,34 @@ def compare(reference, standardVersion, compareVersion):
50
  # print(result)
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), compareVersion)) as url:
57
  response = json.load(url)
58
  # print(response)
59
 
60
  for result in response['items']:
61
  # print(result)
62
- candidate = candidateMap['{} {}:{}'.format(result['book'], result['chapter'], result['verse'])]
63
- if candidate is None :
64
  candidate = Candidate(result['book'], result['chapter'], result['verse'], '')
65
  candidates.append(candidate)
66
- candidateMap[candidate.reference()] = candidate
67
 
68
  candidate.compareText = result['text']
69
 
70
  # Isa 1:1 standardText: This is the book of Isaiah compareText: The book that Isaiah wrote.
71
  # Isa 1:2 standardText: Isaiah was a good man compareText: Isaiah did what was right.
72
-
73
- standardTexts = [] # 2 items This is the book of Isaiah, Isaiah was a good man
74
- compareTexts = [] # 2 items The book that Isaiah wrote., Isaiah did what was right.
75
  for candidate in candidates:
76
- standardTexts.append(candidate.standardText)
77
- compareTexts.append(candidate.compareText)
78
-
79
- embeddings1 = model.encode(standardTexts, convert_to_tensor=True)
80
- embeddings2 = model.encode(compareTexts, convert_to_tensor=True)
81
  cosine_scores = util.cos_sim(embeddings1, embeddings2)
82
 
83
  for i in range(len(candidates)):
@@ -87,16 +87,19 @@ def compare(reference, standardVersion, compareVersion):
87
  return '\n\n'.join(c.to_tabbed() for c in candidates)
88
 
89
 
90
- demo = gr.Interface(
91
  compare,
92
  [
93
  gr.Textbox(
94
  label="Scriptures",
95
- info="Enter a verse, verse range, chapter(s) or mix (e.g. Gen 2:3 or Gen 2:1-3 or Exo 12 or Lev 20-23 or Gen 2:3,Exo 12,Lev 20-23)",
 
96
  lines=1,
97
  ),
98
- gr.Radio(["ISR", "RSKJ", "NRSV", "NWT", "SEP", "DSS"], label="Control Version", info="The version of scripture to use as the standard.", value="RSKJ"),
99
- gr.Radio(["ISR", "RSKJ", "NRSV", "NWT", "SEP", "DSS"], label="Comparison Version", info="The version of scripture to use as the comparison.", value="SEP")
 
 
100
  ],
101
  gr.Textbox(label="Comparisons", lines=28))
102
- demo.launch()
 
1
  import gradio as gr
2
  import urllib.request, json
 
 
 
3
  from sentence_transformers import SentenceTransformer
4
  from sentence_transformers import util
5
+ from transformers.utils import logging
6
+ logging.set_verbosity_error()
7
 
8
+ # Search URL for Maccabees Ministries scriptures. Will fetch scriptures as a json object.
9
  SEARCH_URL = 'https://dd4-biblical.appspot.com/_api/scriptures/v1/search?searchText={}&lang=en&version={}'
10
 
11
  model = SentenceTransformer("all-MiniLM-L6-v2")
 
16
  compareText = ''
17
  score = 0
18
 
19
+ def __init__(self, book, chapter, verse, standard_text):
20
  self.book = book
21
  self.chapter = chapter
22
  self.verse = verse
23
+ self.standardText = standard_text
24
 
25
  def reference(self):
26
  return '{} {}:{}'.format(self.book, self.chapter, self.verse)
27
 
28
  def to_tabbed(self):
29
  return '{}\t\t {:.4f}\t\t {}\t\t {}'.format(self.reference(), self.score, self.standardText, self.compareText)
30
+
31
  def to_csv(self):
32
  return '{},{:.4f},"{}","{}"'.format(self.reference(), self.score, self.standardText, self.compareText)
33
 
 
37
  def __repr__(self):
38
  return self.to_csv()
39
 
40
+
41
+ def compare(reference, standard_version, compare_version):
42
  candidates = []
43
+ candidate_map = {}
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
 
 
50
  # print(result)
51
  candidate = Candidate(result['book'], result['chapter'], result['verse'], result['text'])
52
  candidates.append(candidate)
53
+ candidate_map[candidate.reference()] = candidate
54
+
55
  # Then fetch the comparison text
56
+ with urllib.request.urlopen(SEARCH_URL.format(urllib.parse.quote(reference), compare_version)) as url:
57
  response = json.load(url)
58
  # print(response)
59
 
60
  for result in response['items']:
61
  # print(result)
62
+ candidate = candidate_map['{} {}:{}'.format(result['book'], result['chapter'], result['verse'])]
63
+ if candidate is None:
64
  candidate = Candidate(result['book'], result['chapter'], result['verse'], '')
65
  candidates.append(candidate)
66
+ candidate_map[candidate.reference()] = candidate
67
 
68
  candidate.compareText = result['text']
69
 
70
  # Isa 1:1 standardText: This is the book of Isaiah compareText: The book that Isaiah wrote.
71
  # Isa 1:2 standardText: Isaiah was a good man compareText: Isaiah did what was right.
72
+
73
+ standard_texts = [] # 2 items This is the book of Isaiah, Isaiah was a good man
74
+ compare_texts = [] # 2 items The book that Isaiah wrote., Isaiah did what was right.
75
  for candidate in candidates:
76
+ standard_texts.append(candidate.standardText)
77
+ compare_texts.append(candidate.compareText)
78
+
79
+ embeddings1 = model.encode(standard_texts, convert_to_tensor=True)
80
+ embeddings2 = model.encode(compare_texts, convert_to_tensor=True)
81
  cosine_scores = util.cos_sim(embeddings1, embeddings2)
82
 
83
  for i in range(len(candidates)):
 
87
  return '\n\n'.join(c.to_tabbed() for c in candidates)
88
 
89
 
90
+ ui = gr.Interface(
91
  compare,
92
  [
93
  gr.Textbox(
94
  label="Scriptures",
95
+ info="Enter a verse, verse range, chapter(s) or mix "
96
+ + "(e.g. Gen 2:3 or Gen 2:1-3 or Exo 12 or Lev 20-23 or Gen 2:3,Exo 12,Lev 20-23)",
97
  lines=1,
98
  ),
99
+ gr.Radio(["ISR", "RSKJ", "NRSV", "NWT", "SEP", "DSS"], label="Control Version",
100
+ info="The version of scripture to use as the standard.", value="RSKJ"),
101
+ gr.Radio(["ISR", "RSKJ", "NRSV", "NWT", "SEP", "DSS"], label="Comparison Version",
102
+ info="The version of scripture to use as the comparison.", value="SEP")
103
  ],
104
  gr.Textbox(label="Comparisons", lines=28))
105
+ ui.launch()