kasper-boy commited on
Commit
3b2fd95
·
verified ·
1 Parent(s): fd070cf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import gradio as gr
3
+ import torch
4
+ from transformers import pipeline
5
+ from youtube_transcript_api import YouTubeTranscriptApi
6
+ from youtube_transcript_api.formatters import TextFormatter
7
+
8
+ # Initialize the summarization pipeline
9
+ text_summary = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", torch_dtype=torch.bfloat16)
10
+
11
+ def summary(text_youtube):
12
+ output = text_summary(text_youtube)
13
+ return output[0]['summary_text']
14
+
15
+ def extract_video_id(url):
16
+ # Regex to extract the video ID from various YouTube URL formats
17
+ regex = r"(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})"
18
+ match = re.search(regex, url)
19
+ if match:
20
+ return match.group(1)
21
+ return None
22
+
23
+ def get_youtube_transcript(video_url):
24
+ video_id = extract_video_id(video_url)
25
+ if not video_id:
26
+ return "Video ID could not be extracted.", ""
27
+
28
+ try:
29
+ # Fetch the transcript
30
+ transcript = YouTubeTranscriptApi.get_transcript(video_id)
31
+
32
+ # Format the transcript into plain text
33
+ formatter = TextFormatter()
34
+ text_transcript = formatter.format_transcript(transcript)
35
+ summary_text = summary(text_transcript)
36
+
37
+ return text_transcript, summary_text
38
+ except Exception as e:
39
+ return f"An error occurred: {e}", ""
40
+
41
+ # Remove any existing Gradio interfaces
42
+ gr.close_all()
43
+
44
+ # Define the Gradio interface
45
+ demo = gr.Interface(
46
+ fn=get_youtube_transcript,
47
+ inputs=[gr.Textbox(label="Input YouTube URL to summarize", lines=1)],
48
+ outputs=[
49
+ gr.Textbox(label="Text extracted from video", lines=4),
50
+ gr.Textbox(label="Summarized text", lines=4)
51
+ ],
52
+ title="YouTube Script for Text Extract & Summarizer"
53
+ )
54
+
55
+ # Launch the Gradio interface
56
+ demo.launch(auth=('youtube','test@123'))