File size: 3,766 Bytes
7fa1f25
21e50d6
 
 
 
 
 
 
 
 
 
 
 
3a87bdf
 
21e50d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a87bdf
21e50d6
02a6a93
3a87bdf
 
21e50d6
 
 
 
 
 
 
 
3a87bdf
21e50d6
3a87bdf
21e50d6
 
 
 
 
 
 
 
 
 
 
 
 
02a6a93
 
3a87bdf
02a6a93
 
21e50d6
3a87bdf
 
 
 
 
 
 
 
 
c37860e
3a87bdf
7bbb090
fa1f8f2
 
04a7cb9
 
 
 
 
 
 
daf2dcd
fa1f8f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import gradio as gr
import urllib.request, json
from transformers.utils import logging
logging.set_verbosity_error()

from sentence_transformers import SentenceTransformer
from sentence_transformers import util

# Search URL for Mackabee Ministries scriptures. Will fetch scriptures as a json object.
SEARCH_URL = 'https://dd4-biblical.appspot.com/_api/scriptures/v1/search?searchText={}&lang=en&version={}'
STANDARD_VERSION = 'RSKJ' # Normally RSKJ (Restored King James Version) as our standard text, may also use NRSV.
COMPARISON_VERSION = 'SEP' # Will use the Septuagint "SEP" version for most comparisons, also have DSS available for Isa

model = SentenceTransformer("all-MiniLM-L6-v2")


# Candidate helper class that holds a scripture, the comparison text and the resulting match score.
class Candidate:
    compareText = ''
    score = 0

    def __init__(self, book, chapter, verse, standardText):
        self.book = book
        self.chapter = chapter
        self.verse = verse
        self.standardText = standardText

    def reference(self):
        return '{} {}:{}'.format(self.book, self.chapter, self.verse)

    def __str__(self):
        return '{},{:.4f},"{}","{}"'.format(self.reference(), self.score, self.standardText, self.compareText)

    def __repr__(self):
        return str(self)

def compare(reference):
    candidates = []
    candidateMap = {}
    # Connect to the api and get the standard translation
    with urllib.request.urlopen(SEARCH_URL.format(urllib.parse.quote(reference), STANDARD_VERSION)) as url:
        response = json.load(url)
        # print(response)

        for result in response['items']:
            # print(result)
            candidate = Candidate(result['book'], result['chapter'], result['verse'], result['text'])
            candidates.append(candidate)
            candidateMap[candidate.reference()] = candidate
            
    # Then fetch the comparison text
    with urllib.request.urlopen(SEARCH_URL.format(urllib.parse.quote(reference), COMPARISON_VERSION)) as url:
        response = json.load(url)
        # print(response)

        for result in response['items']:
            # print(result)
            candidate = candidateMap['{} {}:{}'.format(result['book'], result['chapter'], result['verse'])]
            if candidate is None :
                candidate = Candidate(result['book'], result['chapter'], result['verse'], '')
                candidates.append(candidate)
                candidateMap[candidate.reference()] = candidate

            candidate.compareText = result['text']

        # Isa 1:1 standardText: This is the book of Isaiah compareText: The book that Isaiah wrote.
        # Isa 1:2 standardText: Isaiah was a good man compareText: Isaiah did what was right.
        
    standardTexts = [] # 2 items This is the book of Isaiah, Isaiah was a good man
    compareTexts = [] # 2 items The book that Isaiah wrote., Isaiah did what was right.
    for candidate in candidates:
        standardTexts.append(candidate.standardText)
        compareTexts.append(candidate.compareText)
        
    embeddings1 = model.encode(standardTexts, convert_to_tensor=True)
    embeddings2 = model.encode(compareTexts, convert_to_tensor=True)
    cosine_scores = util.cos_sim(embeddings1, embeddings2)

    for i in range(len(candidates)):
        candidates[i].score = cosine_scores[i][i]
        # print(candidates[i])

    return '\n\n'.join(str(c) for c in candidates)


demo = gr.Interface(
    compare,
     gr.Textbox(
         label="Scriptures",
         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)",
         lines=1,
     ),
     gr.Textbox(label="Comparisons", lines=28))
demo.launch()