maliahson commited on
Commit
353620b
·
verified ·
1 Parent(s): 2aa9457

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -50
app.py CHANGED
@@ -1,75 +1,45 @@
1
- import re
2
  import gradio as gr
 
3
  from youtube_utils import get_youtube_captions, summarize_large_text_with_bart
4
 
 
5
  def extract_video_id(url):
6
- """
7
- Extract video ID from YouTube URLs using regular expressions.
8
- """
9
  patterns = [
10
  r'(?:v=|\/)([0-9A-Za-z_-]{11}).*', # Standard and shortened URLs
11
  r'(?:embed\/)([0-9A-Za-z_-]{11})', # Embed URLs
12
  r'(?:youtu\.be\/)([0-9A-Za-z_-]{11})' # Shortened URLs
13
  ]
 
14
  for pattern in patterns:
15
  match = re.search(pattern, url)
16
  if match:
17
  return match.group(1)
18
  return None
19
 
20
- def summarize_youtube_video(video_url):
21
- """
22
- Summarize YouTube video by extracting captions and applying summarization.
23
- """
24
  try:
25
- # Extract video ID
26
  video_id = extract_video_id(video_url)
27
  if not video_id:
28
- return "Invalid YouTube URL. Please provide a valid link.", None
29
-
30
- # Fetch captions
31
  captions = get_youtube_captions(video_id)
32
  if not captions:
33
- return "Unable to fetch captions for this video. Ensure it has subtitles.", None
34
-
35
- # Generate summary
36
  summary = summarize_large_text_with_bart(captions)
37
- return summary, f"https://www.youtube.com/watch?v={video_id}"
38
-
39
  except Exception as e:
40
- return f"An error occurred: {str(e)}", None
41
-
42
- # Gradio Interface
43
- def create_gradio_interface():
44
- """
45
- Create and return the Gradio Blocks interface.
46
- """
47
- with gr.Blocks(title="YouTube Video Summarizer") as interface:
48
- gr.Markdown("# 🎥 YouTube Video Summarizer")
49
- gr.Markdown(
50
- "Provide a YouTube video URL, and this tool will extract captions and summarize the content using a BART model."
51
- )
52
-
53
- with gr.Row():
54
- video_url_input = gr.Textbox(
55
- label="YouTube Video URL",
56
- placeholder="Enter the URL of the YouTube video"
57
- )
58
- summarize_button = gr.Button("Summarize")
59
-
60
- with gr.Row():
61
- summary_output = gr.Textbox(label="Summary", lines=10, interactive=False)
62
- video_link_output = gr.Textbox(label="YouTube Video Link", interactive=False)
63
-
64
- summarize_button.click(
65
- fn=summarize_youtube_video,
66
- inputs=[video_url_input],
67
- outputs=[summary_output, video_link_output]
68
- )
69
 
70
- return interface
 
 
 
 
 
 
 
71
 
72
- # Launch the interface
73
  if __name__ == "__main__":
74
- interface = create_gradio_interface()
75
- interface.launch()
 
 
1
  import gradio as gr
2
+ import re
3
  from youtube_utils import get_youtube_captions, summarize_large_text_with_bart
4
 
5
+ # Helper function to extract video ID from YouTube URL
6
  def extract_video_id(url):
 
 
 
7
  patterns = [
8
  r'(?:v=|\/)([0-9A-Za-z_-]{11}).*', # Standard and shortened URLs
9
  r'(?:embed\/)([0-9A-Za-z_-]{11})', # Embed URLs
10
  r'(?:youtu\.be\/)([0-9A-Za-z_-]{11})' # Shortened URLs
11
  ]
12
+
13
  for pattern in patterns:
14
  match = re.search(pattern, url)
15
  if match:
16
  return match.group(1)
17
  return None
18
 
19
+ # Gradio function to process summarization
20
+ def summarize_video(video_url):
 
 
21
  try:
 
22
  video_id = extract_video_id(video_url)
23
  if not video_id:
24
+ return "Invalid YouTube URL"
25
+
 
26
  captions = get_youtube_captions(video_id)
27
  if not captions:
28
+ return "Unable to fetch video captions"
29
+
 
30
  summary = summarize_large_text_with_bart(captions)
31
+ return summary
 
32
  except Exception as e:
33
+ return f"An error occurred: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
+ # Gradio interface
36
+ iface = gr.Interface(
37
+ fn=summarize_video,
38
+ inputs=gr.Textbox(label="YouTube Video URL", placeholder="Enter the YouTube video URL here"),
39
+ outputs=gr.Textbox(label="Summary", lines=5),
40
+ title="YouTube Video Summarizer",
41
+ description="Enter a YouTube video URL to extract captions and summarize them using AI."
42
+ )
43
 
 
44
  if __name__ == "__main__":
45
+ iface.launch()