neuralworm commited on
Commit
2b9dfff
·
1 Parent(s): e4ee4df

patch app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -64
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import logging
2
-
3
  logger = logging.getLogger(__name__)
4
  logging.basicConfig(level=logging.DEBUG)
5
 
@@ -25,7 +24,6 @@ from typing import List, Tuple
25
  DATABASE_FILE = 'gematria.db'
26
  MAX_PHRASE_LENGTH_LIMIT = 20
27
 
28
-
29
  # --- Database Initialization ---
30
  def initialize_database():
31
  global conn
@@ -40,7 +38,7 @@ def initialize_database():
40
  chapter INTEGER,
41
  verse INTEGER,
42
  phrase_length INTEGER,
43
- word_position TEXT,
44
  PRIMARY KEY (gematria_sum, words, book, chapter, verse, word_position)
45
  )
46
  ''')
@@ -56,31 +54,27 @@ def initialize_database():
56
  ''')
57
  conn.commit()
58
 
59
-
60
  # --- Initialize Database ---
61
  initialize_database()
62
 
63
-
64
  # --- Helper Functions (from Network app.py) ---
65
  def flatten_text(text: List) -> str:
66
  if isinstance(text, list):
67
  return " ".join(flatten_text(item) if isinstance(item, list) else item for item in text)
68
  return text
69
 
70
-
71
  def search_gematria_in_db(gematria_sum: int, max_words: int) -> List[Tuple[str, str, int, int, int, str]]:
72
  global conn
73
  with sqlite3.connect(DATABASE_FILE) as conn:
74
  cursor = conn.cursor()
75
  cursor.execute('''
76
- SELECT words, book, chapter, verse, phrase_length, word_position
77
- FROM results
78
  WHERE gematria_sum = ? AND phrase_length <= ?
79
  ''', (gematria_sum, max_words))
80
  results = cursor.fetchall()
81
  return results
82
 
83
-
84
  def get_most_frequent_phrase(results):
85
  phrase_counts = defaultdict(int)
86
  for words, book, chapter, verse, phrase_length, word_position in results:
@@ -88,18 +82,16 @@ def get_most_frequent_phrase(results):
88
  most_frequent_phrase = max(phrase_counts, key=phrase_counts.get) if phrase_counts else None # Handle empty results
89
  return most_frequent_phrase
90
 
91
-
92
  # --- Functions from BOS app.py ---
93
  def create_language_dropdown(label, default_value='en', show_label=True):
94
  languages = GoogleTranslator(source='en', target='en').get_supported_languages(as_dict=True)
95
  return gr.Dropdown(
96
- choices=list(languages.keys()),
97
- label=label,
98
- value=default_value,
99
- show_label=show_label
100
  )
101
 
102
-
103
  def calculate_gematria_sum(text, date_words):
104
  if text or date_words:
105
  combined_input = f"{text} {date_words}"
@@ -112,9 +104,7 @@ def calculate_gematria_sum(text, date_words):
112
  else:
113
  return None
114
 
115
-
116
- def perform_els_search(step, rounds_combination, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk,
117
- merge_results, include_torah, include_bible, include_quran):
118
  if step == 0 or rounds_combination == "0,0":
119
  return None
120
 
