GenAILearniverse commited on
Commit
b73d62b
·
verified ·
1 Parent(s): c7201b1

Create app.py

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