neuralworm's picture
Update app.py
e594864 verified
raw
history blame
10.4 kB
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
import gradio as gr
import torah
import bible
import quran
from utils import number_to_ordinal_word, custom_normalize, date_to_words, translate_date_to_words
from gematria import calculate_gematria, strip_diacritics
import pandas as pd
from deep_translator import GoogleTranslator
from gradio_calendar import Calendar
from datetime import datetime
import math
import json
import re
import sqlite3
from collections import defaultdict
# --- Constants ---
DATABASE_FILE = 'gematria.db'
MAX_PHRASE_LENGTH_LIMIT = 20
# --- Database Initialization ---
def initialize_database():
global conn
conn = sqlite3.connect(DATABASE_FILE)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS results (
gematria_sum INTEGER,
words TEXT,
translation TEXT,
book TEXT,
chapter INTEGER,
verse INTEGER,
phrase_length INTEGER,
word_position TEXT,
PRIMARY KEY (gematria_sum, words, book, chapter, verse, word_position)
)
''')
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_results_gematria
ON results (gematria_sum)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS processed_books (
book TEXT PRIMARY KEY,
max_phrase_length INTEGER
)
''')
conn.commit()
# --- Initialize Database ---
initialize_database()
# --- Helper Functions (from Network app.py) ---
def flatten_text(text: List) -> str:
if isinstance(text, list):
return " ".join(flatten_text(item) if isinstance(item, list) else item for item in text)
return text
def search_gematria_in_db(gematria_sum: int, max_words: int) -> List[Tuple[str, str, int, int, int, str]]:
global conn
with sqlite3.connect(DATABASE_FILE) as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT words, book, chapter, verse, phrase_length, word_position
FROM results
WHERE gematria_sum = ? AND phrase_length <= ?
''', (gematria_sum, max_words))
results = cursor.fetchall()
return results
def get_most_frequent_phrase(results):
phrase_counts = defaultdict(int)
for words, book, chapter, verse, phrase_length, word_position in results:
phrase_counts[words] += 1
most_frequent_phrase = max(phrase_counts, key=phrase_counts.get) if phrase_counts else None # Handle empty results
return most_frequent_phrase
# --- Functions from BOS app.py ---
def create_language_dropdown(label, default_value='en', show_label=True):
languages = GoogleTranslator(source='en', target='en').get_supported_languages(as_dict=True)
return gr.Dropdown(
choices=list(languages.keys()),
label=label,
value=default_value,
show_label=show_label
)
def calculate_gematria_sum(text, date_words):
if text or date_words:
combined_input = f"{text} {date_words}"
numbers = re.findall(r'\d+', combined_input)
text_without_numbers = re.sub(r'\d+', '', combined_input)
number_sum = sum(int(number) for number in numbers)
text_gematria = calculate_gematria(strip_diacritics(text_without_numbers))
total_sum = text_gematria + number_sum
return total_sum
else:
return None
def perform_els_search(step, rounds_combination, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk, merge_results, include_torah, include_bible, include_quran):
if step == 0 or rounds_combination == "0,0":
return None
torah_results = []
bible_results = []
quran_results = []
if include_torah:
torah_results.extend(torah.process_json_files(1, 39, step, rounds_combination, 0, tlang, strip_spaces, strip_in_braces, strip_diacritics))
if include_bible:
bible_results.extend(bible.process_json_files(40, 66, step, rounds_combination, 0, tlang, strip_spaces, strip_in_braces, strip_diacritics))
if include_quran:
quran_results.extend(quran.process_json_files(1, 114, step, rounds_combination, 0, tlang, strip_spaces, strip_in_braces, strip_diacritics))
if merge_results:
results = []
max_length = max(len(torah_results), len(bible_results), len(quran_results))
for i in range(max_length):
if i < len(torah_results):
results.append(torah_results[i])
if i < len(bible_results):
results.append(bible_results[i])
if i < len(quran_results):
results.append(quran_results[i])
else:
results = torah_results + bible_results + quran_results
return results
# --- Main Gradio App ---
with gr.Blocks() as app:
with gr.Row():
tlang = create_language_dropdown("Target Language for Translation", default_value='english')
selected_date = Calendar(type="datetime", label="Date to investigate (optional)", info="Pick a date from the calendar")
date_language_input = create_language_dropdown("Language of the person/topic (optional) (Date Word Language)", default_value='english')
date_words_output = gr.Textbox(label="Date in Words Translated (optional)")
with gr.Row():
gematria_text = gr.Textbox(label="Name and/or Topic (required)", value="Hans Albert Einstein")
gematria_result = gr.Number(label="Journal Sum")
with gr.Row():
step = gr.Number(label="Jump Width (Steps) for ELS")
float_step = gr.Number(visible=False, value=1)
half_step_btn = gr.Button("Steps / 2")
double_step_btn = gr.Button("Steps * 2")
with gr.Column():
round_x = gr.Number(label="Round (1)", value=1)
round_y = gr.Number(label="Round (2)", value=-1)
rounds_combination = gr.Textbox(label="Combined Rounds", value="1,-1")
with gr.Row():
include_torah_chk = gr.Checkbox(label="Include Torah", value=True)
include_bible_chk = gr.Checkbox(label="Include Bible", value=True)
include_quran_chk = gr.Checkbox(label="Include Quran", value=True)
merge_results_chk = gr.Checkbox(label="Merge Results (Torah-Bible-Quran)", value=True)
strip_spaces = gr.Checkbox(label="Strip Spaces from Books", value=True)
strip_in_braces = gr.Checkbox(label="Strip Text in Braces from Books", value=True)
strip_diacritics_chk = gr.Checkbox(label="Strip Diacritics from Books", value=True)
translate_btn = gr.Button("Search with ELS")
# --- Output Components ---
markdown_output = gr.Dataframe(label="ELS Results")
most_frequent_phrase_output = gr.Textbox(label="Most Frequent Phrase in Network Search")
# --- Event Handlers ---
def update_date_words(selected_date, date_language_input):
return translate_date_to_words(selected_date, date_language_input)
def update_journal_sum(gematria_text, date_words_output):
sum_value = calculate_gematria_sum(gematria_text, date_words_output)
return sum_value, sum_value, sum_value
def update_rounds_combination(round_x, round_y):
return f"{int(round_x)},{int(round_y)}"
def update_step_half(float_step):
new_step = math.ceil(float_step / 2)
return new_step, float_step / 2
def update_step_double(float_step):
new_step = math.ceil(float_step * 2)
return new_step, float_step * 2
def perform_search(step, rounds_combination, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk, merge_results, include_torah, include_bible, include_quran, gematria_text, date_words_output):
els_results = perform_els_search(step, rounds_combination, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk, merge_results, include_torah, include_bible, include_quran)
# --- Network Search Integration ---
df_data = []
for result in els_results:
gematria_sum = calculate_gematria(result['match'])
max_words = len(result['match'].split())
matching_phrases = search_gematria_in_db(gematria_sum, max_words)
most_frequent_phrase = get_most_frequent_phrase(matching_phrases)
# Add data to the list for DataFrame creation
df_data.append({
'book': result['book'],
'chapter': result['chapter'],
'verse': result['verse'],
'match': result['match'],
'Most Frequent Phrase': most_frequent_phrase
})
# Create DataFrame
df = pd.DataFrame(df_data)
df.index = range(1, len(df) + 1)
df.reset_index(inplace=True)
df.rename(columns={'index': 'Result Number'}, inplace=True)
return df, df['Most Frequent Phrase'].iloc[0] if not df.empty else None
# --- Event Triggers ---
round_x.change(update_rounds_combination, inputs=[round_x, round_y], outputs=rounds_combination)
round_y.change(update_rounds_combination, inputs=[round_x, round_y], outputs=rounds_combination)
selected_date.change(update_date_words, inputs=[selected_date, date_language_input], outputs=[date_words_output])
date_language_input.change(update_date_words, inputs=[selected_date, date_language_input], outputs=[date_words_output])
gematria_text.change(update_journal_sum, inputs=[gematria_text, date_words_output], outputs=[gematria_result, step, float_step])
date_words_output.change(update_journal_sum, inputs=[gematria_text, date_words_output], outputs=[gematria_result, step, float_step])
half_step_btn.click(update_step_half, inputs=[float_step], outputs=[step, float_step])
double_step_btn.click(update_step_double, inputs=[float_step], outputs=[step, float_step])
translate_btn.click(
perform_search,
inputs=[step, rounds_combination, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk, merge_results_chk, include_torah_chk, include_bible_chk, include_quran_chk, gematria_text, date_words_output],
outputs=[markdown_output, most_frequent_phrase_output]
)
app.load(
update_date_words,
inputs=[selected_date, date_language_input],
outputs=[date_words_output]
)
app.load(
update_journal_sum,
inputs=[gematria_text, date_words_output],
outputs=[gematria_result, step, float_step]
)
if __name__ == "__main__":
app.launch(share=False)