Spaces:
Sleeping
Sleeping
Commit
·
11f9fd2
1
Parent(s):
399c247
Login test 2.0
Browse files
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 |
-
|
| 53 |
-
|
| 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 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
gr.
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
with gr.
|
| 157 |
-
gr.
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
# Event handlers
|
| 176 |
-
next_video_button.click(
|
| 177 |
-
fn=get_next_components,
|
| 178 |
-
outputs=[caption_editor, current_video_id]
|
| 179 |
-
)
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 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)
|