maryna7679 commited on
Commit
cb2adf3
·
1 Parent(s): 00487e6

Login requirements

Browse files
Files changed (2) hide show
  1. Functions/caption_editor_functions.py +2 -34
  2. app.py +89 -93
Functions/caption_editor_functions.py CHANGED
@@ -2,38 +2,6 @@ import pandas as pd
2
  from Functions.db_connection import default_app
3
 
4
 
5
- # def get_captions_by_video_id(video_id):
6
- # response = default_app.database().child("Captions").child(video_id).get().val()
7
- # if response is None:
8
- # captions = pd.DataFrame(columns=["clean_text", "end_time", "signer", "start_time", "text", "user_id"])
9
- # else:
10
- # captions = pd.DataFrame(response)
11
- # captions_edit = captions[['start_time', 'text', 'end_time']]
12
- # captions_edit.columns = ["Start", "Text", "End"]
13
- # return captions_edit, captions
14
- #
15
- #
16
- # def save_dataframe(df, df_full, video_id, user):
17
- # if len(df.index) > len(df_full.index):
18
- # new_rows = len(df_full.index) - len(df.index)
19
- # empty_data = {col: [None for _ in range(new_rows)] for col in df_full.columns}
20
- # empty_df = pd.DataFrame(empty_data)
21
- #
22
- # df = pd.concat([df, empty_df], ignore_index=True)
23
- # try:
24
- # df_full["user_id"] = user
25
- # df_full["start_time"] = df["Start"].apply(lambda x: float(x))
26
- # df_full["text"] = df["Text"]
27
- # df_full["end_time"] = df["End"].apply(lambda x: float(x))
28
- #
29
- # df_json = df_full.to_dict(orient="index")
30
- # default_app.database().child("Captions").child(video_id).set(df_json)
31
- #
32
- # return "Save successful!"
33
- # except ValueError:
34
- # return "Save failed: Incorrect input format"
35
-
36
-
37
  def save_captions_to_db(df, video_id, user):
38
  try:
39
  df.columns = ['start_time', 'text', 'end_time']
@@ -41,8 +9,8 @@ def save_captions_to_db(df, video_id, user):
41
  default_app.database().child("video_captions").child(video_id).child("captions").set(df_json)
42
  default_app.database().child("video_captions").child(video_id).child("username").set(user)
43
  return "Save successful!"
44
- except Exception:
45
- return "Save failed"
46
 
47
 
48
  def request_captions_by_video_id(video_id):
 
2
  from Functions.db_connection import default_app
3
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  def save_captions_to_db(df, video_id, user):
6
  try:
7
  df.columns = ['start_time', 'text', 'end_time']
 
9
  default_app.database().child("video_captions").child(video_id).child("captions").set(df_json)
10
  default_app.database().child("video_captions").child(video_id).child("username").set(user)
11
  return "Save successful!"
12
+ except Exception as e:
13
+ return f"Save failed: {e}"
14
 
15
 
16
  def request_captions_by_video_id(video_id):
app.py CHANGED
@@ -15,10 +15,6 @@ def get_username(profile: gr.OAuthProfile):
15
  return profile
16
 
17
 
18
- # def save(df_copy, df_full, video_id):
19
- # return save_dataframe(df_copy, df_full, video_id, user)
20
-
21
-
22
  def on_row_select(df, evt: gr.SelectData):
23
  """Handle row selection in DataFrame"""
24
  if evt.index is not None and len(evt.index) > 0:
@@ -125,103 +121,103 @@ with gr.Blocks(css=css, head=yt_init_js) as main_page:
125
  # current_captions_full = gr.DataFrame(value=start_captions_full, visible=False, interactive=False)
126
  selected_row_idx = gr.Number(value=-1, visible=False)
127
 
