|
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 = 'https://dd4-biblical.appspot.com/_api/scriptures/v1/search?searchText={}&lang=en&version={}' |
|
STANDARD_VERSION = 'RSKJ' |
|
COMPARISON_VERSION = 'SEP' |
|
|
|
model = SentenceTransformer("all-MiniLM-L6-v2") |
|
|
|
|
|
|
|
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 = {} |
|
|
|
with urllib.request.urlopen(SEARCH_URL.format(urllib.parse.quote(reference), STANDARD_VERSION)) as url: |
|
response = json.load(url) |
|
|
|
|
|
for result in response['items']: |
|
|
|
candidate = Candidate(result['book'], result['chapter'], result['verse'], result['text']) |
|
candidates.append(candidate) |
|
candidateMap[candidate.reference()] = candidate |
|
|
|
|
|
with urllib.request.urlopen(SEARCH_URL.format(urllib.parse.quote(reference), COMPARISON_VERSION)) as url: |
|
response = json.load(url) |
|
|
|
|
|
for result in response['items']: |
|
|
|
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'] |
|
|
|
|
|
|
|
|
|
standardTexts = [] |
|
compareTexts = [] |
|
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] |
|
|
|
|
|
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() |