neuralworm commited on
Commit
cb87c06
·
1 Parent(s): 63ae283

els cache with function parameters

Browse files
Files changed (3) hide show
  1. app.py +82 -28
  2. els_cache.db +0 -3
  3. els_cache.db.initial +0 -3
app.py CHANGED
@@ -44,6 +44,20 @@ MAX_PHRASE_LENGTH_LIMIT = 20
44
  ELS_CACHE_DB = "els_cache.db"
45
  DATABASE_TIMEOUT = 60
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  # --- Database Initialization ---
48
  def initialize_database():
49
  global conn
@@ -58,7 +72,7 @@ def initialize_database():
58
  chapter INTEGER,
59
  verse INTEGER,
60
  phrase_length INTEGER,
61
- word_position TEXT,
62
  PRIMARY KEY (gematria_sum, words, book, chapter, verse, word_position)
63
  )
64
  ''')
@@ -80,25 +94,53 @@ initialize_database()
80
  # --- ELS Cache Functions ---
81
  def create_els_cache_table():
82
  with sqlite3.connect(ELS_CACHE_DB) as conn:
83
- conn.execute('''
84
- CREATE TABLE IF NOT EXISTS els_cache (
85
- query_hash TEXT PRIMARY KEY,
86
- results TEXT
87
- )
88
- ''')
89
-
90
- def get_query_hash(func, *args, **kwargs):
91
- key = (func.__name__, args, tuple(sorted(kwargs.items())))
 
 
 
 
 
 
92
  return hashlib.sha256(json.dumps(key).encode()).hexdigest()
93
 
94
 
95
  def cached_process_json_files(func, *args, **kwargs):
96
- query_hash = get_query_hash(func, *args, **kwargs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  try:
99
  with sqlite3.connect(ELS_CACHE_DB, timeout=DATABASE_TIMEOUT) as conn:
100
  cursor = conn.cursor()
101
- cursor.execute("SELECT results FROM els_cache WHERE query_hash = ?", (query_hash,))
 
102
  result = cursor.fetchone()
103
  if result:
104
  logger.info(f"Cache hit for query: {query_hash}")
@@ -112,13 +154,15 @@ def cached_process_json_files(func, *args, **kwargs):
112
  try:
113
  with sqlite3.connect(ELS_CACHE_DB, timeout=DATABASE_TIMEOUT) as conn:
114
  cursor = conn.cursor()
115
- cursor.execute("INSERT INTO els_cache (query_hash, results) VALUES (?, ?)", (query_hash, json.dumps(results)))
 
116
  conn.commit()
117
  except sqlite3.Error as e:
118
  logger.error(f"Database error caching results: {e}")
119
 
120
  return results
121
 
 
122
  # --- Helper Functions (from Network app.py) ---
123
  def flatten_text(text: List) -> str:
124
  if isinstance(text, list):
@@ -130,8 +174,8 @@ def search_gematria_in_db(gematria_sum: int, max_words: int) -> List[Tuple[str,
130
  with sqlite3.connect(DATABASE_FILE) as conn:
131
  cursor = conn.cursor()
132
  cursor.execute('''
133
- SELECT words, book, chapter, verse, phrase_length, word_position
134
- FROM results
135
  WHERE gematria_sum = ? AND phrase_length <= ?
