File size: 2,669 Bytes
2fbb982
db4e125
2d7377d
2fbb982
 
4051497
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fbb982
3a43614
4051497
 
 
3a43614
00487e6
4051497
83a1f47
 
 
 
2d7377d
cb2adf3
2d7377d
db4e125
2fbb982
4051497
00487e6
4051497
 
 
 
 
 
00487e6
 
4051497
00487e6
 
4051497
 
 
 
00487e6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import pandas as pd
from Functions.db_connection import default_app
from Resources.localization import get_string


# ADC-IMPLEMENTS: <gc-transform-caption-df-01>
def save_captions_to_db(df, video_id, user, video_pointer):
    """Save captions to Firebase with per-entry alignment and video assignment.



    Args:

        df: pandas DataFrame with columns ["Start", "Text", "End", "Aligned"]

            (4-column DataFrame with per-entry aligned values already set by caller)

        video_id: YouTube video ID string

        user: HuggingFace username string

        video_pointer: Integer index of the video in the videos collection



    The Aligned column is already set correctly per-entry by save_entry in app.py.

    This function simply renames columns to Firebase field names and writes.

    It also writes the current user to videos/{video_pointer}/assigned_to.

    """
    try:
        data = df.copy()
        # The Aligned column is already set correctly per-entry by the caller.
        # Just rename columns to Firebase field names.
        data.columns = ['start_time', 'text', 'end_time', 'aligned']
        df_json = data.to_dict(orient="index")
        default_app.database().child("video_captions").child(video_id).child("captions").set(df_json)

        # Auto-assign: only if the video isn't already assigned to someone
        existing_assignment = default_app.database().child("videos").child(str(video_pointer)).child("assigned_to").get().val()
        if not existing_assignment:
            default_app.database().child("videos").child(str(video_pointer)).child("assigned_to").set(user)
        return get_string("save_successful")
    except Exception as e:
        return f"{get_string('save_failed')} {str(e)}"


# ADC-IMPLEMENTS: <gc-transform-firebase-df-01>
def request_captions_by_video_id(video_id):
    """Read captions from Firebase and return a 4-column DataFrame.



    Returns all 4 columns: Start, Text, End, Aligned. The caller

    (get_next_components in app.py) returns this 4-column DataFrame

    directly to the gr.DataFrame for display.

    """
    response = default_app.database().child("video_captions").child(video_id).child("captions").get().val()
    if response is None:
        captions = pd.DataFrame(columns=["end_time", "start_time", "text", "aligned"])
    else:
        captions = pd.DataFrame(response)
        if 'aligned' not in captions.columns:
            captions['aligned'] = False
    captions_edit = captions[['start_time', 'text', 'end_time', 'aligned']]
    captions_edit.columns = ["Start", "Text", "End", "Aligned"]
    return captions_edit