adelevett commited on
Commit
2b3d36c
·
1 Parent(s): 524ad6a

Fix Sagan video loading: robust fallback video ID extraction from filename/transcript, SRT file upload support, and privacy-enhanced YouTube host

Browse files
Files changed (2) hide show
  1. app.py +47 -15
  2. theater_template.html +1 -0
app.py CHANGED
@@ -61,31 +61,63 @@ def play_simulation_selection(selection: str) -> str:
61
  return ""
62
 
63
  def handle_generation(yt_url: str, pdf_file, doc_text: str, srt_text: str, hf_token: str):
64
- # 1. Validate YouTube Link
65
  video_id = extract_youtube_video_id(yt_url)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  if not video_id:
67
  return (
68
  gr.update(),
69
- "### ❌ Error\nInvalid YouTube URL. Please provide a valid YouTube link or 11-character Video ID.",
70
  gr.update()
71
  )
72
 
73
- # 2. Identify Document Source
74
  doc_path = None
75
  document_content = None
 
 
 
 
76
  if pdf_file is not None:
77
- doc_path = pdf_file.name
78
- elif doc_text.strip():
79
- document_content = doc_text.strip()
 
 
 
 
 
 
 
 
 
 
80
  else:
81
- return (
82
- gr.update(),
83
- "### ❌ Error\nPlease upload a PDF/text file or paste some reference document text.",
84
- gr.update()
85
- )
86
-
87
- # 3. Clean manual transcript input if any
88
- manual_transcript = srt_text.strip() if srt_text.strip() else None
 
 
 
89
 
90
  # 4. Use provided token or default token
91
  token = hf_token.strip() if hf_token.strip() else DEFAULT_HF_TOKEN
@@ -298,7 +330,7 @@ with gr.Blocks(title="ReLiveStream - Interactive Replay", theme=custom_theme, cs
298
 
299
  pdf_input = gr.File(
300
  label="Upload Reference PDF/Text",
301
- file_types=[".pdf", ".txt"],
302
  file_count="single"
303
  )
304
 
 
61
  return ""
62
 
63
  def handle_generation(yt_url: str, pdf_file, doc_text: str, srt_text: str, hf_token: str):
64
+ # 1. Extract YouTube Video ID with fallback to uploaded filename or SRT text content
65
  video_id = extract_youtube_video_id(yt_url)
66
+
67
+ # Fallback 1: Extract from the uploaded PDF/text filename if yt_url is empty
68
+ if not video_id and pdf_file is not None:
69
+ filename = os.path.basename(pdf_file.name)
70
+ video_id = extract_youtube_video_id(filename)
71
+
72
+ # Fallback 2: Extract from the SRT text content (first 5 lines) if still empty
73
+ if not video_id and srt_text:
74
+ lines = srt_text.split('\n')[:5]
75
+ for line in lines:
76
+ extracted = extract_youtube_video_id(line)
77
+ if extracted:
78
+ video_id = extracted
79
+ break
80
+
81
  if not video_id:
82
  return (
83
  gr.update(),
84
+ "### ❌ Error\nInvalid YouTube URL. Please provide a valid YouTube link or 11-character Video ID in the input box, or name your uploaded transcript file with the video ID (e.g. `v=VIDEO_ID.srt`).",
85
  gr.update()
86
  )
87
 
88
+ # 2. Identify Document Source and Transcript Source
89
  doc_path = None
90
  document_content = None
91
+ manual_transcript = None
92
+
93
+ # Check if the uploaded file is actually an SRT transcript
94
+ uploaded_as_srt = False
95
  if pdf_file is not None:
96
+ filename = os.path.basename(pdf_file.name)
97
+ if filename.endswith(".srt") or filename.endswith(".srt.txt") or "v=" in filename:
98
+ uploaded_as_srt = True
99
+
100
+ if uploaded_as_srt:
101
+ # Read the uploaded SRT file as the manual transcript
102
+ with open(pdf_file.name, "r", encoding="utf-8") as f:
103
+ manual_transcript = f.read()
104
+ # If no separate reference document is pasted, default to using the transcript itself
105
+ if doc_text.strip():
106
+ document_content = doc_text.strip()
107
+ else:
108
+ document_content = manual_transcript
109
  else:
110
+ if pdf_file is not None:
111
+ doc_path = pdf_file.name
112
+ elif doc_text.strip():
113
+ document_content = doc_text.strip()
114
+ else:
115
+ return (
116
+ gr.update(),
117
+ "### Error\nPlease upload a PDF/text file or paste some reference document text.",
118
+ gr.update()
119
+ )
120
+ manual_transcript = srt_text.strip() if srt_text.strip() else None
121
 
122
  # 4. Use provided token or default token
123
  token = hf_token.strip() if hf_token.strip() else DEFAULT_HF_TOKEN
 
330
 
331
  pdf_input = gr.File(
332
  label="Upload Reference PDF/Text",
333
+ file_types=[".pdf", ".txt", ".srt"],
334
  file_count="single"
335
  )
336
 
theater_template.html CHANGED
@@ -482,6 +482,7 @@
482
  player = new YT.Player('player', {
483
  height: '100%',
484
  width: '100%',
 
485
  videoId: videoId,
486
  playerVars: {
487
  'playsinline': 1,
 
482
  player = new YT.Player('player', {
483
  height: '100%',
484
  width: '100%',
485
+ host: 'https://www.youtube-nocookie.com',
486
  videoId: videoId,
487
  playerVars: {
488
  'playsinline': 1,