HarshanaLF commited on
Commit
ff1d636
·
verified ·
1 Parent(s): ad4937f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -6
app.py CHANGED
@@ -4,9 +4,20 @@ from youtube_transcript_api import YouTubeTranscriptApi
4
  from youtube_transcript_api.formatters import TextFormatter
5
  import re
6
 
7
- # Load the summarization model
8
- text_summary = pipeline("summarization", model="Falconsai/text_summarization")
 
 
 
9
 
 
 
 
 
 
 
 
 
10
  def extract_video_id(url):
11
  regex = r"(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})"
12
  match = re.search(regex, url)
@@ -14,6 +25,7 @@ def extract_video_id(url):
14
  return match.group(1)
15
  return None
16
 
 
17
  def get_youtube_transcript(video_url):
18
  video_id = extract_video_id(video_url)
19
  if not video_id:
@@ -26,20 +38,25 @@ def get_youtube_transcript(video_url):
26
  except Exception as e:
27
  return f"An error occurred: {e}"
28
 
29
- def summarize_youtube_video(url):
 
30
  transcript = get_youtube_transcript(url)
31
  if "An error occurred" in transcript:
32
  return transcript
33
- summary = text_summary(transcript, min_length=10, max_length=1000, do_sample=False)
 
34
  return summary[0]['summary_text']
35
 
36
  # Define the Gradio interface
37
  iface = gr.Interface(
38
  fn=summarize_youtube_video,
39
- inputs=gr.Textbox(label="Enter YouTube Video URL", placeholder="e.g. https://www.youtube.com/watch?v=abcdef12345"),
 
 
 
40
  outputs=gr.Textbox(label="Video Summary"),
41
  title="YouTube Video Summarizer",
42
- description="Enter the URL of a YouTube video to get a summary of its transcript."
43
  )
44
 
45
  if __name__ == "__main__":
 
4
  from youtube_transcript_api.formatters import TextFormatter
5
  import re
6
 
7
+ # Define the models
8
+ models = {
9
+ "Falconsai/text_summarization": "Falconsai/text_summarization",
10
+ "suriya7/bart-finetuned-text-summarization": "suriya7/bart-finetuned-text-summarization"
11
+ }
12
 
13
+ # Default model
14
+ default_model = "Falconsai/text_summarization"
15
+
16
+ # Function to create a summarization pipeline
17
+ def create_summarization_pipeline(model_name):
18
+ return pipeline("summarization", model=model_name)
19
+
20
+ # Function to extract video ID from URL
21
  def extract_video_id(url):
22
  regex = r"(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})"
23
  match = re.search(regex, url)
 
25
  return match.group(1)
26
  return None
27
 
28
+ # Function to get YouTube transcript
29
  def get_youtube_transcript(video_url):
30
  video_id = extract_video_id(video_url)
31
  if not video_id:
 
38
  except Exception as e:
39
  return f"An error occurred: {e}"
40
 
41
+ # Function to summarize YouTube video with selected model
42
+ def summarize_youtube_video(url, model_name):
43
  transcript = get_youtube_transcript(url)
44
  if "An error occurred" in transcript:
45
  return transcript
46
+ summarization_pipeline = create_summarization_pipeline(model_name)
47
+ summary = summarization_pipeline(transcript, min_length=10, max_length=1000, do_sample=False)
48
  return summary[0]['summary_text']
49
 
50
  # Define the Gradio interface
51
  iface = gr.Interface(
52
  fn=summarize_youtube_video,
53
+ inputs=[
54
+ gr.Textbox(label="Enter YouTube Video URL", placeholder="e.g. https://www.youtube.com/watch?v=abcdef12345"),
55
+ gr.Dropdown(choices=list(models.keys()), value=default_model, label="Select Summarization Model")
56
+ ],
57
  outputs=gr.Textbox(label="Video Summary"),
58
  title="YouTube Video Summarizer",
59
+ description="Enter the URL of a YouTube video and select a summarization model to get a summary of its transcript."
60
  )
61
 
62
  if __name__ == "__main__":