Py.Cafe

AIPHeX/

RushXplorerDemo

AI-driven dashboard to streamline your rushes editing, storyboarding, and EDL creation.

DocsPricing
  • CHARACTERS.json
  • EDITORIAL.json
  • EPISODE.json
  • HIGHLIGHTS.json
  • RushXplorer.png
  • STORYLINE.json
  • TEASER.json
  • app.py
  • requirements.txt
  • transcript_test.enc
app.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import solara
import pycafe
import pandas as pd
import altair as alt
import json
from datetime import datetime, timedelta
from cryptography.fernet import Fernet
#import solara.website
from pathlib import Path
import PIL.Image

# Global reactive states


continuous_update = solara.reactive(True)
file_path_state = solara.reactive("")
transcript_data_state = solara.reactive(None)
error_message = solara.reactive("")
selected_datum = solara.reactive(None)
solara.lab.theme.themes.light.primary = "#AEDEE9"
solara.lab.theme.themes.light.secondary = "#B0DFE9"


image_path = "./RushXplorer.png"


solara.lab.theme.themes.dark.primary ="#E17E35"#"#CF884F"### #"#CF884F" #

RUSHXPLORER_KEY = pycafe.get_secret(
        "RUSHXPLORER_KEY",
        """We need the RUSHXPLORER key to make the app work.""",
    )
def _init_fernet():
    return Fernet(RUSHXPLORER_KEY.encode())
_f = _init_fernet()

# Utility functions
def time_to_seconds(time_str: str) -> float:
    t = datetime.strptime(time_str, "%H:%M:%S")
    return timedelta(hours=t.hour, minutes=t.minute, seconds=t.second).total_seconds()

def seconds_to_time(seconds: int) -> str:
    return str(timedelta(seconds=int(seconds)))

def load_json(filename: str) -> dict:
    try:
        with open(filename, 'r') as f:
            return json.load(f)
    except Exception as e:
        print(f"Error loading {filename}: {e}")
        return {}

# Categorical options for relevance
relevance_options = ["low", "medium", "high"]
    
image = PIL.Image.open(image_path)

@solara.component
def Home():

    solara.lab.ThemeToggle()
    solara.Image(image,width="25%")
    solara.Markdown(
        """
Welcome to the RushXplorer Demo Dashboard! This proof-of-concept showcases AI-powered transcript processing and a future interactive dashboard for rushes editing:

- **Transcript**: Your raw footage transcripts—of any format—are **AI-preprocessed** into a structured JSON model, ready for exploration.
- **Characters**: AI-drafted character lists and metadata are fully editable, with future support for **prompt-driven LLM chats** to refine roles and relevance.
- **Storyline**: Narrative arcs and sub-arcs are **AI-generated**, editable on-the-fly, and can be reorganized via conversational prompts.
- **Highlights**: Key moments are initially selected by AI for emotional and narrative impact; adjust or add highlights through direct editing or LLM guidance.
- **Episode & Teaser**: Episode cuts and teasers are assembled by AI into EDLs (Edit Decision Lists) of **any format**, and can be re-formatted or re-ordered dynamically.
- **Editorial**: Manage review questions and tasks with AI-suggested priorities, all editable and extendable via chat.

Each tab combines interactive Altair visualizations with JSON viewers for transparency. Soon, you’ll be able to talk directly to the embedded LLM to adjust data, regenerate edits, and output custom EDL formats—all in real time.
        """
    )

@solara.component
def FileInput():
    solara.Button(
        "Insert Demo Rushes",
        on_click=lambda: file_path_state.set("./transcript_test.enc")
    )
#    def handle_file_load():
#        path = file_path_state.value.strip()
#        try:
#            with open(path, 'r') as f:
#                transcript_data_state.value = json.load(f)
#                error_message.value = ''
#        except Exception as e:
#            transcript_data_state.value = None
#            error_message.value = f"Error: {e}"

    def handle_file_load():
        path = file_path_state.value.strip()
        try:
            # Open encrypted file in binary mode
            with open(path, 'rb') as f_enc:
                encrypted = f_enc.read()
            # Decrypt and decode bytes to string
            decrypted = _f.decrypt(encrypted).decode('utf-8')
            # Parse JSON
            transcript_data_state.value = json.loads(decrypted)
            error_message.value = ''
        except Exception as e:
            transcript_data_state.value = None
            error_message.value = f"Error loading encrypted JSON: {e}"


    with solara.Column():
        solara.InputText(label="Transcript JSON file:", value=file_path_state)
        solara.Button("Load Transcript", on_click=handle_file_load)
        if error_message.value:
            solara.Error(error_message.value)

