maryna7679 commited on
Commit
50dc821
·
1 Parent(s): 6911451

Render test

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