128
- # @gr.render(inputs=current_user)
129
- # def render_page(logged_in_user):
130
- # if logged_in_user is None:
131
- # gr.Markdown("## Please log in via Hugging Face")
132
- # else:
133
- with gr.Row():
134
- with gr.Column(scale=2, min_width=600):
135
- # Video player and "next video button
136
- video_embed = gr.HTML(value=get_youtube_player_html())
137
- next_video_button = gr.Button("Next")
138
- with gr.Column(scale=1, min_width=200):
139
- # Read-only DataFrame with add button
140
- caption_editor = gr.DataFrame(interactive=False,
141
- elem_id="tbl",
142
- value=start_captions,
143
- datatype=["number", "str", "number"],
144
- col_count=(3, "fixed"),
145
- column_widths=["20%", "60%", "20%"],
146
- headers=["Start", "Text", "End"],
147
- wrap=True)
148
- add_entry_button = gr.Button("Add Entry", variant="secondary")
149
-
150
- with gr.Row():
151
- # Editing panel (initially hidden) - spans full width
152
- with gr.Group(visible=False) as editing_panel:
153
- gr.Markdown("### Edit Caption Entry")
154
- with gr.Row(equal_height=False):
155
- with gr.Column():
156
- start_time_input = gr.Textbox(label="Start Time (seconds)", value="0.000", interactive=False)
157
- insert_start_time_button = gr.Button("Insert Current Time")
158
- with gr.Column():
159
- text_input = gr.Textbox(label="Caption Text", placeholder="Enter caption text...")
160
-
161
- with gr.Column():
162
- end_time_input = gr.Textbox(label="End Time (seconds)", value="0.000", interactive=False)
163
- insert_end_time_button = gr.Button("Insert Current Time")
164
-
165
- with gr.Row(equal_height=False):
166
- save_entry_button = gr.Button("Save Entry", variant="primary")
167
- cancel_button = gr.Button("Cancel", variant="secondary")
168
-
169
- save_result = gr.Markdown()
170
-
171
- # Event handlers
172
- next_video_button.click(
173
- fn=get_next_components,
174
- outputs=[caption_editor, current_video_id]
175
- )
176
 
177
- # Load video when current_video_id changes
178
- current_video_id.change(
179
- fn=None,
180
- inputs=current_video_id,
181
- outputs=None,
182
- js="""(videoId) => {
183
- if (window.ytPlayer && window.ytPlayer.cueVideoById) {
184
- console.log('[Video Load] Calling cueVideoById with:', videoId);
185
- window.ytPlayer.cueVideoById(videoId);
186
- } else {
187
- console.error('[Video Load] Player not ready yet');
188
- }
189
- }"""
190
- )
191
 
192
- # Handle row selection in DataFrame
193
- caption_editor.select(
194
- fn=on_row_select,
195
- inputs=[caption_editor],
196
- outputs=[editing_panel, start_time_input, text_input, end_time_input, selected_row_idx, save_entry_button]
197
- )
198
 
199
- # Handle add entry button
200
- add_entry_button.click(
201
- fn=show_add_entry_form,
202
- outputs=[editing_panel, start_time_input, text_input, end_time_input, selected_row_idx, save_entry_button]
203
- )
204
 
205
- # Handle save entry
206
- save_entry_button.click(
207
- fn=save_entry,
208
- inputs=[caption_editor, start_time_input, text_input, end_time_input,
209
- selected_row_idx, current_video_id],
210
- outputs=[caption_editor, editing_panel, save_result]
211
- )
212
 
213
- insert_start_time_button.click(fn=None, inputs=None, outputs=start_time_input,
214
- js="() => window.ytPlayer ? +window.ytPlayer.getCurrentTime().toFixed(3) : 0")
215
 
216
- insert_end_time_button.click(fn=None, inputs=None, outputs=end_time_input,
217
- js="() => window.ytPlayer ? +window.ytPlayer.getCurrentTime().toFixed(3) : 0")
218
 
219
 
220
- # Handle cancel
221
- cancel_button.click(
222
- fn=cancel_edit,
223
- outputs=[editing_panel]
224
- )
225
 
226
  # Load initial video on page load