@solara.component
def TranscriptProcessor():
    data = transcript_data_state.value
    if data is None:
        solara.Markdown("No transcript loaded.")
        return
    transcripts = data.get('transcripts', [])
    df = pd.DataFrame(transcripts).dropna(subset=['timestamp', 'dialogue'])
    df['ts_sec'] = df['timestamp'].apply(time_to_seconds)
    df.sort_values('ts_sec', inplace=True)
    window_size = 120
    total = int(df['ts_sec'].max() if not df.empty else 0)
    speakers = sorted(df['role'].unique())

    windows = []
    for start in range(0, total, window_size):
        end = start + window_size
        w = df[(df['ts_sec'] >= start) & (df['ts_sec'] < end)]
        summary = {'start_time': seconds_to_time(start), 'end_time': seconds_to_time(end)}
        for s in speakers:
            wc = int(w[w['role']==s]['dialogue'].str.split().str.len().sum()) if not w.empty else 0
            summary[f"{s}_word_count"] = wc
        windows.append(summary)
    result_df = pd.DataFrame(windows)

    solara.DataFrame(result_df)

    # Heatmap of word counts
    melt_vars = [col for col in result_df.columns if col.endswith('_word_count')]
    melted = result_df.melt(
        id_vars=['start_time', 'end_time'],
        value_vars=melt_vars,
        var_name='speaker',
        value_name='word_count'
    )
    melted['speaker'] = melted['speaker'].str.replace('_word_count', '')

    heatmap = (
        alt.Chart(melted, title='Speaker Word Count Over Time')
        .mark_rect()
        .encode(
            alt.X('start_time:N', title='Start Time', sort=None),
            alt.Y('speaker:N', title='Speaker'),
            alt.Color('word_count:Q', title='Words Spoken'),
            tooltip=[
                alt.Tooltip('start_time', title='Start'),
                alt.Tooltip('end_time', title='End'),
                alt.Tooltip('speaker', title='Speaker'),
                alt.Tooltip('word_count', title='Words')
            ],
        )
        .configure_view(step=13, strokeWidth=0)
        .configure_axis(domain=False)
        .properties(width='container')
    )
    with solara.Card('Speaker Word Count Heatmap'):
        solara.AltairChart(heatmap, on_click=lambda d: selected_datum.set(d))

@solara.component
def Transcript():
    solara.Markdown("Complete, timestamped dialogue entries capturing every role’s spoken lines and moments in the raw rushes.")
    FileInput()
    TranscriptProcessor()