@@ -123,19 +113,13 @@ def perform_els_search(step, rounds_combination, tlang, strip_spaces, strip_in_b
123
  quran_results = []
124
 
125
  if include_torah:
126
- torah_results.extend(
127
- torah.process_json_files(1, 39, step, rounds_combination, 0, tlang, strip_spaces, strip_in_braces,
128
- strip_diacritics))
129
 
130
  if include_bible:
131
- bible_results.extend(
132
- bible.process_json_files(40, 66, step, rounds_combination, 0, tlang, strip_spaces, strip_in_braces,
133
- strip_diacritics))
134
 
135
  if include_quran:
136
- quran_results.extend(
137
- quran.process_json_files(1, 114, step, rounds_combination, 0, tlang, strip_spaces, strip_in_braces,
138
- strip_diacritics))
139
 
140
  if merge_results:
141
  results = []
@@ -152,14 +136,12 @@ def perform_els_search(step, rounds_combination, tlang, strip_spaces, strip_in_b
152
 
153
  return results
154
 
155
-
156
- def generate_json_dump(start, end, step, rounds_combination, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk,
157
- search_phrase, results_df, start_date, end_date):
158
- """Generates the JSON dump with configuration, date range, and results."""
159
  config = {
160
  "Start Book": start,
161
  "End Book": end,
162
- "Step": step,
163
  "Rounds": rounds_combination,
164
  "Target Language": tlang,
165
  "Strip Spaces": strip_spaces,
@@ -178,7 +160,6 @@ def generate_json_dump(start, end, step, rounds_combination, tlang, strip_spaces
178
  logger.info(f"Generated JSON dump: {result}")
179
  return json.dumps(result, indent=4, ensure_ascii=False)
180
 
181
-
182
  def download_json_file(config_json, step, rounds_combination, strip_spaces, strip_in_braces, strip_diacritics_chk):
183
  """Downloads the JSON config file with a descriptive name."""
184
  filename_suffix = ""
@@ -203,8 +184,7 @@ with gr.Blocks() as app:
203
 
204
  with gr.Row():
205
  tlang = create_language_dropdown("Target Language for Translation", default_value='english')
206
- date_language_input = create_language_dropdown("Language of the person/topic (optional) (Date Word Language)",
207
- default_value='english')
208
 
209
  with gr.Row():
210
  gematria_text = gr.Textbox(label="Name and/or Topic (required)", value="Hans Albert Einstein")
@@ -236,51 +216,45 @@ with gr.Blocks() as app:
236
 
237
  # --- Output Components ---
238
  markdown_output = gr.Dataframe(label="ELS Results")
239
- most_frequent_phrase_output = gr.Textbox(label="Most Frequent Phrase in Network Search")
240
  json_output = gr.Textbox(label="JSON Configuration Output")
241
  json_download_btn = gr.Button("Prepare .json for Download")
242
  json_file = gr.File(label="Download Config JSON", file_count="single")
243
 
244
-
245
  # --- Event Handlers ---
246
  def update_journal_sum(gematria_text, date_words_output):
247
  sum_value = calculate_gematria_sum(gematria_text, date_words_output)
248
  return sum_value, sum_value, sum_value
249
 
250
-
251
  def update_rounds_combination(round_x, round_y):
252
  return f"{int(round_x)},{int(round_y)}"
253
 
254
-
255
  def update_step_half(float_step):
256
  new_step = math.ceil(float_step / 2)
257
  return new_step, float_step / 2
258
 
259
-
260
  def update_step_double(float_step):
261
  new_step = math.ceil(float_step * 2)
262
  return new_step, float_step * 2
263
 
264
-
265
- def perform_search(start_date, end_date, date_language_input, step, rounds_combination, tlang, strip_spaces,
266
- strip_in_braces, strip_diacritics_chk, merge_results, include_torah, include_bible,
267
- include_quran, gematria_text):
268
  all_results = []
269
  delta = timedelta(days=1)
270
  current_date = start_date
271
 
 
 
272
  while current_date <= end_date:
273
  date_words_output = translate_date_to_words(current_date, date_language_input)
274
  journal_sum, _, _ = update_journal_sum(gematria_text, date_words_output)
275
- step = journal_sum
276
 
277
- els_results = perform_els_search(step, rounds_combination, tlang, strip_spaces, strip_in_braces,
278
- strip_diacritics_chk, merge_results, include_torah, include_bible,
279
- include_quran)
280
 
281
  # --- Network Search Integration ---
282
  updated_els_results = []
283
  for result in els_results:
 
284
  try:
285
  gematria_sum = calculate_gematria(result['result_text'])
286
  except KeyError as e:
@@ -301,8 +275,7 @@ with gr.Blocks() as app:
301
  most_frequent_phrase = get_most_frequent_phrase(matching_phrases)
302
  else:
303
  # Sort initial results by word count and take the first phrase
304
- sorted_results = sorted(search_gematria_in_db(gematria_sum, max_words_limit),
305
- key=lambda x: len(x[0].split()))
306
  most_frequent_phrase = sorted_results[0][0] if sorted_results else ""
307
 
308
  # Add most frequent phrase, date, and date_words to the result dictionary
@@ -325,18 +298,14 @@ with gr.Blocks() as app:
325
  most_frequent_phrase = max(set(all_phrases), key=all_phrases.count) if all_phrases else ""
326
 
327
  # Generate JSON output
328
- search_phrase = f"{gematria_text}" # Removed date_words_output as it's now included in each result
329
- config_json = generate_json_dump(1, 180, step, rounds_combination, tlang, strip_spaces, strip_in_braces,
330
- strip_diacritics_chk, search_phrase, df, start_date, end_date)
331
-
332
- return df, most_frequent_phrase, config_json
333
 
 
334
 
335
- def handle_json_download(config_json, step, rounds_combination, strip_spaces, strip_in_braces,
336
- strip_diacritics_chk):
337
  """Handles the download of the JSON config file."""
338
- return download_json_file(config_json, step, rounds_combination, strip_spaces, strip_in_braces,
339
- strip_diacritics_chk)
340
 
341
 
342
  # --- Event Triggers ---
@@ -348,10 +317,8 @@ with gr.Blocks() as app:
348
 
349
  translate_btn.click(
350
  perform_search,
351
- inputs=[start_date, end_date, date_language_input, step, rounds_combination, tlang, strip_spaces,
352
- strip_in_braces, strip_diacritics_chk, merge_results_chk, include_torah_chk, include_bible_chk,
353
- include_quran_chk, gematria_text],
354
- outputs=[markdown_output, most_frequent_phrase_output, json_output]
355
  )
356
 
357
  json_download_btn.click(
@@ -361,4 +328,4 @@ with gr.Blocks() as app:
361
  )
362
 
363
  if __name__ == "__main__":
364
- app.launch(share=False)
 
1
  import logging
 
2
  logger = logging.getLogger(__name__)
3
  logging.basicConfig(level=logging.DEBUG)
4
 
 
24
  DATABASE_FILE = 'gematria.db'
25
  MAX_PHRASE_LENGTH_LIMIT = 20
26
 
 
27
  # --- Database Initialization ---
28
  def initialize_database():
29
  global conn
 
38
  chapter INTEGER,
39
  verse INTEGER,
40
  phrase_length INTEGER,
41
+ word_position TEXT,
42
  PRIMARY KEY (gematria_sum, words, book, chapter, verse, word_position)
43
  )
44
  ''')
 
54
  ''')
55
  conn.commit()
56
 
 
57
  # --- Initialize Database ---
58
  initialize_database()
59
 
 
60
  # --- Helper Functions (from Network app.py) ---
61
  def flatten_text(text: List) -> str:
62
  if isinstance(text, list):
63
  return " ".join(flatten_text(item) if isinstance(item, list) else item for item in text)
64
  return text
65
 
 
66
  def search_gematria_in_db(gematria_sum: int, max_words: int) -> List[Tuple[str, str, int, int, int, str]]:
67
  global conn
68
  with sqlite3.connect(DATABASE_FILE) as conn:
69
  cursor = conn.cursor()
70
  cursor.execute('''
71
+ SELECT words, book, chapter, verse, phrase_length, word_position
72
+ FROM results
73
  WHERE gematria_sum = ? AND phrase_length <= ?
74
  ''', (gematria_sum, max_words))
75
  results = cursor.fetchall()
76
  return results
77
 
 
78
  def get_most_frequent_phrase(results):
79
  phrase_counts = defaultdict(int)
80
  for words, book, chapter, verse, phrase_length, word_position in results:
 
82
  most_frequent_phrase = max(phrase_counts, key=phrase_counts.get) if phrase_counts else None # Handle empty results
83
  return most_frequent_phrase
84
 
 
85
  # --- Functions from BOS app.py ---
86
  def create_language_dropdown(label, default_value='en', show_label=True):
87
  languages = GoogleTranslator(source='en', target='en').get_supported_languages(as_dict=True)
88
  return gr.Dropdown(
89
+ choices=list(languages.keys()),
90
+ label=label,
91
+ value=default_value,
92
+ show_label=show_label
93
  )
94
 
 
95
  def calculate_gematria_sum(text, date_words):
96
  if text or date_words:
97
  combined_input = f"{text} {date_words}"
 
104
  else:
105
  return None
106
 
107
+ def perform_els_search(step, rounds_combination, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk, merge_results, include_torah, include_bible, include_quran):
 
 
108
  if step == 0 or rounds_combination == "0,0":
109
  return None
110
 
 
113
  quran_results = []
114
 
115
  if include_torah:
116
+ torah_results.extend(torah.process_json_files(1, 39, step, rounds_combination, 0, tlang, strip_spaces, strip_in_braces, strip_diacritics))
 
 
117
 
118
  if include_bible:
119
+ bible_results.extend(bible.process_json_files(40, 66, step, rounds_combination, 0, tlang, strip_spaces, strip_in_braces, strip_diacritics))
 
 
120
 
121
  if include_quran:
122
+ quran_results.extend(quran.process_json_files(1, 114, step, rounds_combination, 0, tlang, strip_spaces, strip_in_braces, strip_diacritics))
 
 
123
 
124
  if merge_results:
125
  results = []
 
136
 
137
  return results
138
 
139
+ def generate_json_dump(start, end, initial_step, rounds_combination, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk, search_phrase, results_df, start_date, end_date):
140
+ """Generates the JSON dump with configuration, date range, and results, including the initial step."""
 
 
141
  config = {
142
  "Start Book": start,
143
  "End Book": end,
144
+ "Step": initial_step, # Use initial_step here
145
  "Rounds": rounds_combination,
146
  "Target Language": tlang,
147
  "Strip Spaces": strip_spaces,
 
160
  logger.info(f"Generated JSON dump: {result}")
161
  return json.dumps(result, indent=4, ensure_ascii=False)
162
 
 
163
  def download_json_file(config_json, step, rounds_combination, strip_spaces, strip_in_braces, strip_diacritics_chk):
164
  """Downloads the JSON config file with a descriptive name."""
165
  filename_suffix = ""
 
184
 
185
  with gr.Row():
186
  tlang = create_language_dropdown("Target Language for Translation", default_value='english')
187
+ date_language_input = create_language_dropdown("Language of the person/topic (optional) (Date Word Language)", default_value='english')
 
188
 
189
  with gr.Row():
190
  gematria_text = gr.Textbox(label="Name and/or Topic (required)", value="Hans Albert Einstein")
 
216
 
217
  # --- Output Components ---
218
  markdown_output = gr.Dataframe(label="ELS Results")
219
+ #most_frequent_phrase_output = gr.Textbox(label="Most Frequent Phrase in Network Search")
220
  json_output = gr.Textbox(label="JSON Configuration Output")
221
  json_download_btn = gr.Button("Prepare .json for Download")
222
  json_file = gr.File(label="Download Config JSON", file_count="single")
223
 
 
224
  # --- Event Handlers ---
225
  def update_journal_sum(gematria_text, date_words_output):
226
  sum_value = calculate_gematria_sum(gematria_text, date_words_output)
227
  return sum_value, sum_value, sum_value
228
 
 
229
  def update_rounds_combination(round_x, round_y):
230
  return f"{int(round_x)},{int(round_y)}"
231
 
 
232
  def update_step_half(float_step):
233
  new_step = math.ceil(float_step / 2)
234
  return new_step, float_step / 2
235
 
 
236
  def update_step_double(float_step):
237
  new_step = math.ceil(float_step * 2)
238
  return new_step, float_step * 2
239
 
240
+ def perform_search(start_date, end_date, date_language_input, step, rounds_combination, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk, merge_results, include_torah, include_bible, include_quran, gematria_text):
 
 
 
241
  all_results = []
242
  delta = timedelta(days=1)
243
  current_date = start_date
244
 
245
+ initial_step = step # Store the initial step value
246
+
247
  while current_date <= end_date:
248
  date_words_output = translate_date_to_words(current_date, date_language_input)
249
  journal_sum, _, _ = update_journal_sum(gematria_text, date_words_output)
250
+ step = journal_sum # Update step for each date
251
 
252
+ 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)
 
 
253
 
254
  # --- Network Search Integration ---
255
  updated_els_results = []
256
  for result in els_results:
257
+ logger.debug(f"Processing result: {result}") # Add debugging log
258
  try:
259
  gematria_sum = calculate_gematria(result['result_text'])
260
  except KeyError as e:
 
275
  most_frequent_phrase = get_most_frequent_phrase(matching_phrases)
276
  else:
277
  # Sort initial results by word count and take the first phrase
278
+ sorted_results = sorted(search_gematria_in_db(gematria_sum, max_words_limit), key=lambda x: len(x[0].split()))
 
279
  most_frequent_phrase = sorted_results[0][0] if sorted_results else ""
280
 
281
  # Add most frequent phrase, date, and date_words to the result dictionary
 
298
  most_frequent_phrase = max(set(all_phrases), key=all_phrases.count) if all_phrases else ""
299
 
300
  # Generate JSON output
301
+ search_phrase = f"{gematria_text}" # Removed date_words_output as it's now included in each result
302
+ config_json = generate_json_dump(1, 180, initial_step, rounds_combination, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk, search_phrase, df, start_date, end_date)
 
 
 
303
 
304
+ return df, config_json
305
 
306
+ def handle_json_download(config_json, step, rounds_combination, strip_spaces, strip_in_braces, strip_diacritics_chk):
 
307
  """Handles the download of the JSON config file."""
308
+ return download_json_file(config_json, step, rounds_combination, strip_spaces, strip_in_braces, strip_diacritics_chk)
 
309
 
310
 
311
  # --- Event Triggers ---
 
317
 
318
  translate_btn.click(
319
  perform_search,
320
+ inputs=[start_date, end_date, date_language_input, 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],
321
+ outputs=[markdown_output, json_output]
 
 
322
  )
323
 
324
  json_download_btn.click(
 
328
  )
329
 
330
  if __name__ == "__main__":
331
+ app.launch(share=False)