Spaces:
Running
Running
neuralworm
commited on
Commit
·
21790a4
1
Parent(s):
ccf23f2
add Tab 5
Browse files
app.py
CHANGED
@@ -29,6 +29,7 @@ import calendar
|
|
29 |
import translation_utils
|
30 |
import hashlib
|
31 |
import copy
|
|
|
32 |
|
33 |
translation_utils.create_translation_table()
|
34 |
|
@@ -1099,6 +1100,210 @@ with gr.Blocks() as app:
|
|
1099 |
outputs=[filtered_results_output_els]
|
1100 |
)
|
1101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1102 |
# --- Event Handlers ---
|
1103 |
|
1104 |
search_type.change(
|
|
|
29 |
import translation_utils
|
30 |
import hashlib
|
31 |
import copy
|
32 |
+
from collections import Counter
|
33 |
|
34 |
translation_utils.create_translation_table()
|
35 |
|
|
|
1100 |
outputs=[filtered_results_output_els]
|
1101 |
)
|
1102 |
|
1103 |
+
|
1104 |
+
with gr.Tab("Comprehensive ELS Search"):
|
1105 |
+
with gr.Row():
|
1106 |
+
start_date_comp = Calendar(type="datetime", label="Start Date")
|
1107 |
+
end_date_comp = Calendar(type="datetime", label="End Date")
|
1108 |
+
with gr.Row():
|
1109 |
+
names_input_comp = gr.Textbox(label="Names (one per line)", lines=5)
|
1110 |
+
with gr.Row():
|
1111 |
+
sub_oscillation_level_comp = gr.Number(label="Sub-Oscillation Level", precision=0, value=1)
|
1112 |
+
with gr.Row():
|
1113 |
+
include_torah_chk_comp = gr.Checkbox(label="Include Torah", value=True)
|
1114 |
+
include_bible_chk_comp = gr.Checkbox(label="Include Bible", value=True)
|
1115 |
+
include_quran_chk_comp = gr.Checkbox(label="Include Quran", value=True)
|
1116 |
+
include_hindu_chk_comp = gr.Checkbox(label="Include Rigveda", value=False)
|
1117 |
+
include_tripitaka_chk_comp = gr.Checkbox(label="Include Tripitaka", value=False)
|
1118 |
+
with gr.Row():
|
1119 |
+
translate_results_chk_comp = gr.Checkbox(label="Translate Results to English", value=False)
|
1120 |
+
with gr.Row():
|
1121 |
+
common_gematria_results = gr.JSON(label="Common Gematria Results")
|
1122 |
+
with gr.Row():
|
1123 |
+
perform_search_btn_comp = gr.Button("Perform Search")
|
1124 |
+
|
1125 |
+
|
1126 |
+
|
1127 |
+
def perform_comprehensive_els_search(
|
1128 |
+
start_date,
|
1129 |
+
end_date,
|
1130 |
+
names_input,
|
1131 |
+
sub_oscillation_level,
|
1132 |
+
include_torah,
|
1133 |
+
include_bible,
|
1134 |
+
include_quran,
|
1135 |
+
include_hindu,
|
1136 |
+
include_tripitaka,
|
1137 |
+
translate_results
|
1138 |
+
):
|
1139 |
+
names = [n.strip() for n in names_input.split("\n") if n.strip()]
|
1140 |
+
date_gematrias = perform_gematria_calculation_for_date_range(start_date, end_date)
|
1141 |
+
|
1142 |
+
all_results_by_date = {}
|
1143 |
+
|
1144 |
+
for date_str, date_data in date_gematrias.items():
|
1145 |
+
all_results_by_date[date_str] = {}
|
1146 |
+
for name in names:
|
1147 |
+
name_gem = calculate_gematria_sum(name, "")
|
1148 |
+
initial_gem = date_data["date_gematria"] + name_gem
|
1149 |
+
|
1150 |
+
base_results_list = []
|
1151 |
+
base_results = perform_els_search_for_gematria_sum(
|
1152 |
+
gematria_sum=initial_gem,
|
1153 |
+
include_torah=include_torah,
|
1154 |
+
include_bible=include_bible,
|
1155 |
+
include_quran=include_quran,
|
1156 |
+
include_hindu=include_hindu,
|
1157 |
+
include_tripitaka=include_tripitaka
|
1158 |
+
)
|
1159 |
+
for book_name, res_list in base_results.items():
|
1160 |
+
for one_res in res_list:
|
1161 |
+
if not isinstance(one_res, dict) or "result_sum" not in one_res:
|
1162 |
+
print(f"Warning: Invalid result format for {name} on {date_str}: {one_res}")
|
1163 |
+
continue
|
1164 |
+
|
1165 |
+
base_results_list.append({
|
1166 |
+
"book": book_name,
|
1167 |
+
"result": one_res,
|
1168 |
+
"subresults": []
|
1169 |
+
})
|
1170 |
+
|
1171 |
+
|
1172 |
+
if sub_oscillation_level > 0 and base_results_list:
|
1173 |
+
perform_sub_oscillation_search(
|
1174 |
+
base_results_list,
|
1175 |
+
initial_gem,
|
1176 |
+
sub_oscillation_level,
|
1177 |
+
include_torah,
|
1178 |
+
include_bible,
|
1179 |
+
include_quran,
|
1180 |
+
include_hindu,
|
1181 |
+
include_tripitaka
|
1182 |
+
)
|
1183 |
+
|
1184 |
+
all_results_by_date[date_str][name] = base_results_list
|
1185 |
+
|
1186 |
+
|
1187 |
+
common_results = {}
|
1188 |
+
for date_str, name_results in all_results_by_date.items():
|
1189 |
+
common_results[date_str] = {}
|
1190 |
+
sums_by_name = {}
|
1191 |
+
all_sums_count = Counter()
|
1192 |
+
for name, results in name_results.items():
|
1193 |
+
sums_by_name[name] = set()
|
1194 |
+
count_sums_recursive(results, all_sums_count)
|
1195 |
+
extract_sums_recursive(results, sums_by_name[name])
|
1196 |
+
|
1197 |
+
ordered_common_sums = [item for item, count in all_sums_count.most_common() if all(item in sums_by_name[n] for n in sums_by_name)]
|
1198 |
+
|
1199 |
+
for common_sum in ordered_common_sums:
|
1200 |
+
common_results[date_str][str(common_sum)] = {}
|
1201 |
+
for name, results in name_results.items():
|
1202 |
+
matching_items = []
|
1203 |
+
for item in results:
|
1204 |
+
if find_results_by_sum([item], common_sum):
|
1205 |
+
item_copy = copy.deepcopy(item)
|
1206 |
+
|
1207 |
+
if translate_results:
|
1208 |
+
translate_recursive(item_copy)
|
1209 |
+
|
1210 |
+
|
1211 |
+
item_copy["subresults"] = find_results_by_sum(item.get("subresults", []), common_sum)
|
1212 |
+
|
1213 |
+
if translate_results:
|
1214 |
+
for sub_item in item_copy["subresults"]:
|
1215 |
+
translate_recursive(sub_item)
|
1216 |
+
|
1217 |
+
matching_items.append(item_copy)
|
1218 |
+
|
1219 |
+
if matching_items:
|
1220 |
+
common_results[date_str][str(common_sum)][name] = matching_items
|
1221 |
+
|
1222 |
+
return common_results
|
1223 |
+
|
1224 |
+
def find_results_by_sum(results, target_sum):
|
1225 |
+
matches = []
|
1226 |
+
if isinstance(results, list):
|
1227 |
+
for item in results:
|
1228 |
+
matches.extend(find_results_by_sum(item, target_sum))
|
1229 |
+
elif isinstance(results, dict):
|
1230 |
+
if "result" in results and isinstance(results["result"], dict) and "result_sum" in results["result"]:
|
1231 |
+
try:
|
1232 |
+
result_sum = int(results["result"]["result_sum"])
|
1233 |
+
if result_sum == target_sum:
|
1234 |
+
matches.append(results)
|
1235 |
+
except (TypeError, ValueError) as e:
|
1236 |
+
print(f"Error comparing result_sum: {e}, Data: {results}")
|
1237 |
+
if "subresults" in results:
|
1238 |
+
matches.extend(find_results_by_sum(results["subresults"], target_sum))
|
1239 |
+
|
1240 |
+
return matches
|
1241 |
+
|
1242 |
+
|
1243 |
+
def extract_sums_recursive(data, sums_set):
|
1244 |
+
if isinstance(data, list):
|
1245 |
+
for item in data:
|
1246 |
+
extract_sums_recursive(item, sums_set)
|
1247 |
+
elif isinstance(data, dict):
|
1248 |
+
if "result" in data and isinstance(data["result"], dict) and "result_sum" in data["result"]:
|
1249 |
+
try:
|
1250 |
+
result_sum = int(data["result"]["result_sum"])
|
1251 |
+
sums_set.add(result_sum)
|
1252 |
+
except (TypeError, ValueError) as e:
|
1253 |
+
print(f"Error extracting result_sum: {e}, Data: {data}")
|
1254 |
+
|
1255 |
+
if "subresults" in data:
|
1256 |
+
extract_sums_recursive(data["subresults"], sums_set)
|
1257 |
+
|
1258 |
+
|
1259 |
+
def count_sums_recursive(data, sums_counter):
|
1260 |
+
if isinstance(data, list):
|
1261 |
+
for item in data:
|
1262 |
+
count_sums_recursive(item, sums_counter)
|
1263 |
+
elif isinstance(data, dict):
|
1264 |
+
if "result" in data and isinstance(data["result"], dict) and "result_sum" in data["result"]:
|
1265 |
+
try:
|
1266 |
+
result_sum = int(data["result"]["result_sum"])
|
1267 |
+
sums_counter[result_sum] += 1
|
1268 |
+
except (TypeError, ValueError) as e:
|
1269 |
+
print(f"Error counting result_sum: {e}, Data: {data}")
|
1270 |
+
if "subresults" in data:
|
1271 |
+
count_sums_recursive(data["subresults"], sums_counter)
|
1272 |
+
|
1273 |
+
|
1274 |
+
|
1275 |
+
def translate_recursive(item):
|
1276 |
+
if "result" in item and isinstance(item["result"], dict) and "result_text" in item["result"]:
|
1277 |
+
text = item["result"].get("result_text", "")
|
1278 |
+
source_lang = item["result"].get("source_language", "auto")
|
1279 |
+
if text:
|
1280 |
+
translated_text = translation_utils.get_translation(text, "en", source_lang)[0]
|
1281 |
+
item["result"]["translated_text"] = translated_text
|
1282 |
+
|
1283 |
+
if "subresults" in item and isinstance(item["subresults"], list):
|
1284 |
+
for sub in item["subresults"]:
|
1285 |
+
translate_recursive(sub)
|
1286 |
+
|
1287 |
+
|
1288 |
+
|
1289 |
+
|
1290 |
+
perform_search_btn_comp.click(
|
1291 |
+
perform_comprehensive_els_search,
|
1292 |
+
inputs=[
|
1293 |
+
start_date_comp,
|
1294 |
+
end_date_comp,
|
1295 |
+
names_input_comp,
|
1296 |
+
sub_oscillation_level_comp,
|
1297 |
+
include_torah_chk_comp,
|
1298 |
+
include_bible_chk_comp,
|
1299 |
+
include_quran_chk_comp,
|
1300 |
+
include_hindu_chk_comp,
|
1301 |
+
include_tripitaka_chk_comp,
|
1302 |
+
translate_results_chk_comp
|
1303 |
+
],
|
1304 |
+
outputs=[common_gematria_results]
|
1305 |
+
)
|
1306 |
+
|
1307 |
# --- Event Handlers ---
|
1308 |
|
1309 |
search_type.change(
|