@solara.component
def ViewerJSON(filename: str, id_field: str = None):
    import json
    import pandas as pd

    relevance_options = ["low", "medium", "high"]
    raw = load_json(filename)
    if not raw:
        solara.Markdown(f"Failed to load {filename}")
        return

    # --- Special branch for editorial.json ---
    if isinstance(raw, dict) and "questions" in raw and "tasks" in raw:
        # Render questions
        show_dialog = solara.use_reactive(False)
        solara.Markdown("## Questions")
        for q in raw["questions"]:
            solara.Markdown(f"- {q}")
            with solara.Row():
                solara.Button("Answer", on_click=lambda: show_dialog.set(True))

        # Render tasks
        solara.Markdown("## Tasks")
        for task in raw["tasks"]:
            solara.Markdown("—")
            for k, v in task.items():
                with solara.Columns([1, 3]):
                    solara.Markdown(f"**{k.replace('_', ' ').title()}**")
                    # Priority as select
                    if k == "priority" and isinstance(v, str):
                        sel, set_sel = solara.use_state(v)
                        solara.Select(label="", value=sel, values=relevance_options, on_value=set_sel)
                    # related_highlights list
                    elif isinstance(v, list):
                        solara.Markdown(", ".join(v) or "None")
                    else:
                        solara.Markdown(str(v))
            with solara.Row():
                solara.Button("Execute task", on_click=lambda: show_dialog.set(True))
                #solara.Button("Show your answer", on_click=lambda: show_conditional.set(not show_conditional.value))
                #with solara.lab.ConfirmationDialog(open=show_dialog):
                #    solara.InputText("Type your additional task instructions here", autofocus=True)
                
        return

    # --- General JSON viewer below ---
    # Normalize raw into a list of entries
    if isinstance(raw, dict) and id_field and raw.get(id_field) is not None:
        items = [raw]
    elif isinstance(raw, dict):
        lists = [v for v in raw.values() if isinstance(v, list)]
        if len(lists) == 1:
            items = lists[0]
        elif all(isinstance(v, dict) for v in raw.values()):
            items = list(raw.values())
        else:
            solara.Markdown("Unsupported JSON dict structure")
            return
    elif isinstance(raw, list):
        items = raw
    else:
        solara.Markdown("Unsupported JSON structure")
        return

    if not items:
        solara.Markdown("No entries to display")
        return

    # Auto-detect id_field if not provided
    if id_field is None:
        possible = [k for k in items[0].keys() if k.endswith("_id")]
        id_field = possible[0] if possible else list(items[0].keys())[0]

    # Dropdown for selecting entry
    ids = [str(item.get(id_field, i)) for i, item in enumerate(items)]
    selected, set_selected = solara.use_state(ids[0])
    solara.Select(
        label=id_field.replace("_", " ").title(),
        value=selected,
        values=ids,
        on_value=set_selected,
    )

    # Render selected entry in two columns
    idx = ids.index(selected)
    entry = items[idx]
    for k, v in entry.items():
        with solara.Columns([1, 3]):
            solara.Markdown(f"**{k.replace('_', ' ').title()}**")
            # categorical select for relevance-like fields
            if isinstance(v, str) and v.lower() in relevance_options:
                sel, set_sel = solara.use_state(v.lower())
                solara.Select(label="", value=sel, values=relevance_options, on_value=set_sel)
            # primitive types
            elif isinstance(v, (str, int, float, bool)):
                val = solara.reactive(str(v))
                solara.InputText(label="", value=val, continuous_update=continuous_update.value)
            # list of dicts -> DataFrame
            elif isinstance(v, list) and v and isinstance(v[0], dict):
                df = pd.DataFrame(v)
                solara.DataFrame(df)
            # list of primitives
            elif isinstance(v, list):
                solara.Markdown(", ".join(map(str, v)))
            # nested dict -> code block
            elif isinstance(v, dict):
                code = json.dumps(v, indent=2)
                solara.Markdown(f"```json\n{code}\n```")
            else:
                solara.Markdown(str(v))


@solara.component
def Characters():
    solara.Markdown("A roster of all persons on scene, with metadata on their importance and which story arcs and highlights they appear in.")
    # Summary plot: number of arcs per character
    chars = load_json("CHARACTERS.json").get("characters", []) if isinstance(load_json("CHARACTERS.json"), dict) else load_json("CHARACTERS.json")
    df_chars = pd.DataFrame(chars)
    df_chars['arc_count'] = df_chars['relevant_arcs'].apply(len)
    arc_chart = (
        alt.Chart(df_chars, title='Relevant Arcs per Character')
        .mark_bar()
        .encode(
            alt.X('name:N', sort=None),
            alt.Y('arc_count:Q', title='Number of Arcs'),
            tooltip=['name', 'relative_importance']
        )
        .properties(width='container')
    )
    solara.AltairChart(arc_chart)
    ViewerJSON("CHARACTERS.json", id_field="character_id")

