neuralworm commited on
Commit
34bc94c
·
verified ·
1 Parent(s): 8adbf8c

revert to refactor

Browse files
Files changed (1) hide show
  1. app.py +20 -41
app.py CHANGED
@@ -8,7 +8,6 @@ from util import process_json_files
8
  from gematria import calculate_gematria
9
  from deep_translator import GoogleTranslator, exceptions
10
  from urllib.parse import quote_plus
11
- from tqdm import tqdm
12
 
13
  # Set up logging
14
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
@@ -57,8 +56,7 @@ def populate_database(tanach_texts, max_phrase_length=1):
57
  logging.info("Populating database...")
58
  cursor = conn.cursor()
59
 
60
- for book_id in tqdm(range(1, len(tanach_texts) + 1), desc="Populating database", leave=False):
61
- book_data = tanach_texts[book_id]
62
  # Check if the book is already processed for this max_phrase_length
63
  cursor.execute('''SELECT max_phrase_length FROM processed_books WHERE book = ?''', (book_id,))
64
  result = cursor.fetchone()
@@ -73,7 +71,7 @@ def populate_database(tanach_texts, max_phrase_length=1):
73
  continue
74
 
75
  title = book_data.get('title', 'Unknown')
76
- book_names[book_id] = title
77
 
78
  chapters = book_data['text']
79
  for chapter_id, chapter in enumerate(chapters):
@@ -138,7 +136,7 @@ def get_translation(phrase):
138
  def translate_and_store(phrase):
139
  """Translates a Hebrew phrase to English using Google Translate and handles potential errors."""
140
  global translator
141
- max_retries = 3
142
  retries = 0
143
 
144
  while retries < max_retries:
@@ -147,7 +145,7 @@ def translate_and_store(phrase):
147
  logging.debug(f"Translated phrase: {translation}")
148
  return translation
149
  except (exceptions.TranslationNotFound, exceptions.NotValidPayload,
150
- exceptions.ServerException, exceptions.RequestError, requests.exceptions.ConnectionError) as e:
151
  retries += 1
152
  logging.warning(f"Error translating phrase '{phrase}': {e}. Retrying... ({retries}/{max_retries})")
153
 
@@ -165,11 +163,11 @@ def search_gematria_in_db(gematria_sum):
165
  logging.debug(f"Found {len(results)} matching phrases for Gematria: {gematria_sum}")
166
  return results
167
 
168
- def gematria_search_interface(phrase, progress_bar):
169
  """The main function for the Gradio interface."""
170
  if not phrase.strip():
171
- return "Please enter a phrase.", progress_bar
172
-
173
  global conn, book_names
174
  conn = sqlite3.connect('gematria.db')
175
  cursor = conn.cursor()
@@ -179,7 +177,7 @@ def gematria_search_interface(phrase, progress_bar):
179
 
180
  matching_phrases = search_gematria_in_db(phrase_gematria)
181
  if not matching_phrases:
182
- return "No matching phrases found.", progress_bar
183
 
184
  # Sort results by book, chapter, and verse
185
  sorted_phrases = sorted(matching_phrases, key=lambda x: (x[1], x[2], x[3]))
@@ -240,7 +238,7 @@ def gematria_search_interface(phrase, progress_bar):
240
  </style>
