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()