cryptocalypse commited on
Commit
cea802d
1 Parent(s): 11096c7

torah julian lib

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. lib/torah.py +51 -0
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from lib.gematria import calculate_gematria, strip_diacritics
4
-
5
 
6
  def translate_texts(start, end, step, length=0, tlang="en", spaces_include=False, strip_in_braces=True, strip_diacritics=True):
7
  results = torah.process_json_files(start, end, step, length, tlang, spaces_include, strip_in_braces, strip_diacritics)
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from lib.gematria import calculate_gematria, strip_diacritics
4
+ import lib.torah as torah
5
 
6
  def translate_texts(start, end, step, length=0, tlang="en", spaces_include=False, strip_in_braces=True, strip_diacritics=True):
7
  results = torah.process_json_files(start, end, step, length, tlang, spaces_include, strip_in_braces, strip_diacritics)
lib/torah.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import re
4
+ from deep_translator import GoogleTranslator
5
+
6
+ def process_json_files(start, end, step, length=0, tlang="en", spaces_include=False, strip_in_braces=True, strip_diacritics=True):
7
+ base_path = "texts"
8
+ translator = GoogleTranslator(source='auto', target=tlang)
9
+ results = []
10
+
11
+ for i in range(start, end + 1):
12
+ file_name = f"{base_path}/{i:02}.json"
13
+ try:
14
+ with open(file_name, 'r', encoding='utf-8') as file:
15
+ data = json.load(file)
16
+ text_blocks = data["text"]
17
+
18
+ full_text = ""
19
+ for block in text_blocks:
20
+ full_text += ' '.join(block)
21
+
22
+ clean_text = full_text
23
+ if strip_in_braces:
24
+ clean_text = re.sub(r"\[.*?\]", "", clean_text, flags=re.DOTALL)
25
+ if strip_diacritics:
26
+ clean_text = re.sub(r"[^\u05D0-\u05EA ]+", "", clean_text)
27
+ if not spaces_include:
28
+ clean_text = clean_text.replace(" ", "")
29
+
30
+ if length != 0:
31
+ selected_characters = clean_text[step - 1::step][:length]
32
+ else:
33
+ selected_characters = clean_text[step - 1::step] # If length is 0, select all characters from step
34
+
35
+ translated_text = translator.translate(''.join(selected_characters))
36
+ if selected_characters != "":
37
+ results.append({
38
+ "book": i,
39
+ "title": data["title"],
40
+ "original_text": selected_characters,
41
+ "translated_text": translated_text
42
+ })
43
+
44
+ except FileNotFoundError:
45
+ results.append({"error": f"File {file_name} not found."})
46
+ except json.JSONDecodeError:
47
+ results.append({"error": f"File {file_name} could not be read as JSON."})
48
+ except KeyError:
49
+ results.append({"error": f"Expected key 'text' is missing in {file_name}."})
50
+
51
+ return results