@solara.component
def Highlights():
    solara.Markdown("Selected key moments with IDs, arc associations, precise timestamps, summaries, emotional impact scores, and involved characters.")
    # Load storyline and highlights JSON
    sl = load_json("STORYLINE.json")
    storyline = sl if isinstance(sl, dict) else next(
        (el for el in sl if isinstance(el, dict) and 'story_arcs' in el), {}
    )
    hl = load_json("HIGHLIGHTS.json")
    highlights = hl if isinstance(hl, dict) else next(
        (el for el in hl if isinstance(el, dict) and 'highlights' in el), {}
    )

    # Helpers to convert between HH:MM:SS and seconds
    def to_seconds(ts: str) -> int:
        h, m, s = map(int, ts.split(':'))
        return h * 3600 + m * 60 + s

    def to_time(sec: int) -> str:
        h = sec // 3600
        m = (sec % 3600) // 60
        s = sec % 60
        return f"{h:02d}:{m:02d}:{s:02d}"

    # Define time windows across full timeline
    start_t = to_seconds(storyline.get('start_time', '00:00:00'))
    end_t = to_seconds(storyline.get('end_time', '00:00:00'))
    window_size = 60  # seconds per bin
    windows = []
    for ws in range(start_t, end_t, window_size):
        we = min(ws + window_size, end_t)
        windows.append({'start_s': ws, 'end_s': we,
                        'start_time': to_time(ws), 'end_time': to_time(we)})

    # Precompute highlight midpoints
    hl_items = []
    for item in highlights.get('highlights', []):
        s = to_seconds(item['start_timestamp'])
        e = to_seconds(item['end_timestamp'])
        hl_items.append({'arc_id': item['arc_id'], 'mid': (s + e)/2})

    # Build records: one row per window per arc if arc covers window
    records = []
    for w in windows:
        for arc in storyline.get('story_arcs', []):
            a_start = to_seconds(arc['start_timestamp'])
            a_end = to_seconds(arc['end_timestamp'])
            if not (w['end_s'] <= a_start or w['start_s'] >= a_end):
                cnt = sum(1 for h in hl_items if h['arc_id'] == arc['arc_id']
                          and w['start_s'] <= h['mid'] < w['end_s'])
                records.append({
                    'start_time': w['start_time'],
                    'arc_title': arc['title'],
                    'highlight_count': cnt
                })
    df = pd.DataFrame(records)
    if df.empty:
        solara.Markdown("No data available for highlight density plot.")
        return

    # State for click selection
    selected = solara.use_state(None)

    # Heatmap: window start vs arc, intensity = highlight count
    heatmap = (
        alt.Chart(df, title='Highlights Density per Story Arc Over Time')
        .mark_rect()
        .encode(
            x=alt.X('start_time:N', title='Window Start', sort=None),
            y=alt.Y('arc_title:N', title='Story Arc', sort=[arc['title'] for arc in storyline.get('story_arcs', [])]),
            color=alt.Color('highlight_count:Q', title='Highlights', scale=alt.Scale(scheme='orangered')),
            tooltip=[
                alt.Tooltip('start_time', title='Window Start'),
                alt.Tooltip('arc_title', title='Arc'),
                alt.Tooltip('highlight_count', title='Highlights')
            ]
        )
        .properties(width='container', height=400)
    )

    # Display chart with click handler
    with solara.Card('Story arcs & highlights over time'):
        solara.AltairChart(heatmap)#, on_click=lambda datum: selected.set(datum))
    # Show selected detail
    #if selected.value:
    #    solara.Markdown(f"**Selected**: Window `{selected.value['start_time']}`, Arc `{selected.value['arc_title']}`, Highlights `{selected.value['highlight_count']}`")

    ViewerJSON("HIGHLIGHTS.json", id_field="highlight_id")

