File size: 4,458 Bytes
97260a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import json
import datetime
import logging

import gradio as gr
from deep_translator import GoogleTranslator, exceptions

from utils import process_json_files, flatten_text_with_line_breaks, calculate_tanach_statistics, build_word_index

# Set up logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# Load Tanach text
TANACH_DATA = process_json_files(1, 39)
WORD_INDEX = build_word_index(TANACH_DATA)

# --- Utility Functions ---

def get_current_word_data():
    """Gets data about the current word based on the current time."""
    total_seconds = int(datetime.datetime.now().strftime("%H")) * 3600 + \
                   int(datetime.datetime.now().strftime("%M")) * 60 + \
                   int(datetime.datetime.now().strftime("%S"))
    word_position = total_seconds % len(WORD_INDEX)
    return WORD_INDEX.get(word_position), word_position

def get_formatted_verse(book_id, chapter_id, verse_id, highlight_word=True):
    """Returns a formatted verse with optional word highlighting."""
    chapter_text = TANACH_DATA[book_id]["text"][chapter_id]
    flattened_chapter = flatten_text_with_line_breaks(chapter_text)

    if highlight_word:
        flattened_chapter[verse_id - 1] = \
            f"<span class='highlight'>{flattened_chapter[verse_id - 1]}</span>"

    return '<br>'.join(flattened_chapter)

def translate_verse(hebrew_verse, verse_id, highlight_word=True):
    """Translates a Hebrew verse to English and highlights the given word."""
    try:
        translator = GoogleTranslator(source='iw', target='en')
        translated_text = translator.translate(hebrew_verse).split('\n')

        if highlight_word:
            translated_text[verse_id - 1] = \
                f"<span class='highlight'>{translated_text[verse_id - 1]}</span>"

        return '<br>'.join(translated_text)
    except exceptions.TranslationError as e:
        logging.warning(f"Translation failed: {e}")
        return "Translation unavailable"

# --- Gradio Interface ---

def update_tanach_display():
    """Updates the Gradio interface with current time, verse info, and translations."""
    current_time = datetime.datetime.now().strftime("%H:%M:%S")
    word_data, word_position = get_current_word_data()

    if word_data is None:
        logging.error(f"Word position {word_position} not found in index.")
        return current_time, "Error: Word not found", "", ""

    book_id = word_data["book_id"]
    chapter_id = word_data["chapter_id"]
    verse_id = word_data["verse_id"]

    # Format verse information
    verse_info = f"""
    **{TANACH_DATA[book_id]['title']}**
    Chapter {chapter_id + 1}, Verse {verse_id}
    """

    # Get and format Hebrew and English verses
    hebrew_verse = get_formatted_verse(book_id, chapter_id, verse_id)
    english_verse = translate_verse('\n'.join(hebrew_verse.split('<br>')), verse_id)

    return current_time, verse_info, hebrew_verse, english_verse

with gr.Blocks(css="""
    .container {
        display: flex;
        flex-direction: column;
        align-items: center;
        font-family: 'Times New Roman', serif;
    }
    .highlight {
        background-color: #FFFF00;
        padding: 2px 5px;
        border-radius: 5px;
    }
    #verse-info {
        margin-bottom: 20px;
        text-align: center;
    }
    #verses {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: flex-start;
        gap: 50px;
    }
    #hebrew-verse {
        font-size: 18px;
        line-height: 1.5;
        margin-bottom: 20px;
        text-align: right;
        direction: rtl;
    }
    #english-verse {
        font-size: 18px;
        line-height: 1.5;
        margin-bottom: 20px;
    }
""") as iface:
    # ... (no changes in the Markdown component)

    with gr.Row():
        time_output = gr.Textbox(label="Current Time", elem_id="current-time")

    with gr.Row():
        verse_info_output = gr.Markdown(label="Verse Information", elem_id="verse-info")

    # Place Hebrew and English verses within a flex container
    with gr.Row(elem_id="verses"):
        hebrew_verse_output = gr.HTML(label="Hebrew Verse", elem_id="hebrew-verse")
        english_verse_output = gr.HTML(label="English Translation", elem_id="english-verse")

    iface.load(update_tanach_display, [],
               [time_output, verse_info_output, hebrew_verse_output, english_verse_output])
    iface.launch(share=True)