maryna7679 commited on
Commit
3ac04fc
·
1 Parent(s): 1a8ceff
Files changed (1) hide show
  1. app.py +105 -108
app.py CHANGED
@@ -6,7 +6,7 @@ from Resources.css import css
6
  from Resources.js import yt_init_js
7
 
8
  next_video_pointer = 0
9
- user = ""
10
 
11
 
12
  def get_username(profile: gr.OAuthProfile):
@@ -114,6 +114,7 @@ def get_next_components():
114
 
115
  with gr.Blocks(css=css, head=yt_init_js) as main_page:
116
  gr.Markdown("## Caption Editor")
 
117
 
118
  current_user = gr.Textbox(visible=False, interactive=False)
119
  current_video_id = gr.Textbox(value=start_video_id, visible=False, interactive=False)
@@ -121,121 +122,117 @@ with gr.Blocks(css=css, head=yt_init_js) as main_page:
121
 
122
  main_page.load(get_username, outputs=current_user) # Disabled when auth is disabled
123
 
124
- @gr.render(inputs=current_user)
125
- def render_page(logged_in_user):
126
- if logged_in_user is None:
127
- gr.Markdown("## Please log in via Hugging Face")
128
- gr.LoginButton()
129
- else:
130
- with gr.Row():
131
- with gr.Column(scale=2, min_width=600):
132
- # Video player and "next video button
133
- # video_embed = gr.HTML(value=get_youtube_player_html())
134
- gr.HTML(value=get_youtube_player_html())
135
- next_video_button = gr.Button("Next")
136
- with gr.Column(scale=1, min_width=200):
137
- # Read-only DataFrame with add button
138
- caption_editor = gr.DataFrame(interactive=False,
139
- elem_id="tbl",
140
- value=start_captions,
141
- datatype=["number", "str", "number"],
142
- col_count=(3, "fixed"),
143
- column_widths=["20%", "60%", "20%"],
144
- headers=["Start", "Text", "End"],
145
- wrap=True)
146
- add_entry_button = gr.Button("Add Entry", variant="secondary")
147
-
148
- with gr.Row():
149
- # Editing panel (initially hidden) - spans full width
150
- with gr.Group(visible=False) as editing_panel:
151
- gr.Markdown("### Edit Caption Entry")
152
- with gr.Row(equal_height=False):
153
- with gr.Column():
154
- start_time_input = gr.Textbox(label="Start Time (seconds)", value="0.000", interactive=False)
155
- insert_start_time_button = gr.Button("Insert Current Time")
156
- with gr.Column():
157
- text_input = gr.Textbox(label="Caption Text", placeholder="Enter caption text...")
158
-
159
- with gr.Column():
160
- end_time_input = gr.Textbox(label="End Time (seconds)", value="0.000", interactive=False)
161
- insert_end_time_button = gr.Button("Insert Current Time")
162
-
163
- with gr.Row(equal_height=False):
164
- save_entry_button = gr.Button("Save Entry", variant="primary")
165
- cancel_button = gr.Button("Cancel", variant="secondary")
166
-
167
- save_result = gr.Markdown()
168
-
169
- # Event handlers
170
- next_video_button.click(
171
- fn=get_next_components,
172
- outputs=[caption_editor, current_video_id]
173
- )
174
-
175
- # Load video when current_video_id changes
176
- current_video_id.change(
177
- fn=None,
178
- inputs=current_video_id,
179
- outputs=None,
180
- js="""(videoId) => {
181
- if (window.ytPlayer && window.ytPlayer.cueVideoById) {
182
- console.log('[Video Load] Calling cueVideoById with:', videoId);
183
- window.ytPlayer.cueVideoById(videoId);
184
- } else {
185
- console.error('[Video Load] Player not ready yet');
186
- }
187
- }"""
188
- )
189
 
