bartman081523 commited on
Commit
3f409fa
·
1 Parent(s): 8017292
Files changed (1) hide show
  1. app.py +49 -105
app.py CHANGED
@@ -27,8 +27,8 @@ def get_current_word_data(client_time_str):
27
  try:
28
  client_time = datetime.datetime.strptime(client_time_str, "%H:%M:%S")
29
  total_seconds = int(client_time.strftime("%H")) * 3600 + \
30
- int(client_time.strftime("%M")) * 60 + \
31
- int(client_time.strftime("%S"))
32
 
33
  # Find the closest key in WORD_INDEX
34
  word_position = min(WORD_INDEX.keys(), key=lambda k: abs(k - total_seconds))
@@ -46,7 +46,7 @@ def get_formatted_verse(book_id, chapter_id, verse_id, highlight_word=True):
46
  # Highlight the word *before* joining with <br>
47
  if highlight_word and 0 <= verse_id - 1 < len(flattened_chapter):
48
  flattened_chapter[verse_id - 1] = \
49
- f"<span class='highlight'>{flattened_chapter[verse_id - 1]}</span>"
50
 
51
  return '<br>'.join(flattened_chapter)
52
 
@@ -82,23 +82,6 @@ def translate_verse(hebrew_verse, highlight_word=True):
82
  return "Translation unavailable: Request Error"
83
 
84
 
85
- def get_client_time_from_ip(ip_address):
86
- """Attempts to get client time using IP address and API."""
87
- try:
88
- api_url = f"http://ip-api.com/json/{ip_address}"
89
- response = requests.get(api_url)
90
- response.raise_for_status() # Raise an exception for bad status codes
91
-
92
- data = response.json()
93
- timezone = data.get("timezone")
94
-
95
- if timezone:
96
- return timezone # Return timezone only
97
-
98
- except requests.exceptions.RequestException as e:
99
- logging.warning(f"Error fetching time from IP: {e}")
100
- return None
101
-
102
  # --- Gradio Interface ---
103
 
104
  def update_tanach_display(client_time_str, timezone):
@@ -140,70 +123,55 @@ def update_tanach_display(client_time_str, timezone):
140
 
141
  return verse_info, hebrew_verse, english_verse
142
 
143
- def auto_advance(client_time_str, timezone):
144
- """Automatically advances the text based on the client's time and a fixed interval."""
145
- while True:
146
- current_time = datetime.datetime.now().strftime("%H:%M:%S")
147
- verse_info, hebrew_verse, english_verse = update_tanach_display(current_time, timezone)
148
- yield verse_info, hebrew_verse, english_verse
149
- time.sleep(1) # Update every second
150
-
151
- # --- Fetching User's IP ---
152
-
153
- def fetch_user_ip():
154
- """Fetches the user's IP address using a public API."""
155
- try:
156
- response = requests.get('https://api.ipify.org?format=json')
157
- response.raise_for_status()
158
- data = response.json()
159
- return data.get('ip')
160
- except requests.exceptions.RequestException as e:
161
- logging.warning(f"Error fetching user's IP: {e}")
162
- return None
163
 
164
  # --- Gradio Interface ---
165
 
166
  with gr.Blocks(css="""
167
- .container {
168
- display: flex;
169
- flex-direction: column;
170
- align-items: center;
171
- font-family: 'Times New Roman', serif;
172
- }
173
- /* Add this highlight class styling */
174
- .highlight {
175
- background-color: #FFFF00; /* Yellow highlight */
176
- padding: 2px 5px;
177
- border-radius: 5px;
178
- }
179
- #verse-info {
180
- margin-bottom: 20px;
181
- text-align: center;
182
- }
183
- #verses {
184
- display: flex;
185
- flex-direction: row;
186
- justify-content: center;
187
- align-items: flex-start;
188
- gap: 50px;
189
- }
190
- #hebrew-verse {
191
- font-size: 18px;
192
- line-height: 1.5;
193
- margin-bottom: 20px;
194
- text-align: right;
195
- direction: rtl;
196
- }
197
- #english-verse {
198
- font-size: 18px;
199
- line-height: 1.5;
200
- margin-bottom: 20px;
201
- }
202
  """) as iface:
 
 
 
 
 
 
203
 
204
  with gr.Row():
205
- client_ip_input = gr.Textbox(label="Enter your IP address (optional)", value="")
206
- timezone_input = gr.Textbox(label="Timezone", value="", interactive=False) # Added timezone input
207
 
208
  with gr.Row():
209
  verse_info_output = gr.Markdown(label="Verse Information", elem_id="verse-info")
