Spaces:
Runtime error
Runtime error
bartman081523
commited on
Commit
·
8881e78
1
Parent(s):
973aec5
translation cache for cli version
Browse files- cli_clock.py +89 -64
cli_clock.py
CHANGED
|
@@ -7,6 +7,7 @@ import pytz
|
|
| 7 |
from deep_translator import GoogleTranslator
|
| 8 |
from deep_translator.exceptions import NotValidLength, RequestError
|
| 9 |
from utils import process_json_files, flatten_text_with_line_breaks, build_word_index
|
|
|
|
| 10 |
|
| 11 |
# Set up logging
|
| 12 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
@@ -15,77 +16,101 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
|
|
| 15 |
TANACH_DATA = process_json_files(1, 39)
|
| 16 |
WORD_INDEX = build_word_index(TANACH_DATA)
|
| 17 |
|
| 18 |
-
# ---
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def display_current_verse():
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
| 72 |
# --- Utility Functions --- (Same as before)
|
| 73 |
|
| 74 |
def get_current_word_data(client_time_str):
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
-
|
|
|
|
| 7 |
from deep_translator import GoogleTranslator
|
| 8 |
from deep_translator.exceptions import NotValidLength, RequestError
|
| 9 |
from utils import process_json_files, flatten_text_with_line_breaks, build_word_index
|
| 10 |
+
import sqlite3 # Import sqlite3 for database handling
|
| 11 |
|
| 12 |
# Set up logging
|
| 13 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
| 16 |
TANACH_DATA = process_json_files(1, 39)
|
| 17 |
WORD_INDEX = build_word_index(TANACH_DATA)
|
| 18 |
|
| 19 |
+
# --- Database Setup ---
|
| 20 |
+
conn = sqlite3.connect('translation_cache.db') # Create or connect to database
|
| 21 |
+
cursor = conn.cursor()
|
| 22 |
+
cursor.execute('''
|
| 23 |
+
CREATE TABLE IF NOT EXISTS translations (
|
| 24 |
+
book_id INTEGER,
|
| 25 |
+
chapter_id INTEGER,
|
| 26 |
+
english_text TEXT,
|
| 27 |
+
PRIMARY KEY (book_id, chapter_id)
|
| 28 |
+
)
|
| 29 |
+
''')
|
| 30 |
+
conn.commit()
|
| 31 |
+
|
| 32 |
+
def translate_chapter(hebrew_chapter, book_id, chapter_id):
|
| 33 |
+
"""Translates a Hebrew chapter to English, caching the result in the database."""
|
| 34 |
+
|
| 35 |
+
# Check if translation exists in the database
|
| 36 |
+
cursor.execute(
|
| 37 |
+
"SELECT english_text FROM translations WHERE book_id=? AND chapter_id=?",
|
| 38 |
+
(book_id, chapter_id)
|
| 39 |
+
)
|
| 40 |
+
result = cursor.fetchone()
|
| 41 |
+
|
| 42 |
+
if result:
|
| 43 |
+
return result[0].split('\n') # Retrieve from database and split into lines
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
translator = GoogleTranslator(source='iw', target='en')
|
| 47 |
+
max_length = 2000 # Slightly below the limit to be safe
|
| 48 |
+
translated_text = ""
|
| 49 |
+
|
| 50 |
+
# Split the chapter into chunks smaller than the max length
|
| 51 |
+
chunks = [hebrew_chapter[i:i + max_length] for i in range(0, len(hebrew_chapter), max_length)]
|
| 52 |
+
|
| 53 |
+
for chunk in chunks:
|
| 54 |
+
translated_text += translator.translate(chunk)
|
| 55 |
+
|
| 56 |
+
# Store the translation in the database
|
| 57 |
+
cursor.execute(
|
| 58 |
+
"INSERT INTO translations (book_id, chapter_id, english_text) VALUES (?, ?, ?)",
|
| 59 |
+
(book_id, chapter_id, translated_text)
|
| 60 |
+
)
|
| 61 |
+
conn.commit()
|
| 62 |
+
|
| 63 |
+
return translated_text.split('\n') # Return as list of lines
|
| 64 |
+
|
| 65 |
+
except RequestError as e:
|
| 66 |
+
logging.warning(f"Translation failed: Request Error - {e}")
|
| 67 |
+
return ["Translation unavailable: Request Error"]
|
| 68 |
|
| 69 |
def display_current_verse():
|
| 70 |
+
"""Displays the verse corresponding to the current time."""
|
| 71 |
+
while True:
|
| 72 |
+
now = datetime.datetime.now()
|
| 73 |
+
current_time_str = now.strftime("%H:%M:%S")
|
| 74 |
+
|
| 75 |
+
word_data, _ = get_current_word_data(current_time_str)
|
| 76 |
+
if word_data is None:
|
| 77 |
+
logging.error("Word data not found for current time.")
|
| 78 |
+
time.sleep(1)
|
| 79 |
+
continue
|
| 80 |
+
|
| 81 |
+
book_id = word_data["book_id"]
|
| 82 |
+
chapter_id = word_data["chapter_id"]
|
| 83 |
+
verse_id = word_data["verse_id"]
|
| 84 |
+
|
| 85 |
+
hebrew_chapter = flatten_text_with_line_breaks(TANACH_DATA[book_id]["text"][chapter_id])
|
| 86 |
+
english_chapter = translate_chapter('\n'.join(hebrew_chapter), book_id, chapter_id)
|
| 87 |
+
|
| 88 |
+
print("\033c", end="") # Clear the terminal
|
| 89 |
+
print(f"Time: {current_time_str}")
|
| 90 |
+
print(f"{TANACH_DATA[book_id]['title']}, Chapter {chapter_id + 1}, Verse {verse_id}")
|
| 91 |
+
print("-" * 30)
|
| 92 |
+
print(hebrew_chapter[verse_id - 1])
|
| 93 |
+
print(english_chapter[verse_id - 1]) # Display corresponding English line
|
| 94 |
+
print("-" * 30)
|
| 95 |
+
time.sleep(1)
|
| 96 |
|
| 97 |
# --- Utility Functions --- (Same as before)
|
| 98 |
|
| 99 |
def get_current_word_data(client_time_str):
|
| 100 |
+
"""Gets data about the current word based on the client's time."""
|
| 101 |
+
try:
|
| 102 |
+
client_time = datetime.datetime.strptime(client_time_str, "%H:%M:%S")
|
| 103 |
+
total_seconds = int(client_time.strftime("%H")) * 3600 + \
|
| 104 |
+
int(client_time.strftime("%M")) * 60 + \
|
| 105 |
+
int(client_time.strftime("%S"))
|
| 106 |
|
| 107 |
+
# Find the closest key in WORD_INDEX
|
| 108 |
+
word_position = min(WORD_INDEX.keys(), key=lambda k: abs(k - total_seconds))
|
| 109 |
|
| 110 |
+
return WORD_INDEX[word_position], word_position
|
| 111 |
+
except Exception as e:
|
| 112 |
+
logging.error(f"Error processing client time: {e}")
|
| 113 |
+
return None, None
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
+
display_current_verse()
|