@solara.component
def Storyline():
    solara.Markdown("Definitions of each narrative arc—IDs, titles, summaries, start/end times, and nested sub-arcs that structure the full timeline.")
    sl = load_json("STORYLINE.json")
    storyline = sl if isinstance(sl, dict) else next(
        (el for el in sl if isinstance(el, dict) and 'story_arcs' in el), {}
    )
    hl = load_json("HIGHLIGHTS.json")
    highlights = hl if isinstance(hl, dict) else next(
        (el for el in hl if isinstance(el, dict) and 'highlights' in el), {}
    )

    # Helpers to convert between HH:MM:SS and seconds
    def to_seconds(ts: str) -> int:
        h, m, s = map(int, ts.split(':'))
        return h * 3600 + m * 60 + s

    def to_time(sec: int) -> str:
        h = sec // 3600
        m = (sec % 3600) // 60
        s = sec % 60
        return f"{h:02d}:{m:02d}:{s:02d}"

    # Define time windows across full timeline
    start_t = to_seconds(storyline.get('start_time', '00:00:00'))
    end_t = to_seconds(storyline.get('end_time', '00:00:00'))
    window_size = 60  # seconds per bin
    windows = []
    for ws in range(start_t, end_t, window_size):
        we = min(ws + window_size, end_t)
        windows.append({'start_s': ws, 'end_s': we,
                        'start_time': to_time(ws), 'end_time': to_time(we)})

    # Precompute highlight midpoints
    hl_items = []
    for item in highlights.get('highlights', []):
        s = to_seconds(item['start_timestamp'])
        e = to_seconds(item['end_timestamp'])
        hl_items.append({'arc_id': item['arc_id'], 'mid': (s + e)/2})

    # Build records: one row per window per arc if arc covers window
    records = []
    for w in windows:
        for arc in storyline.get('story_arcs', []):
            a_start = to_seconds(arc['start_timestamp'])
            a_end = to_seconds(arc['end_timestamp'])
            if not (w['end_s'] <= a_start or w['start_s'] >= a_end):
                cnt = sum(1 for h in hl_items if h['arc_id'] == arc['arc_id']
                          and w['start_s'] <= h['mid'] < w['end_s'])
                records.append({
                    'start_time': w['start_time'],
                    'arc_title': arc['title'],
                    'highlight_count': cnt
                })
    df = pd.DataFrame(records)
    if df.empty:
        solara.Markdown("No data available for highlight density plot.")
        return

    # State for click selection
    selected = solara.use_state(None)

    # Heatmap: window start vs arc, intensity = highlight count
    heatmap = (
        alt.Chart(df, title='Highlights Density per Story Arc Over Time')
        .mark_rect()
        .encode(
            x=alt.X('start_time:N', title='Window Start', sort=None),
            y=alt.Y('arc_title:N', title='Story Arc', sort=[arc['title'] for arc in storyline.get('story_arcs', [])]),
            color=alt.Color('highlight_count:Q', title='Highlights', scale=alt.Scale(scheme='orangered')),
            tooltip=[
                alt.Tooltip('start_time', title='Window Start'),
                alt.Tooltip('arc_title', title='Arc'),
                alt.Tooltip('highlight_count', title='Highlights')
            ]
        )
        .properties(width='container', height=400)
    )

    # Display chart with click handler
    with solara.Card('Story arcs & highlights over time'):
        solara.AltairChart(heatmap)
    ViewerJSON("STORYLINE.json", id_field="arc_id")

@solara.component
def Episode():
    solara.Markdown("The assembled episode cut: chosen arcs, highlights, characters, and dialogue snippets mapped to a target run time.")
    ViewerJSON("EPISODE.json", id_field="episode_id")

@solara.component
def Teaser():
    solara.Markdown("A concise preview edit: top highlights and key moments distilled into a short teaser with associated timestamps and descriptions.")
    ViewerJSON("TEASER.json", id_field="teaser_id")

@solara.component
def Editorial():
    solara.Markdown("A collection of review questions and pending tasks—each with status, priority, and related highlights—guiding editorial decisions and improvements.")
    # Freeform text viewer
    ViewerJSON("EDITORIAL.json")

routes = [
    solara.Route(path='/', component=Home, label='RushXplorer'),
    solara.Route(path='transcript', component=Transcript, label='Transcript'),
    solara.Route(path='characters', component=Characters, label='Characters'),
    solara.Route(path='storyline', component=Storyline, label='Storyline'),
    solara.Route(path='highlights', component=Highlights, label='Highlights'),
    solara.Route(path='episode', component=Episode, label='Episode'),
    solara.Route(path='teaser', component=Teaser, label='Teaser'),
    solara.Route(path='editorial', component=Editorial, label='Editorial'),
]