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

Changes to save format

Browse files
Files changed (2) hide show
  1. Functions/caption_editor_functions.py +47 -25
  2. app.py +11 -11
Functions/caption_editor_functions.py CHANGED
@@ -2,33 +2,55 @@ 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_full = pd.concat([df_full, 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"
 
 
 
 
 
 
 
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']
40
+ df_json = df.to_dict(orient="index")
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):
49
+ response = default_app.database().child("video_captions").child(video_id).child("captions").get().val()
50
+ if response is None:
51
+ captions = pd.DataFrame(columns=["end_time", "start_time", "text"])
52
+ else:
53
+ captions = pd.DataFrame(response)
54
+ captions_edit = captions[['start_time', 'text', 'end_time']]
55
+ captions_edit.columns = ["Start", "Text", "End"]
56
+ return captions_edit
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import pandas as pd
3
  from Functions.video_player_functions import youtube_link_to_id, get_video_embed_by_id, get_video_link_by_pointer, get_youtube_player_html
4
- from Functions.caption_editor_functions import get_captions_by_video_id, save_dataframe
5
  from Resources.css import css
6
  from Resources.js import yt_init_js
7
 
@@ -15,8 +15,8 @@ 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):
@@ -48,7 +48,7 @@ def show_add_entry_form():
48
  )
49
 
50
 
51
- def save_entry(df, start_time, text, end_time, selected_row_idx, video_id, df_full):
52
  """Save or update a caption entry"""
53
  try:
54
  start_time = float(start_time)
@@ -78,7 +78,7 @@ def save_entry(df, start_time, text, end_time, selected_row_idx, video_id, df_fu
78
  df_copy = df_copy.sort_values('Start').reset_index(drop=True)
79
 
80
  # Update in database
81
- save_result = save(df_copy, df_full, video_id)
82
 
83
  return (
84
  df_copy,
@@ -107,14 +107,14 @@ def get_next_components():
107
 
108
  try:
109
  next_video_id = youtube_link_to_id(next_video_link)
110
- next_captions, next_captions_full = get_captions_by_video_id(next_video_id)
111
- return next_captions, next_captions_full, next_video_id
112
  except (ValueError, Exception) as e:
113
  empty_captions = pd.DataFrame(columns=["Start", "Text", "End"])
114
  return empty_captions, "error"
115
 
116
 
117
- (start_captions, start_captions_full, start_video_id) = get_next_components()
118
 
119
  with gr.Blocks(css=css, head=yt_init_js) as main_page:
120
  gr.Markdown("## Caption Editor")
@@ -122,7 +122,7 @@ with gr.Blocks(css=css, head=yt_init_js) as main_page:
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
- 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)
@@ -171,7 +171,7 @@ with gr.Blocks(css=css, head=yt_init_js) as main_page:
171
  # Event handlers
172
  next_video_button.click(
173
  fn=get_next_components,
174
- outputs=[caption_editor, current_captions_full, current_video_id]
175
  )
176
 
177
  # Load video when current_video_id changes
@@ -206,7 +206,7 @@ with gr.Blocks(css=css, head=yt_init_js) as main_page:
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, current_captions_full],
210
  outputs=[caption_editor, editing_panel, save_result]
211
  )
212
 
 
1
  import gradio as gr
2
  import pandas as pd
3
  from Functions.video_player_functions import youtube_link_to_id, get_video_embed_by_id, get_video_link_by_pointer, get_youtube_player_html
4
+ from Functions.caption_editor_functions import request_captions_by_video_id, save_captions_to_db
5
  from Resources.css import css
6
  from Resources.js import yt_init_js
7
 
 
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):
 
48
  )
49
 
50
 
51
+ def save_entry(df, start_time, text, end_time, selected_row_idx, video_id):
52
  """Save or update a caption entry"""
53
  try:
54
  start_time = float(start_time)
 
78
  df_copy = df_copy.sort_values('Start').reset_index(drop=True)
79
 
80
  # Update in database
81
+ save_result = save_captions_to_db(df_copy, video_id, user)
82
 
83
  return (
84
  df_copy,
 
107
 
108
  try:
109
  next_video_id = youtube_link_to_id(next_video_link)
110
+ next_captions = request_captions_by_video_id(next_video_id)
111
+ return next_captions, next_video_id
112
  except (ValueError, Exception) as e:
113
  empty_captions = pd.DataFrame(columns=["Start", "Text", "End"])
114
  return empty_captions, "error"
115
 
116
 
117
+ (start_captions, start_video_id) = get_next_components()
118
 
119
  with gr.Blocks(css=css, head=yt_init_js) as main_page:
120
  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
+ # 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)
 
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
 
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