136
  ''', (gematria_sum, max_words))
137
  results = cursor.fetchall()
@@ -174,40 +218,50 @@ def perform_els_search(step, rounds_combination, tlang, strip_spaces, strip_in_b
174
  length = 0
175
 
176
  selected_language_long = tlang # From the Gradio dropdown (long form)
177
- tlang = LANGUAGES_SUPPORTED.get(selected_language_long) #Get the short code.
178
- if tlang is None: # Handle unsupported languages
 
179
  tlang = "en"
180
- logger.warning(f"Unsupported language selected: {selected_language_long}. Defaulting to English (en).")
 
181
 
 
182
  if include_torah:
183
- logger.debug(f"Arguments for Torah: {(1, 39, step, rounds_combination, length, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk)}")
184
- results["Torah"] = cached_process_json_files(torah.process_json_files, 1, 39, step, rounds_combination, length, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk)
 
 
185
  else:
186
  results["Torah"] = []
187
 
188
  if include_bible:
189
- results["Bible"] = cached_process_json_files(bible.process_json_files, 40, 66, step, rounds_combination, length, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk)
 
190
  else:
191
  results["Bible"] = []
192
 
193
  if include_quran:
194
- results["Quran"] = cached_process_json_files(quran.process_json_files, 1, 114, step, rounds_combination, length, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk)
 
195
  else:
196
  results["Quran"] = []
197
 
198
  if include_hindu:
199
- results["Rig Veda"] = cached_process_json_files(hindu.process_json_files, 1, 10, step, rounds_combination, length, tlang, False, strip_in_braces, strip_diacritics_chk)
 
200
  else:
201
  results["Rig Veda"] = []
202
 
203
  if include_tripitaka:
204
- results["Tripitaka"] = cached_process_json_files(tripitaka.process_json_files, 1, 52, step, rounds_combination, length, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk)
 
205
  else:
206
  results["Tripitaka"] = []
207
 
208
  return results
209
 
210
 
 
211
  def add_24h_projection(results_dict): #Now takes a dictionary of results
212
  for book_name, results in results_dict.items(): # Iterate per book
213
  num_results = len(results)
@@ -304,14 +358,14 @@ with gr.Blocks() as app:
304
 
305
  with gr.Row():
306
  step = gr.Number(label="Jump Width (Steps) for ELS")
307
- float_step = gr.Number(visible=False, value=1)
308
  half_step_btn = gr.Button("Steps / 2")
309
  double_step_btn = gr.Button("Steps * 2")
310
-
311
  with gr.Column():
312
  round_x = gr.Number(label="Round (1)", value=1)
313
  round_y = gr.Number(label="Round (2)", value=-1)
314
-
315
  rounds_combination = gr.Textbox(label="Combined Rounds", value="1,-1")
316
 
317
  with gr.Row():
@@ -320,7 +374,7 @@ with gr.Blocks() as app:
320
  include_quran_chk = gr.Checkbox(label="Include Quran", value=True)
321
  include_hindu_chk = gr.Checkbox(label="Include Rigveda", value=False)
322
  include_tripitaka_chk = gr.Checkbox(label="Include Tripitaka", value=False)
323
-
324
  strip_spaces = gr.Checkbox(label="Strip Spaces from Books", value=True)
325
  strip_in_braces = gr.Checkbox(label="Strip Text in Braces from Books", value=True)
326
  strip_diacritics_chk = gr.Checkbox(label="Strip Diacritics from Books", value=True)
 
44
  ELS_CACHE_DB = "els_cache.db"
45
  DATABASE_TIMEOUT = 60
46
 
47
+ # --- ELS Cache Functions ---
48
+ def create_els_cache_table():
49
+ if not os.path.exists(ELS_CACHE_DB):
50
+ with sqlite3.connect(ELS_CACHE_DB) as conn:
51
+ conn.execute('''
52
+ CREATE TABLE els_cache (
53
+ query_hash TEXT PRIMARY KEY,
54
+ function_name TEXT,
55
+ args TEXT,
56
+ kwargs TEXT,
57
+ results TEXT
58
+ )
59
+ ''')
60
+
61
  # --- Database Initialization ---
62
  def initialize_database():
63
  global conn
 
72
  chapter INTEGER,
73
  verse INTEGER,
74
  phrase_length INTEGER,
75
+ word_position TEXT,
76
  PRIMARY KEY (gematria_sum, words, book, chapter, verse, word_position)
77
  )
78
  ''')
 
94
  # --- ELS Cache Functions ---
95
  def create_els_cache_table():
96
  with sqlite3.connect(ELS_CACHE_DB) as conn:
97
+ try:
98
+ conn.execute('''
99
+ CREATE TABLE IF NOT EXISTS els_cache (
100
+ query_hash TEXT PRIMARY KEY,
101
+ function_name TEXT,
102
+ args TEXT,
103
+ kwargs TEXT,
104
+ results TEXT
105
+ )
106
+ ''')
107
+ except sqlite3.OperationalError as e:
108
+ logger.error(f"Error creating table: {e}")
109
+
110
+ def get_query_hash(func, args, kwargs):
111
+ key = (func.__name__, args, kwargs)
112
  return hashlib.sha256(json.dumps(key).encode()).hexdigest()
113
 
114
 
115
  def cached_process_json_files(func, *args, **kwargs):
116
+ # Create a dictionary to store the parameters
117
+ params = {
118
+ "function": f"{func.__module__}.{func.__name__}"
119
+ }
120
+
121
+ # Add the positional arguments with their names
122
+ arg_names = func.__code__.co_varnames[:func.__code__.co_argcount]
123
+ for name, value in zip(arg_names, args):
124
+ params[name] = value
125
+
126
+ # Add the keyword arguments
127
+ for name, value in kwargs.items():
128
+ params[name] = value
129
+
130
+ # Convert the parameters to a JSON string
131
+ params_json = json.dumps(params)
132
+
133
+ # Use the parameters JSON string to generate the query hash
134
+ query_hash = get_query_hash(func, params_json, "")
135
+
136
+ # Ensure the table exists before any operations
137
+ create_els_cache_table()
138
 
139
  try:
140
  with sqlite3.connect(ELS_CACHE_DB, timeout=DATABASE_TIMEOUT) as conn:
141
  cursor = conn.cursor()
142
+ cursor.execute(
143
+ "SELECT results FROM els_cache WHERE query_hash = ?", (query_hash,))
144
  result = cursor.fetchone()
145
  if result:
146
  logger.info(f"Cache hit for query: {query_hash}")
 
