moFouad1 commited on
Commit
d98ef73
·
verified ·
1 Parent(s): 05718b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -58,7 +58,6 @@ def download_audio_youtube(url, output_path="audio.wav", cookies_path=None):
58
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
59
  ydl.download([url])
60
  except Exception as e:
61
- # On failure, run yt-dlp in subprocess to list formats
62
  try:
63
  list_cmd = ["yt-dlp", "-F", url]
64
  if cookies_path:
@@ -66,7 +65,7 @@ def download_audio_youtube(url, output_path="audio.wav", cookies_path=None):
66
  result = subprocess.run(list_cmd, capture_output=True, text=True, timeout=15)
67
  formats = result.stdout or "No formats found."
68
  except Exception as format_err:
69
- formats = f"⚠️ Could not list formats due to: {format_err}"
70
 
71
  raise RuntimeError(
72
  f"yt-dlp failed: {e}\n\n"
@@ -174,14 +173,19 @@ def generate_questions_with_pipeline(text, num_questions=5):
174
 
175
  # === MAIN FUNCTION ===
176
 
177
- def process_input_gradio(url_input, file_input, cookies_file):
178
  try:
179
  cookies_path = None
180
  if cookies_file is not None:
181
  cookies_path = "cookies.txt"
182
  shutil.copyfile(cookies_file.name, cookies_path)
183
 
184
- if file_input is not None:
 
 
 
 
 
185
  audio_path = extract_audio_from_video(file_input.name)
186
  chunks, sr = split_audio(audio_path, chunk_length_sec=15)
187
  transcript = transcribe_chunks_dataset(chunks, sr)
@@ -196,14 +200,14 @@ def process_input_gradio(url_input, file_input, cookies_file):
196
  chunks, sr = split_audio(audio_path, chunk_length_sec=15)
197
  transcript = transcribe_chunks_dataset(chunks, sr)
198
  except Exception as e:
199
- return f"⚠️ Could not download this YouTube video due to restrictions. Please upload the video manually.\nDetails: {e}", ""
200
  else:
201
  video_file = download_video_direct(url_input)
202
  audio_path = extract_audio_from_video(video_file)
203
  chunks, sr = split_audio(audio_path, chunk_length_sec=15)
204
  transcript = transcribe_chunks_dataset(chunks, sr)
205
  else:
206
- return "Please provide a URL or upload a video file.", ""
207
 
208
  summary = summarize_with_bart(transcript)
209
  questions = generate_questions_with_pipeline(summary)
@@ -219,13 +223,14 @@ iface = gr.Interface(
219
  gr.Textbox(label="YouTube or Direct Video URL", placeholder="https://..."),
220
  gr.File(label="Or Upload a Video File", file_types=[".mp4", ".mkv", ".webm"]),
221
  gr.File(label="Optional cookies.txt for YouTube", file_types=[".txt"]),
 
222
  ],
223
  outputs=[
224
  gr.Textbox(label="Summary", lines=10),
225
  gr.Textbox(label="Generated Questions", lines=10),
226
  ],
227
  title="Lecture Summary & Question Generator",
228
- description="Provide a YouTube/Direct video URL or upload a video file. If the video is restricted, upload cookies.txt or the video file directly."
229
  )
230
 
231
- iface.launch()
 
58
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
59
  ydl.download([url])
60
  except Exception as e:
 
61
  try:
62
  list_cmd = ["yt-dlp", "-F", url]
63
  if cookies_path:
 
65
  result = subprocess.run(list_cmd, capture_output=True, text=True, timeout=15)
66
  formats = result.stdout or "No formats found."
67
  except Exception as format_err:
68
+ formats = f"\u26a0\ufe0f Could not list formats due to: {format_err}"
69
 
70
  raise RuntimeError(
71
  f"yt-dlp failed: {e}\n\n"
 
173
 
174
  # === MAIN FUNCTION ===
175
 
176
+ def process_input_gradio(url_input, file_input, cookies_file, text_input):
177
  try:
178
  cookies_path = None
179
  if cookies_file is not None:
180
  cookies_path = "cookies.txt"
181
  shutil.copyfile(cookies_file.name, cookies_path)
182
 
183
+ transcript = ""
184
+
185
+ if text_input:
186
+ transcript = text_input.strip()
187
+
188
+ elif file_input is not None:
189
  audio_path = extract_audio_from_video(file_input.name)
190
  chunks, sr = split_audio(audio_path, chunk_length_sec=15)
191
  transcript = transcribe_chunks_dataset(chunks, sr)
 
200
  chunks, sr = split_audio(audio_path, chunk_length_sec=15)
201
  transcript = transcribe_chunks_dataset(chunks, sr)
202
  except Exception as e:
203
+ return f"\u26a0\ufe0f Could not download this YouTube video due to restrictions. Please upload the video manually.\nDetails: {e}", ""
204
  else:
205
  video_file = download_video_direct(url_input)
206
  audio_path = extract_audio_from_video(video_file)
207
  chunks, sr = split_audio(audio_path, chunk_length_sec=15)
208
  transcript = transcribe_chunks_dataset(chunks, sr)
209
  else:
210
+ return "Please provide a URL, upload a video file, or paste text.", ""
211
 
212
  summary = summarize_with_bart(transcript)
213
  questions = generate_questions_with_pipeline(summary)
 
223
  gr.Textbox(label="YouTube or Direct Video URL", placeholder="https://..."),
224
  gr.File(label="Or Upload a Video File", file_types=[".mp4", ".mkv", ".webm"]),
225
  gr.File(label="Optional cookies.txt for YouTube", file_types=[".txt"]),
226
+ gr.Textbox(label="Or Paste Transcript/Text Directly", lines=10, placeholder="Paste transcript or text here...")
227
  ],
228
  outputs=[
229
  gr.Textbox(label="Summary", lines=10),
230
  gr.Textbox(label="Generated Questions", lines=10),
231
  ],
232
  title="Lecture Summary & Question Generator",
233
+ description="Provide a YouTube/Direct video URL, upload a video file, or paste text. If the video is restricted, upload cookies.txt or the video file directly."
234
  )
235
 
236
+ iface.launch()