241
  """
242
 
243
- return style + "\n".join(results), progress_bar
244
 
245
  def flatten_text(text):
246
  """Helper function to flatten nested lists into a single list."""
@@ -254,40 +252,21 @@ def run_app():
254
  initialize_translator()
255
 
256
  # Pre-populate the database
257
- tanach_texts = process_json_files(1, 39)
258
- populate_database(tanach_texts, max_phrase_length=1)
259
- tanach_texts = process_json_files(27, 27)
260
  populate_database(tanach_texts, max_phrase_length=12)
 
 
261
 
262
- progress_bar = gr.Progress(label="Database Population", visible=False)
263
- with gr.Blocks() as iface:
264
- with gr.Row():
265
- phrase_input = gr.Textbox(label="Enter phrase")
266
- results_output = gr.HTML(label="Results")
267
- iface.launch()
268
-
269
- # Create a hidden interface for populating the database with a progress bar
270
- hidden_iface = gr.Interface(
271
- fn=populate_database,
272
- inputs=[gr.Textbox(label="Tanach texts"), gr.Textbox(label="Max phrase length")],
273
- outputs=[progress_bar],
274
- title="Populate database",
275
- description="Populate the database with phrases from the Tanach.",
276
- live=False,
277
- allow_flagging="never",
278
- visible=False
279
- )
280
-
281
- # Trigger the database population when the main interface is launched
282
- iface.launch(inline=False)
283
- hidden_iface.launch(inline=False, share=False, server_port=8081) # Hidden interface
284
-
285
- # Run the search function when the "Search" button is clicked
286
- phrase_input.change(
287
  fn=gematria_search_interface,
288
- inputs=[phrase_input, progress_bar],
289
- outputs=[results_output, progress_bar]
 
 
 
 
290
  )
 
291
 
292
  if __name__ == "__main__":
293
  run_app()
 
8
  from gematria import calculate_gematria
9
  from deep_translator import GoogleTranslator, exceptions
10
  from urllib.parse import quote_plus
 
11
 
12
  # Set up logging
13
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
56
  logging.info("Populating database...")
57
  cursor = conn.cursor()
58
 
59
+ for book_id, book_data in tanach_texts.items():
 
60
  # Check if the book is already processed for this max_phrase_length
61
  cursor.execute('''SELECT max_phrase_length FROM processed_books WHERE book = ?''', (book_id,))
62
  result = cursor.fetchone()
 
71
  continue
72
 
73
  title = book_data.get('title', 'Unknown')
74
+ book_names[book_id] = title
75
 
76
  chapters = book_data['text']
77
  for chapter_id, chapter in enumerate(chapters):
 
136
  def translate_and_store(phrase):
137
  """Translates a Hebrew phrase to English using Google Translate and handles potential errors."""
138
  global translator
139
+ max_retries = 3
140
  retries = 0
141
 
142
  while retries < max_retries:
 
145
  logging.debug(f"Translated phrase: {translation}")
146
  return translation
147
  except (exceptions.TranslationNotFound, exceptions.NotValidPayload,
148
+ exceptions.ServerException, exceptions.RequestError, requests.exceptions.ConnectionError) as e:
149
  retries += 1
150
  logging.warning(f"Error translating phrase '{phrase}': {e}. Retrying... ({retries}/{max_retries})")
151
 
 
163
  logging.debug(f"Found {len(results)} matching phrases for Gematria: {gematria_sum}")
164
  return results
165
 
166
+ def gematria_search_interface(phrase):
167
  """The main function for the Gradio interface."""
168
  if not phrase.strip():
169
+ return "Please enter a phrase."
170
+
171
  global conn, book_names
172
  conn = sqlite3.connect('gematria.db')
173
  cursor = conn.cursor()
 
177
 
178
  matching_phrases = search_gematria_in_db(phrase_gematria)
179
  if not matching_phrases:
180
+ return "No matching phrases found."
181
 
182
  # Sort results by book, chapter, and verse
183
  sorted_phrases = sorted(matching_phrases, key=lambda x: (x[1], x[2], x[3]))
 
238
  </style>
239
  """
240
 
241
+ return style + "\n".join(results)
242
 
243
  def flatten_text(text):
244
  """Helper function to flatten nested lists into a single list."""
 
252
  initialize_translator()
253
 
254
  # Pre-populate the database
255
+ tanach_texts = process_json_files(1, 39)
 
 
256
  populate_database(tanach_texts, max_phrase_length=12)
257
+ tanach_texts = process_json_files(27, 27)
258
+ populate_database(tanach_texts, max_phrase_length=24)
259
 
260
+ iface = gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  fn=gematria_search_interface,
262
+ inputs=gr.Textbox(label="Enter phrase"),
263
+ outputs=gr.HTML(label="Results"),
264
+ title="Gematria Search in Tanach",
265
+ description="Search for phrases in the Tanach that have the same Gematria value.",
266
+ live=False,
267
+ allow_flagging="never"
268
  )
269
+ iface.launch()
270
 
271
  if __name__ == "__main__":
272
  run_app()