154
  try:
155
  with sqlite3.connect(ELS_CACHE_DB, timeout=DATABASE_TIMEOUT) as conn:
156
  cursor = conn.cursor()
157
+ cursor.execute("INSERT INTO els_cache (query_hash, function_name, args, kwargs, results) VALUES (?, ?, ?, ?, ?)",
158
+ (query_hash, params["function"], params_json, json.dumps({}), json.dumps(results)))
159
  conn.commit()
160
  except sqlite3.Error as e:
161
  logger.error(f"Database error caching results: {e}")
162
 
163
  return results
164
 
165
+
166
  # --- Helper Functions (from Network app.py) ---
167
  def flatten_text(text: List) -> str:
168
  if isinstance(text, list):
 
174
  with sqlite3.connect(DATABASE_FILE) as conn:
175
  cursor = conn.cursor()
176
  cursor.execute('''
177
+ SELECT words, book, chapter, verse, phrase_length, word_position
178
+ FROM results
179
  WHERE gematria_sum = ? AND phrase_length <= ?
180
  ''', (gematria_sum, max_words))
181
  results = cursor.fetchall()
 
218
  length = 0
219
 
220
  selected_language_long = tlang # From the Gradio dropdown (long form)
221
+ # Get the short code.
222
+ tlang = LANGUAGES_SUPPORTED.get(selected_language_long)
223
+ if tlang is None: # Handle unsupported languages
224
  tlang = "en"
225
+ logger.warning(
226
+ f"Unsupported language selected: {selected_language_long}. Defaulting to English (en).")
227
 
228
+ # Cache Update: Pass parameters individually
229
  if include_torah:
230
+ logger.debug(
231
+ f"Arguments for Torah: {(1, 39, step, rounds_combination, length, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk)}")
232
+ results["Torah"] = cached_process_json_files(torah.process_json_files, 1, 39, step, rounds_combination, length,
233
+ tlang, strip_spaces, strip_in_braces, strip_diacritics_chk)
234
  else:
235
  results["Torah"] = []
236
 
237
  if include_bible:
238
+ results["Bible"] = cached_process_json_files(bible.process_json_files, 40, 66, step, rounds_combination, length,
239
+ tlang, strip_spaces, strip_in_braces, strip_diacritics_chk)
240
  else:
241
  results["Bible"] = []
242
 
243
  if include_quran:
244
+ results["Quran"] = cached_process_json_files(quran.process_json_files, 1, 114, step, rounds_combination, length,
245
+ tlang, strip_spaces, strip_in_braces, strip_diacritics_chk)
246
  else:
247
  results["Quran"] = []
248
 
249
  if include_hindu:
250
+ results["Rig Veda"] = cached_process_json_files(
251
+ hindu.process_json_files, 1, 10, step, rounds_combination, length, tlang, False, strip_in_braces, strip_diacritics_chk)
252
  else:
253
  results["Rig Veda"] = []
254
 
255
  if include_tripitaka:
256
+ results["Tripitaka"] = cached_process_json_files(
257
+ tripitaka.process_json_files, 1, 52, step, rounds_combination, length, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk)
258
  else:
259
  results["Tripitaka"] = []
260
 
261
  return results
262
 
263
 
264
+
265
  def add_24h_projection(results_dict): #Now takes a dictionary of results
266
  for book_name, results in results_dict.items(): # Iterate per book
267
  num_results = len(results)
 
358
 
359
  with gr.Row():
360
  step = gr.Number(label="Jump Width (Steps) for ELS")
361
+ float_step = gr.Number(visible=False, value=1)
362
  half_step_btn = gr.Button("Steps / 2")
363
  double_step_btn = gr.Button("Steps * 2")
364
+
365
  with gr.Column():
366
  round_x = gr.Number(label="Round (1)", value=1)
367
  round_y = gr.Number(label="Round (2)", value=-1)
368
+
369
  rounds_combination = gr.Textbox(label="Combined Rounds", value="1,-1")
370
 
371
  with gr.Row():
 
374
  include_quran_chk = gr.Checkbox(label="Include Quran", value=True)
375
  include_hindu_chk = gr.Checkbox(label="Include Rigveda", value=False)
376
  include_tripitaka_chk = gr.Checkbox(label="Include Tripitaka", value=False)
377
+
378
  strip_spaces = gr.Checkbox(label="Strip Spaces from Books", value=True)
379
  strip_in_braces = gr.Checkbox(label="Strip Text in Braces from Books", value=True)
380
  strip_diacritics_chk = gr.Checkbox(label="Strip Diacritics from Books", value=True)
els_cache.db DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:66db30b6eccaaaed442d806a1c1300f62238946ecc0bf09b77e9e385a4ad3458
3
- size 12288
 
 
 
 
els_cache.db.initial DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:66db30b6eccaaaed442d806a1c1300f62238946ecc0bf09b77e9e385a4ad3458
3
- size 12288