maryna7679 commited on
Commit
db4e125
·
1 Parent(s): 9ab5181

DB storage for captions

Browse files
Files changed (2) hide show
  1. Functions/caption_editor_functions.py +17 -31
  2. app.py +8 -7
Functions/caption_editor_functions.py CHANGED
@@ -1,43 +1,29 @@
1
  import pandas as pd
2
- import json
3
-
4
- FILE_PATH = "Resources/captions.jsonl"
5
 
6
 
7
  def get_captions_by_video_id(video_id):
8
- with open(FILE_PATH) as file:
9
- captions = pd.read_json(file, lines=True)
10
-
11
- captions_edit = captions[captions['file'] == video_id]
12
- captions_edit = captions_edit[['start_time', 'text', 'end_time']]
13
  captions_edit.columns = ["Start", "Text", "End"]
14
- return captions_edit
15
-
16
 
17
- def save_dataframe(df, video_id, user):
18
- cols = ["clean_text", "start_time", "user_id", "signer", "file", "end_time", "url", "text"]
19
- other_captions_data = []
20
- new_captions_data = []
21
 
22
- with open(FILE_PATH) as f:
23
- for line in f:
24
- caption = json.loads(line)
25
- if caption['file'] == video_id:
26
- new_captions_data.append(caption)
27
- else:
28
- other_captions_data.append(caption)
29
-
30
- other_captions = pd.DataFrame(data=other_captions_data, columns=cols)
31
- new_captions = pd.DataFrame(data=new_captions_data, columns=cols)
32
  try:
33
- new_captions['start_time'] = df['Start'].apply(lambda x: float(x))
34
- new_captions['text'] = df['Text']
35
- new_captions['end_time'] = df['End'].apply(lambda x: float(x))
36
- new_captions['user_id'] = user
37
-
38
- all_captions = pd.concat([other_captions, new_captions], ignore_index=True)
 
 
 
 
 
39
 
40
- all_captions.to_json(FILE_PATH, orient='records', lines=True)
41
  return "Save successful!"
42
  except ValueError:
43
  return "Save failed: Incorrect input format"
 
1
  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
+ captions = pd.DataFrame(response)
8
+ captions_edit = captions[['start_time', 'text', 'end_time']]
 
 
9
  captions_edit.columns = ["Start", "Text", "End"]
10
+ return captions_edit, captions
 
11
 
 
 
 
 
12
 
13
+ def save_dataframe(df, df_full, video_id, user):
 
 
 
 
 
 
 
 
 
14
  try:
15
+ df_full["user_id"].loc[
16
+ df_full["start_time"] != df["Start"] or
17
+ df_full["end_time"] != df["End"] or
18
+ df_full["text"] != df["Text"]
19
+ ] = user
20
+ df_full["start_time"] = df["Start"].apply(lambda x: float(x))
21
+ df_full["text"] = df["Text"]
22
+ df_full["end_time"] = df["End"].apply(lambda x: float(x))
23
+
24
+ df_json = df.to_json(orient="index")
25
+ default_app.database().child("Captions").child(video_id).set(df_json)
26
 
 
27
  return "Save successful!"
28
  except ValueError:
29
  return "Save failed: Incorrect input format"
app.py CHANGED
@@ -13,8 +13,8 @@ def get_username(profile: gr.OAuthProfile):
13
  return profile
14
 
15
 
16
- def save(df, video_id):
17
- return save_dataframe(df, video_id, user)
18
 
19
 
20
  def get_next_components():
@@ -28,12 +28,12 @@ def get_next_components():
28
  next_video_id = youtube_link_to_id(next_video_link)
29
 
30
  next_video = get_video_embed_by_id(next_video_id)
31
- next_captions = get_captions_by_video_id(next_video_id)
32
 
33
- return next_video, next_captions, next_video_id
34
 
35
 
36
- (start_video, start_captions, start_video_id) = get_next_components()
37
 
38
  with gr.Blocks(css=css) as main_page:
39
  gr.Markdown("# Caption Editor")
@@ -42,6 +42,7 @@ with gr.Blocks(css=css) as main_page:
42
 
43
  current_user = gr.Textbox(visible=False, interactive=False)
44
  current_video_id = gr.Textbox(value=start_video_id, visible=False, interactive=False)
 
45
 
46
  main_page.load(get_username, outputs=current_user)
47
 
@@ -64,9 +65,9 @@ with gr.Blocks(css=css) as main_page:
64
  next_video_button = gr.Button("Next")
65
 
66
  next_video_button.click(fn=get_next_components,
67
- outputs=[video_embed, caption_editor, current_video_id])
68
  save_button.click(fn=save,
69
- inputs=[caption_editor, current_video_id],
70
  outputs=save_result)
71
 
72
  main_page.launch(share=True, ssr_mode=False)
 
13
  return profile
14
 
15
 
16
+ def save(df, df_full, video_id):
17
+ return save_dataframe(df, df_full, video_id, user)
18
 
19
 
20
  def get_next_components():
 
28
  next_video_id = youtube_link_to_id(next_video_link)
29
 
30
  next_video = get_video_embed_by_id(next_video_id)
31
+ next_captions, next_captions_full = get_captions_by_video_id(next_video_id)
32
 
33
+ return next_video, next_video_id, next_captions, next_captions_full
34
 
35
 
36
+ (start_video, start_video_id, start_captions, start_captions_full) = get_next_components()
37
 
38
  with gr.Blocks(css=css) as main_page:
39
  gr.Markdown("# Caption Editor")
 
42
 
43
  current_user = gr.Textbox(visible=False, interactive=False)
44
  current_video_id = gr.Textbox(value=start_video_id, visible=False, interactive=False)
45
+ current_captions_full = gr.DataFrame(value=start_captions_full, visible=False, interactive=False)
46
 
47
  main_page.load(get_username, outputs=current_user)
48
 
 
65
  next_video_button = gr.Button("Next")
66
 
67
  next_video_button.click(fn=get_next_components,
68
+ outputs=[video_embed, caption_editor, current_video_id, current_captions_full])
69
  save_button.click(fn=save,
70
+ inputs=[caption_editor, current_captions_full, current_video_id],
71
  outputs=save_result)
72
 
73
  main_page.launch(share=True, ssr_mode=False)