Spaces:
Running
Running
Update app.py
Browse filesbible book filtering
app.py
CHANGED
|
@@ -68,6 +68,28 @@ relevance_terms = [
|
|
| 68 |
'Likely highly relevant'
|
| 69 |
]
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def make_sorted_passages(passages): #, bible_books):
|
| 72 |
global bible_books
|
| 73 |
numbered_passages = []
|
|
@@ -295,7 +317,7 @@ def seek_hms(seek_ms):
|
|
| 295 |
secs = int(seek_ms - hrs * 3600 - mins * 60)
|
| 296 |
return f'{hrs}h{mins}m{secs}s'
|
| 297 |
|
| 298 |
-
def do_bible_search(prompt, db_name):
|
| 299 |
db_name += '.db'
|
| 300 |
if (not os.path.exists(dataDir + db_name)):
|
| 301 |
return ([])
|
|
@@ -303,8 +325,9 @@ def do_bible_search(prompt, db_name):
|
|
| 303 |
(prompt_embed, prompt_tokens, total_tokens) = get_prompt_embedding(prompt)
|
| 304 |
dot_products = []
|
| 305 |
for (book, chapter, verse_range, verse, db_embed) in embeddings:
|
| 306 |
-
|
| 307 |
-
|
|
|
|
| 308 |
sorted_dots = sorted(dot_products, key=lambda x: x[4])[-10:] # was -10, -5
|
| 309 |
sorted_dots.reverse()
|
| 310 |
return (sorted_dots, prompt_tokens, total_tokens)
|
|
@@ -563,7 +586,7 @@ def updatePassword(user, pwd):
|
|
| 563 |
|
| 564 |
|
| 565 |
def chat(prompt, user_window, pwd_window, past, response, gptModel, clip_text, db_name,
|
| 566 |
-
start_date,end_date, language, en_hebrew): #, bible_books):
|
| 567 |
global bible_books
|
| 568 |
user_window = user_window.lower().strip()
|
| 569 |
translation_count = 0
|
|
@@ -627,15 +650,24 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, clip_text, d
|
|
| 627 |
if len(past) == 0:
|
| 628 |
first_time = True
|
| 629 |
if 'bible' in db_name.casefold():
|
|
|
|
|
|
|
|
|
|
| 630 |
if not populate_bible_books():
|
| 631 |
return [past, 'Failed to load Bible books', None, gptModel,clip_text]
|
| 632 |
instructions = '''You are a helpful assistant who has expert knowledge
|
| 633 |
of the Bible and is familiar with Hebrew versions of biblical names. '''
|
| 634 |
past.append({'role':'developer', 'content': instructions})
|
| 635 |
prompt = make_hebrew(prompt, en_hebrew)
|
| 636 |
-
(results, prompt_tokens, total_tokens) = do_bible_search(prompt,
|
| 637 |
-
|
| 638 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 639 |
bible_list.append(txt)
|
| 640 |
if len(results) == 0:
|
| 641 |
txt = '\nSorry, no verses were found in response to your prompt\n'
|
|
@@ -835,19 +867,29 @@ def show_help():
|
|
| 835 |
txt = '''
|
| 836 |
MTOI Search scans a database you select that contains transcripts of MTOI video teachings, or the
|
| 837 |
ISR Scriptures, finding sections/passages that relate to the question or topic you enter.
|
| 838 |
-
It formulates a response based on that text found
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 843 |
1. Gemeral:
|
| 844 |
1.1 Login with user name and password (not case-sensitive)
|
| 845 |
1.2 Select a database (topic) using "Choose Topic". You can target all the teaching
|
| 846 |
-
video databases by selecting "All Teaching Topics"
|
|
|
|
|
|
|
| 847 |
1.3 Select a Translation language (initially defaults to "English")
|
| 848 |
1.4 Type prompts (questions, topics) into "Prompt or Question" window.
|
| 849 |
-
1.5
|
| 850 |
-
Start Date and End Date entries.
|
|
|
|
|
|
|
| 851 |
2. Search:
|
| 852 |
2.1 Enter prompt/question and tap the "Submit Prompt/Question" button. The responses appear in the Dialog window.
|
| 853 |
2.2 Enter follow-up questions in the Prompt window. Then tap "Submit Prompt/Question".
|
|
@@ -902,10 +944,15 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 902 |
with gr.Column(scale=3):
|
| 903 |
prompt_window = gr.Textbox(label = "Prompt or Question", scale=3)
|
| 904 |
with gr.Column(scale=2):
|
| 905 |
-
gr.Markdown('### **Optional Date Filter. Most common formats are OK<br />such as 12/2004, jan 2015, 4 Dec 2012**')
|
| 906 |
with gr.Row():
|
| 907 |
start_date = gr.Textbox(label='Start Date (YYYY-mm-dd)', scale =1,value='1990-01-01',max_lines=1)
|
| 908 |
end_date = gr.Textbox(label='End Date (YYYY-mm-dd)', scale =1,value=etz_now(),max_lines=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 909 |
gr.Markdown('### **Dialog:**')
|
| 910 |
#output_window = gr.Text(container=True, label='Dialog')
|
| 911 |
output_window = gr.Markdown(container=True)
|
|
@@ -914,7 +961,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 914 |
pwd_window.blur(updatePassword, inputs = [user_window, pwd_window], outputs = [password, pwd_window, button_upload_db])
|
| 915 |
submit_button.click(chat,
|
| 916 |
inputs=[prompt_window, user_window, password, history, output_window,
|
| 917 |
-
model, clip_text, db_chooser,start_date,end_date, lang_chooser,
|
|
|
|
| 918 |
# bible_books],
|
| 919 |
outputs=[history, output_window, prompt_window, model, clip_text])
|
| 920 |
clear_button.click(fn=new_conversation, inputs=[user_window],
|
|
@@ -923,9 +971,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 923 |
button_upload_db.click(fn=upload_db_file,inputs = [file_browser_visibility],
|
| 924 |
outputs = [file_browser_visibility, db_file])
|
| 925 |
db_file.upload(fn=write_db_file, inputs=[db_file], outputs=[output_window])
|
| 926 |
-
|
|
|
|
| 927 |
timer.tick(fn=init_db_end_date, inputs=[en_hebrew], # bible_books],
|
| 928 |
outputs=[timer, db_chooser, end_date, en_hebrew]) #, bible_books])
|
|
|
|
|
|
|
| 929 |
|
| 930 |
demo.launch(share=True, allowed_paths=[dataDir], ssr_mode=False)
|
| 931 |
|
|
|
|
| 68 |
'Likely highly relevant'
|
| 69 |
]
|
| 70 |
|
| 71 |
+
def check_books(filter: bool, books: []):
|
| 72 |
+
if len(books) == 0 and filter:
|
| 73 |
+
return md('<h5>Warning: You enabled bible book filter but have no books selected in filter</h5>\n')
|
| 74 |
+
else:
|
| 75 |
+
return 'ok'
|
| 76 |
+
|
| 77 |
+
def on_db_change(db_name: str):
|
| 78 |
+
global bible_books
|
| 79 |
+
date_vis = True
|
| 80 |
+
bible_vis = False
|
| 81 |
+
books = []
|
| 82 |
+
if 'ISR' in db_name:
|
| 83 |
+
date_vis = False
|
| 84 |
+
bible_vis = True
|
| 85 |
+
if populate_bible_books():
|
| 86 |
+
for key in bible_books.keys():
|
| 87 |
+
books.append(key)
|
| 88 |
+
return [gr.Markdown(visible=date_vis), gr.Textbox(visible=date_vis),
|
| 89 |
+
gr.Textbox(visible=date_vis),
|
| 90 |
+
gr.Dropdown(visible=bible_vis, choices=books),
|
| 91 |
+
gr.Checkbox(visible=bible_vis)]
|
| 92 |
+
|
| 93 |
def make_sorted_passages(passages): #, bible_books):
|
| 94 |
global bible_books
|
| 95 |
numbered_passages = []
|
|
|
|
| 317 |
secs = int(seek_ms - hrs * 3600 - mins * 60)
|
| 318 |
return f'{hrs}h{mins}m{secs}s'
|
| 319 |
|
| 320 |
+
def do_bible_search(prompt, db_name, books, book_filter):
|
| 321 |
db_name += '.db'
|
| 322 |
if (not os.path.exists(dataDir + db_name)):
|
| 323 |
return ([])
|
|
|
|
| 325 |
(prompt_embed, prompt_tokens, total_tokens) = get_prompt_embedding(prompt)
|
| 326 |
dot_products = []
|
| 327 |
for (book, chapter, verse_range, verse, db_embed) in embeddings:
|
| 328 |
+
if not book_filter or book in books:
|
| 329 |
+
dp = dot_product(prompt_embed, db_embed)
|
| 330 |
+
dot_products.append((book, chapter, verse_range, verse, dp) )
|
| 331 |
sorted_dots = sorted(dot_products, key=lambda x: x[4])[-10:] # was -10, -5
|
| 332 |
sorted_dots.reverse()
|
| 333 |
return (sorted_dots, prompt_tokens, total_tokens)
|
|
|
|
| 586 |
|
| 587 |
|
| 588 |
def chat(prompt, user_window, pwd_window, past, response, gptModel, clip_text, db_name,
|
| 589 |
+
start_date,end_date, language, en_hebrew, books, book_filter): #, bible_books):
|
| 590 |
global bible_books
|
| 591 |
user_window = user_window.lower().strip()
|
| 592 |
translation_count = 0
|
|
|
|
| 650 |
if len(past) == 0:
|
| 651 |
first_time = True
|
| 652 |
if 'bible' in db_name.casefold():
|
| 653 |
+
msg = check_books(book_filter, books)
|
| 654 |
+
if msg != 'ok':
|
| 655 |
+
return [past, msg, None, gptModel,clip_text]
|
| 656 |
if not populate_bible_books():
|
| 657 |
return [past, 'Failed to load Bible books', None, gptModel,clip_text]
|
| 658 |
instructions = '''You are a helpful assistant who has expert knowledge
|
| 659 |
of the Bible and is familiar with Hebrew versions of biblical names. '''
|
| 660 |
past.append({'role':'developer', 'content': instructions})
|
| 661 |
prompt = make_hebrew(prompt, en_hebrew)
|
| 662 |
+
(results, prompt_tokens, total_tokens) = do_bible_search(prompt,
|
| 663 |
+
db_name,
|
| 664 |
+
books,
|
| 665 |
+
book_filter)
|
| 666 |
+
insert = ''
|
| 667 |
+
if book_filter:
|
| 668 |
+
book_listing = ' ,'.join(books)
|
| 669 |
+
insert = f' From books in filter: {book_listing}, '
|
| 670 |
+
txt = f'\n=================\n\n<h5>Following are ISR Bible verses in response to your query.</h5>{insert} Listed in the order they appear in the bible:\n=================\n'
|
| 671 |
bible_list.append(txt)
|
| 672 |
if len(results) == 0:
|
| 673 |
txt = '\nSorry, no verses were found in response to your prompt\n'
|
|
|
|
| 867 |
txt = '''
|
| 868 |
MTOI Search scans a database you select that contains transcripts of MTOI video teachings, or the
|
| 869 |
ISR Scriptures, finding sections/passages that relate to the question or topic you enter.
|
| 870 |
+
It formulates a response based on that text found. It appends to the response as
|
| 871 |
+
follows:
|
| 872 |
+
|
| 873 |
+
For video teachings, it lists at least five text clips plus YouTube links to
|
| 874 |
+
the video at the point when that text is spoken. Prompts may be entered in either
|
| 875 |
+
English or the selected translation language. Responses will be given in the
|
| 876 |
+
selected translation language.
|
| 877 |
+
|
| 878 |
+
For ISR_Bible searches, it lists up to ten bible passages. The AI response will be
|
| 879 |
+
given in the selected translation language but the ISR Bible verses remain as found.
|
| 880 |
+
Prompts/questions should be entered in English
|
| 881 |
1. Gemeral:
|
| 882 |
1.1 Login with user name and password (not case-sensitive)
|
| 883 |
1.2 Select a database (topic) using "Choose Topic". You can target all the teaching
|
| 884 |
+
video databases by selecting "All Teaching Topics". Note: This selection does not
|
| 885 |
+
include ISR_Bible scripture in the search. Bible and teachings searches are two
|
| 886 |
+
distinct procedures and cannot be combined.
|
| 887 |
1.3 Select a Translation language (initially defaults to "English")
|
| 888 |
1.4 Type prompts (questions, topics) into "Prompt or Question" window.
|
| 889 |
+
1.5 For teaching videos, you can limit results based on the dates when the results were uploaded to YouTube with the
|
| 890 |
+
Start Date and End Date entries.
|
| 891 |
+
1.6 For ISR_Bible searches, you can filter searches to any selection of books by checking
|
| 892 |
+
Activate Filter and selecting book(s) you want to limit search to.
|
| 893 |
2. Search:
|
| 894 |
2.1 Enter prompt/question and tap the "Submit Prompt/Question" button. The responses appear in the Dialog window.
|
| 895 |
2.2 Enter follow-up questions in the Prompt window. Then tap "Submit Prompt/Question".
|
|
|
|
| 944 |
with gr.Column(scale=3):
|
| 945 |
prompt_window = gr.Textbox(label = "Prompt or Question", scale=3)
|
| 946 |
with gr.Column(scale=2):
|
| 947 |
+
filter_heading = gr.Markdown('### **Optional Date Filter. Most common formats are OK<br />such as 12/2004, jan 2015, 4 Dec 2012**')
|
| 948 |
with gr.Row():
|
| 949 |
start_date = gr.Textbox(label='Start Date (YYYY-mm-dd)', scale =1,value='1990-01-01',max_lines=1)
|
| 950 |
end_date = gr.Textbox(label='End Date (YYYY-mm-dd)', scale =1,value=etz_now(),max_lines=1)
|
| 951 |
+
checkbox_filter = gr.Checkbox(label='Activate Book Filter', scale=2,
|
| 952 |
+
show_label=True, visible=False)
|
| 953 |
+
book_chooser = gr.Dropdown(choices=[],type='value', scale=3,
|
| 954 |
+
multiselect=True, interactive=True,
|
| 955 |
+
label='Book Filter, Select one or more', visible=False)
|
| 956 |
gr.Markdown('### **Dialog:**')
|
| 957 |
#output_window = gr.Text(container=True, label='Dialog')
|
| 958 |
output_window = gr.Markdown(container=True)
|
|
|
|
| 961 |
pwd_window.blur(updatePassword, inputs = [user_window, pwd_window], outputs = [password, pwd_window, button_upload_db])
|
| 962 |
submit_button.click(chat,
|
| 963 |
inputs=[prompt_window, user_window, password, history, output_window,
|
| 964 |
+
model, clip_text, db_chooser,start_date,end_date, lang_chooser,
|
| 965 |
+
en_hebrew, book_chooser, checkbox_filter],
|
| 966 |
# bible_books],
|
| 967 |
outputs=[history, output_window, prompt_window, model, clip_text])
|
| 968 |
clear_button.click(fn=new_conversation, inputs=[user_window],
|
|
|
|
| 971 |
button_upload_db.click(fn=upload_db_file,inputs = [file_browser_visibility],
|
| 972 |
outputs = [file_browser_visibility, db_file])
|
| 973 |
db_file.upload(fn=write_db_file, inputs=[db_file], outputs=[output_window])
|
| 974 |
+
db_chooser.input(fn=on_db_change,inputs= [db_chooser],
|
| 975 |
+
outputs= [filter_heading, start_date, end_date, book_chooser, checkbox_filter])
|
| 976 |
timer.tick(fn=init_db_end_date, inputs=[en_hebrew], # bible_books],
|
| 977 |
outputs=[timer, db_chooser, end_date, en_hebrew]) #, bible_books])
|
| 978 |
+
# checkbox_filter.input(fn=check_books, inputs=[checkbox_filter, book_chooser],
|
| 979 |
+
# outputs=[output_window])
|
| 980 |
|
| 981 |
demo.launch(share=True, allowed_paths=[dataDir], ssr_mode=False)
|
| 982 |
|