190
- # Handle row selection in DataFrame
191
- caption_editor.select(
192
- fn=on_row_select,
193
- inputs=[caption_editor],
194
- outputs=[editing_panel, start_time_input, text_input, end_time_input, selected_row_idx, save_entry_button]
195
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
 
197
- # Handle add entry button
198
- add_entry_button.click(
199
- fn=show_add_entry_form,
200
- outputs=[editing_panel, start_time_input, text_input, end_time_input, selected_row_idx, save_entry_button]
201
- )
 
 
 
 
 
 
 
 
 
202
 
203
- # Handle save entry
204
- save_entry_button.click(
205
- fn=save_entry,
206
- inputs=[caption_editor, start_time_input, text_input, end_time_input,
207
- selected_row_idx, current_video_id],
208
- outputs=[caption_editor, editing_panel, save_result]
209
- )
210
 
211
- insert_start_time_button.click(fn=None, inputs=None, outputs=start_time_input,
212
- js="() => window.ytPlayer ? +window.ytPlayer.getCurrentTime().toFixed(3) : 0")
 
 
 
213
 
214
- insert_end_time_button.click(fn=None, inputs=None, outputs=end_time_input,
215
- js="() => window.ytPlayer ? +window.ytPlayer.getCurrentTime().toFixed(3) : 0")
 
 
 
 
 
216
 
 
 
217
 
218
- # Handle cancel
219
- cancel_button.click(
220
- fn=cancel_edit,
221
- outputs=[editing_panel]
222
- )
223
 
224
- # Load initial video on page load
225
- main_page.load(
226
- fn=None,
227
- inputs=current_video_id,
228
- outputs=None,
229
- js="""(videoId) => {
230
- const checkPlayer = setInterval(() => {
231
- if (window.ytPlayer && window.ytPlayer.cueVideoById) {
232
- clearInterval(checkPlayer);
233
- window.ytPlayer.cueVideoById(videoId);
234
- }
235
- }, 100);
236
- }"""
237
- )
238
 
239
- gr.LoginButton()
 
 
 
 
240
 
241
  main_page.launch(share=True)
 
6
  from Resources.js import yt_init_js
7
 
8
  next_video_pointer = 0
9
+ user = "anonymous_user"
10
 
11
 
12
  def get_username(profile: gr.OAuthProfile):
 
114
 
115
  with gr.Blocks(css=css, head=yt_init_js) as main_page:
116
  gr.Markdown("## Caption Editor")
117
+ gr.LoginButton()
118
 
119
  current_user = gr.Textbox(visible=False, interactive=False)
120
  current_video_id = gr.Textbox(value=start_video_id, visible=False, interactive=False)
 
122
 
123
  main_page.load(get_username, outputs=current_user) # Disabled when auth is disabled
124
 
125
+ # Load initial video on page load
126
+ main_page.load(
127
+ fn=None,
128
+ inputs=current_video_id,
129
+ outputs=None,
130
+ js="""(videoId) => {
131
+ const checkPlayer = setInterval(() => {
132
+ if (window.ytPlayer && window.ytPlayer.cueVideoById) {
133
+ clearInterval(checkPlayer);
134
+ window.ytPlayer.cueVideoById(videoId);
135
+ }
136
+ }, 100);
137
+ }"""
138
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
+ # @gr.render(inputs=current_user)
141
+ # def render_page(logged_in_user):
142
+ # if logged_in_user is None:
143
+ # gr.Markdown("## Please log in via Hugging Face")
144
+ # else:
145
+ with gr.Row():
146
+ with gr.Column(scale=2, min_width=600):
147
+ # Video player and "next video button
148
+ video_embed = gr.HTML(value=get_youtube_player_html())
149
+ next_video_button = gr.Button("Next")
150
+ with gr.Column(scale=1, min_width=200):
151
+ # Read-only DataFrame with add button
152
+ caption_editor = gr.DataFrame(interactive=False,
153
+ elem_id="tbl",
154
+ value=start_captions,
155
+ datatype=["number", "str", "number"],
156
+ col_count=(3, "fixed"),
157
+ column_widths=["20%", "60%", "20%"],
158
+ headers=["Start", "Text", "End"],
159
+ wrap=True)
160
+ add_entry_button = gr.Button("Add Entry", variant="secondary")
161
+
162
+ with gr.Row():
163
+ # Editing panel (initially hidden) - spans full width
164
+ with gr.Group(visible=False) as editing_panel:
165
+ gr.Markdown("### Edit Caption Entry")
166
+ with gr.Row(equal_height=False):
167
+ with gr.Column():
168
+ start_time_input = gr.Textbox(label="Start Time (seconds)", value="0.000", interactive=False)
169
+ insert_start_time_button = gr.Button("Insert Current Time")
170
+ with gr.Column():
171
+ text_input = gr.Textbox(label="Caption Text", placeholder="Enter caption text...")
172
+
173
+ with gr.Column():
174
+ end_time_input = gr.Textbox(label="End Time (seconds)", value="0.000", interactive=False)
175
+ insert_end_time_button = gr.Button("Insert Current Time")
176
+
177
+ with gr.Row(equal_height=False):
178
+ save_entry_button = gr.Button("Save Entry", variant="primary")
179
+ cancel_button = gr.Button("Cancel", variant="secondary")
180
+
181
+ save_result = gr.Markdown()
182
+
183
+ # Event handlers
184
+ next_video_button.click(
185
+ fn=get_next_components,
186
+ outputs=[caption_editor, current_video_id]
187
+ )
188
 
189
+ # Load video when current_video_id changes
190
+ current_video_id.change(
191
+ fn=None,
192
+ inputs=current_video_id,
193
+ outputs=None,
194
+ js="""(videoId) => {
195
+ if (window.ytPlayer && window.ytPlayer.cueVideoById) {
196
+ console.log('[Video Load] Calling cueVideoById with:', videoId);
197
+ window.ytPlayer.cueVideoById(videoId);
198
+ } else {
199
+ console.error('[Video Load] Player not ready yet');
200
+ }
201
+ }"""
202
+ )
203
 
204
+ # Handle row selection in DataFrame
205
+ caption_editor.select(
206
+ fn=on_row_select,
207
+ inputs=[caption_editor],
208
+ outputs=[editing_panel, start_time_input, text_input, end_time_input, selected_row_idx, save_entry_button]
209
+ )
 
210
 
211
+ # Handle add entry button
212
+ add_entry_button.click(
213
+ fn=show_add_entry_form,
214
+ outputs=[editing_panel, start_time_input, text_input, end_time_input, selected_row_idx, save_entry_button]
215
+ )
216
 
217
+ # Handle save entry
218
+ save_entry_button.click(
219
+ fn=save_entry,
220
+ inputs=[caption_editor, start_time_input, text_input, end_time_input,
221
+ selected_row_idx, current_video_id],
222
+ outputs=[caption_editor, editing_panel, save_result]
223
+ )
224
 
225
+ insert_start_time_button.click(fn=None, inputs=None, outputs=start_time_input,
226
+ js="() => window.ytPlayer ? +window.ytPlayer.getCurrentTime().toFixed(3) : 0")
227
 
228
+ insert_end_time_button.click(fn=None, inputs=None, outputs=end_time_input,
229
+ js="() => window.ytPlayer ? +window.ytPlayer.getCurrentTime().toFixed(3) : 0")
 
 
 
230
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
 
232
+ # Handle cancel
233
+ cancel_button.click(
234
+ fn=cancel_edit,
235
+ outputs=[editing_panel]
236
+ )
237
 
238
  main_page.launch(share=True)