Hwilner commited on
Commit
3373fa0
·
verified ·
1 Parent(s): ca722e8

Create app.py

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