SteveDigital commited on
Commit
5188c19
1 Parent(s): b513c56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -36
app.py CHANGED
@@ -9,45 +9,54 @@ model = whisper.load_model("base")
9
  summarizer = pipeline("summarization")
10
 
11
  def get_audio(url):
12
- yt = YouTube(url)
13
- if yt.length < 5400:
14
- video = yt.streams.filter(only_audio=True).first()
15
- out_file=video.download(output_path=".")
16
- base, ext = os.path.splitext(out_file)
17
- new_file = base+'.mp3'
18
- os.rename(out_file, new_file)
19
- a = new_file
20
- return a
21
- else:
22
- raise gr.Error("Videos for transcription on this space are limited to 1.5 hours. Sorry about this limit but some joker thought they could stop this tool from working by transcribing many extremely long videos.")
23
- return ""
 
 
 
24
 
25
  def get_text(url):
26
- if url != '' : output_text_transcribe = ''
27
- result = model.transcribe(get_audio(url))
28
- return result['text'].strip()
 
 
 
29
 
30
  def get_summary(article):
31
- first_sentences = ' '.join(re.split(r'(?<=[.:;])\s', article)[:5])
32
- b = summarizer(first_sentences, min_length = 20, max_length = 120, do_sample = False)
33
- b = b[0]['summary_text'].replace(' .', '.').strip()
34
-
35
- return b
 
 
 
36
 
37
  with gr.Blocks() as demo:
38
- gr.Markdown("<h1><center>Free Fast YouTube URL Video-to-Text using <a href=https://openai.com/blog/whisper/ target=_blank>OpenAI's Whisper</a> Model</center></h1>")
39
- gr.Markdown("<center>Enter the link of any YouTube video to generate a text transcript of the video and then create a summary of the video transcript.</center>")
40
- gr.Markdown("<center><b>'Whisper is a neural net that approaches human level robustness and accuracy on English speech recognition.'</b></center>")
41
- gr.Markdown("<center>Transcription takes 5-10 seconds per minute of the video (bad audio/hard accents slow it down a bit). #patience<br />If you have time while waiting, check out my <a href=https://www.artificial-intelligence.blog target=_blank>AI blog</a> (opens in new tab).</center>")
42
-
43
- input_text_url = gr.Textbox(placeholder='Youtube video URL', label='URL')
44
- result_button_transcribe = gr.Button('1. Transcribe')
45
- output_text_transcribe = gr.Textbox(placeholder='Transcript of the YouTube video.', label='Transcript')
46
-
47
- result_button_summary = gr.Button('2. Create Summary')
48
- output_text_summary = gr.Textbox(placeholder='Summary of the YouTube video transcript.', label='Summary')
49
-
50
- result_button_transcribe.click(get_text, inputs = input_text_url, outputs = output_text_transcribe)
51
- result_button_summary.click(get_summary, inputs = output_text_transcribe, outputs = output_text_summary)
52
-
53
- demo.queue(default_enabled=False).launch(debug = True)
 
9
  summarizer = pipeline("summarization")
10
 
11
  def get_audio(url):
12
+ try:
13
+ yt = YouTube(url)
14
+ if yt.length < 5400:
15
+ video = yt.streams.filter(only_audio=True).first()
16
+ out_file=video.download(output_path=".")
17
+ base, ext = os.path.splitext(out_file)
18
+ new_file = base+'.mp3'
19
+ os.rename(out_file, new_file)
20
+ a = new_file
21
+ return a
22
+ else:
23
+ raise gr.Error("Videos for transcription on this space are limited to 1.5 hours. Sorry about this limit but some joker thought they could stop this tool from working by transcribing many extremely long videos.")
24
+ #return ""
25
+ finally:
26
+ raise gr.Error("Exception: There was a problem getting the video or audio of the URL provided.")
27
 
28
  def get_text(url):
29
+ try:
30
+ if url != '' : output_text_transcribe = ''
31
+ result = model.transcribe(get_audio(url))
32
+ return result['text'].strip()
33
+ finally:
34
+ raise gr.Error("Exception: There was a problem transcribing the audio after successfully retrieving it from the video/URL.")
35
 
36
  def get_summary(article):
37
+ try:
38
+ first_sentences = ' '.join(re.split(r'(?<=[.:;])\s', article)[:5])
39
+ b = summarizer(first_sentences, min_length = 20, max_length = 120, do_sample = False)
40
+ b = b[0]['summary_text'].replace(' .', '.').strip()
41
+ return b
42
+ finally:
43
+ raise gr.Error("Exception: There was a problem summarizing the transcript.")
44
+
45
 
46
  with gr.Blocks() as demo:
47
+ gr.Markdown("<h1><center>Free Fast YouTube URL Video-to-Text using <a href=https://openai.com/blog/whisper/ target=_blank>OpenAI's Whisper</a> Model</center></h1>")
48
+ gr.Markdown("<center>Enter the link of any YouTube video to generate a text transcript of the video and then create a summary of the video transcript.</center>")
49
+ gr.Markdown("<center><b>'Whisper is a neural net that approaches human level robustness and accuracy on English speech recognition.'</b></center>")
50
+ gr.Markdown("<center>Transcription takes 5-10 seconds per minute of the video (bad audio/hard accents slow it down a bit). #patience<br />If you have time while waiting, check out my <a href=https://www.artificial-intelligence.blog target=_blank>AI blog</a> (opens in new tab).</center>")
51
+
52
+ input_text_url = gr.Textbox(placeholder='Youtube video URL', label='URL')
53
+ result_button_transcribe = gr.Button('1. Transcribe')
54
+ output_text_transcribe = gr.Textbox(placeholder='Transcript of the YouTube video.', label='Transcript')
55
+
56
+ result_button_summary = gr.Button('2. Create Summary')
57
+ output_text_summary = gr.Textbox(placeholder='Summary of the YouTube video transcript.', label='Summary')
58
+
59
+ result_button_transcribe.click(get_text, inputs = input_text_url, outputs = output_text_transcribe)
60
+ result_button_summary.click(get_summary, inputs = output_text_transcribe, outputs = output_text_summary)
61
+
62
+ demo.queue(default_enabled = True).launch(debug = True)