tech-rakesh-ai commited on
Commit
fabef8e
1 Parent(s): 6e28871

Update app.py

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