@@ -213,35 +181,11 @@ with gr.Blocks(css="""
213
  hebrew_verse_output = gr.HTML(label="Hebrew Verse", elem_id="hebrew-verse")
214
  english_verse_output = gr.HTML(label="English Translation", elem_id="english-verse")
215
 
216
- # Fetch user's IP and get timezone
217
- gr.Button("Fetch IP and Timezone").click(
218
- fn=fetch_user_ip,
219
- inputs=[],
220
- outputs=[client_ip_input],
221
- queue=False,
222
- )
223
-
224
- client_ip_input.change(
225
- fn=get_client_time_from_ip,
226
- inputs=[client_ip_input],
227
- outputs=[timezone_input],
228
- queue=False,
229
- )
230
-
231
- # Update the display with verse information and translations
232
- client_ip_input.submit(
233
- fn=update_tanach_display,
234
- inputs=[client_ip_input, timezone_input],
235
- outputs=[verse_info_output, hebrew_verse_output, english_verse_output],
236
- queue=False
237
- )
238
-
239
- # Start automatic advancement
240
- gr.Button("Update Position").click(
241
- fn=auto_advance,
242
- inputs=[client_ip_input, timezone_input],
243
  outputs=[verse_info_output, hebrew_verse_output, english_verse_output],
244
- queue=False
245
  )
246
 
247
  class TestWordIndex(unittest.TestCase):
 
27
  try:
28
  client_time = datetime.datetime.strptime(client_time_str, "%H:%M:%S")
29
  total_seconds = int(client_time.strftime("%H")) * 3600 + \
30
+ int(client_time.strftime("%M")) * 60 + \
31
+ int(client_time.strftime("%S"))
32
 
33
  # Find the closest key in WORD_INDEX
34
  word_position = min(WORD_INDEX.keys(), key=lambda k: abs(k - total_seconds))
 
46
  # Highlight the word *before* joining with <br>
47
  if highlight_word and 0 <= verse_id - 1 < len(flattened_chapter):
48
  flattened_chapter[verse_id - 1] = \
49
+ f"<span class='highlight'>{flattened_chapter[verse_id - 1]}</span>"
50
 
51
  return '<br>'.join(flattened_chapter)
52
 
 
82
  return "Translation unavailable: Request Error"
83
 
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  # --- Gradio Interface ---
86
 
87
  def update_tanach_display(client_time_str, timezone):
 
123
 
124
  return verse_info, hebrew_verse, english_verse
125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  # --- Gradio Interface ---
128
 
129
  with gr.Blocks(css="""
130
+ .container {
131
+ display: flex;
132
+ flex-direction: column;
133
+ align-items: center;
134
+ font-family: 'Times New Roman', serif;
135
+ }
136
+ /* Add this highlight class styling */
137
+ .highlight {
138
+ background-color: #FFFF00; /* Yellow highlight */
139
+ padding: 2px 5px;
140
+ border-radius: 5px;
141
+ }
142
+ #verse-info {
143
+ margin-bottom: 20px;
144
+ text-align: center;
145
+ }
146
+ #verses {
147
+ display: flex;
148
+ flex-direction: row;
149
+ justify-content: center;
150
+ align-items: flex-start;
151
+ gap: 50px;
152
+ }
153
+ #hebrew-verse {
154
+ font-size: 18px;
155
+ line-height: 1.5;
156
+ margin-bottom: 20px;
157
+ text-align: right;
158
+ direction: rtl;
159
+ }
160
+ #english-verse {
161
+ font-size: 18px;
162
+ line-height: 1.5;
163
+ margin-bottom: 20px;
164
+ }
165
  """) as iface:
166
+ with gr.Row():
167
+ timezone_input = gr.Dropdown(
168
+ choices=[tz for tz in pytz.common_timezones],
169
+ label="Select Your Timezone",
170
+ value="UTC" # Set a default timezone
171
+ )
172
 
173
  with gr.Row():
174
+ advance_button = gr.Button("Advance to Current Time")
 
175
 
176
  with gr.Row():
177
  verse_info_output = gr.Markdown(label="Verse Information", elem_id="verse-info")
 
181
  hebrew_verse_output = gr.HTML(label="Hebrew Verse", elem_id="hebrew-verse")
182
  english_verse_output = gr.HTML(label="English Translation", elem_id="english-verse")
183
 
184
+ # Update the display with verse information and translations when the button is clicked
185
+ advance_button.click(
186
+ fn=lambda tz: update_tanach_display(datetime.datetime.now(pytz.timezone(tz)).strftime("%H:%M:%S"), tz),
187
+ inputs=[timezone_input],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  outputs=[verse_info_output, hebrew_verse_output, english_verse_output],
 
189
  )
190
 
191
  class TestWordIndex(unittest.TestCase):