227
  main_page.load(
 
15
  return profile
16
 
17
 
 
 
 
 
18
  def on_row_select(df, evt: gr.SelectData):
19
  """Handle row selection in DataFrame"""
20
  if evt.index is not None and len(evt.index) > 0:
 
121
  # current_captions_full = gr.DataFrame(value=start_captions_full, visible=False, interactive=False)
122
  selected_row_idx = gr.Number(value=-1, visible=False)
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
+ else:
129
+ with gr.Row():
130
+ with gr.Column(scale=2, min_width=600):
131
+ # Video player and "next video button
132
+ video_embed = gr.HTML(value=get_youtube_player_html())
133
+ next_video_button = gr.Button("Next")
134
+ with gr.Column(scale=1, min_width=200):
135
+ # Read-only DataFrame with add button
136
+ caption_editor = gr.DataFrame(interactive=False,
137
+ elem_id="tbl",
138
+ value=start_captions,
139
+ datatype=["number", "str", "number"],
140
+ col_count=(3, "fixed"),
141
+ column_widths=["20%", "60%", "20%"],
142
+ headers=["Start", "Text", "End"],
143
+ wrap=True)
144
+ add_entry_button = gr.Button("Add Entry", variant="secondary")
145
+
146
+ with gr.Row():
147
+ # Editing panel (initially hidden) - spans full width
148
+ with gr.Group(visible=False) as editing_panel:
149
+ gr.Markdown("### Edit Caption Entry")
150
+ with gr.Row(equal_height=False):
151
+ with gr.Column():
152
+ start_time_input = gr.Textbox(label="Start Time (seconds)", value="0.000", interactive=False)
153
+ insert_start_time_button = gr.Button("Insert Current Time")
154
+ with gr.Column():
155
+ text_input = gr.Textbox(label="Caption Text", placeholder="Enter caption text...")
156
+
157
+ with gr.Column():
158
+ end_time_input = gr.Textbox(label="End Time (seconds)", value="0.000", interactive=False)
159
+ insert_end_time_button = gr.Button("Insert Current Time")
160
+
161
+ with gr.Row(equal_height=False):
162
+ save_entry_button = gr.Button("Save Entry", variant="primary")
163
+ cancel_button = gr.Button("Cancel", variant="secondary")
164
+
165
+ save_result = gr.Markdown()
166
+
167
+ # Event handlers
168
+ next_video_button.click(
169
+ fn=get_next_components,
170
+ outputs=[caption_editor, current_video_id]
171
+ )
172
 
173
+ # Load video when current_video_id changes
174
+ current_video_id.change(
175
+ fn=None,
176
+ inputs=current_video_id,
177
+ outputs=None,
178
+ js="""(videoId) => {
179
+ if (window.ytPlayer && window.ytPlayer.cueVideoById) {
180
+ console.log('[Video Load] Calling cueVideoById with:', videoId);
181
+ window.ytPlayer.cueVideoById(videoId);
182
+ } else {
183
+ console.error('[Video Load] Player not ready yet');
184
+ }
185
+ }"""
186
+ )
187
 
188
+ # Handle row selection in DataFrame
189
+ caption_editor.select(
190
+ fn=on_row_select,
191
+ inputs=[caption_editor],
192
+ outputs=[editing_panel, start_time_input, text_input, end_time_input, selected_row_idx, save_entry_button]
193
+ )
194
 
195
+ # Handle add entry button
196
+ add_entry_button.click(
197
+ fn=show_add_entry_form,
198
+ outputs=[editing_panel, start_time_input, text_input, end_time_input, selected_row_idx, save_entry_button]
199
+ )
200
 
201
+ # Handle save entry
202
+ save_entry_button.click(
203
+ fn=save_entry,
204
+ inputs=[caption_editor, start_time_input, text_input, end_time_input,
205
+ selected_row_idx, current_video_id],
206
+ outputs=[caption_editor, editing_panel, save_result]
207
+ )
208
 
209
+ insert_start_time_button.click(fn=None, inputs=None, outputs=start_time_input,
210
+ js="() => window.ytPlayer ? +window.ytPlayer.getCurrentTime().toFixed(3) : 0")
211
 
212
+ insert_end_time_button.click(fn=None, inputs=None, outputs=end_time_input,
213
+ js="() => window.ytPlayer ? +window.ytPlayer.getCurrentTime().toFixed(3) : 0")
214
 
215
 
216
+ # Handle cancel
217
+ cancel_button.click(
218
+ fn=cancel_edit,
219
+ outputs=[editing_panel]
220
+ )
221
 
222
  # Load initial video on page load
223
  main_page.load(