maryna7679 commited on
Commit
11f9fd2
·
1 Parent(s): 399c247

Login test 2.0

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