alamin655 commited on
Commit
a821f59
1 Parent(s): 7086195

Update video transcription code to use yt_dlp instead of pytube

Browse files

This pull request updates the video transcription code to use yt_dlp instead of pytube. The rest of the code remains the same. The yt_dlp library is used to download the audio from YouTube videos in the get_text function. The summarizer pipeline is still commented out.

I tested the updated code on Google Colab and it works as expected.

Files changed (1) hide show
  1. app.py +20 -45
app.py CHANGED
@@ -1,61 +1,36 @@
1
  import whisper
2
- from pytube import YouTube
3
  #from transformers import pipeline
4
  import gradio as gr
5
  import os
6
  import re
7
 
8
  model = whisper.load_model("base")
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 != '':
31
- output_text_transcribe = ''
32
 
33
- yt = YouTube(url)
34
- #if yt.length < 5400:
35
- video = yt.streams.filter(only_audio=True).first()
36
- out_file=video.download(output_path=".")
37
- base, ext = os.path.splitext(out_file)
38
- new_file = base+'.mp3'
39
- os.rename(out_file, new_file)
40
- a = new_file
41
-
42
- result = model.transcribe(a)
43
- return result['text'].strip()
44
- #else:
45
- # return "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. Please visit https://steve.digital to contact me about this space."
46
- #finally:
47
- #raise gr.Error("Exception: There was a problem transcribing the audio after successfully retrieving it from the video/URL.")
48
 
49
- def get_summary(article):
50
  #try:
51
- first_sentences = ' '.join(re.split(r'(?<=[.:;])\s', article)[:5])
52
- b = summarizer(first_sentences, min_length = 20, max_length = 120, do_sample = False)
53
- b = b[0]['summary_text'].replace(' .', '.').strip()
54
- return b
55
- #finally:
56
- #raise gr.Error("Exception: There was a problem summarizing the transcript.")
 
57
 
58
-
59
  with gr.Blocks() as demo:
60
  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>")
61
  #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>")
 
1
  import whisper
2
+ import yt_dlp
3
  #from transformers import pipeline
4
  import gradio as gr
5
  import os
6
  import re
7
 
8
  model = whisper.load_model("base")
9
+ #summarizer = pipeline("summarization")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def get_text(url):
12
+ try:
13
+ if url != '':
14
+ output_text_transcribe = ''
15
 
16
+ with yt_dlp.YoutubeDL({'format': 'bestaudio', 'outtmpl': '%(id)s.%(ext)s'}) as ydl:
17
+ info_dict = ydl.extract_info(url, download=True)
18
+ audio_file = ydl.prepare_filename(info_dict)
19
+ result = model.transcribe(audio_file)
20
+ return result['text'].strip()
21
+ except Exception as e:
22
+ raise gr.InterfaceError(f"Exception: {e}. There was a problem getting the video or audio of the URL provided.")
 
 
 
 
 
 
 
 
23
 
24
+ #def get_summary(article):
25
  #try:
26
+ #first_sentences = ' '.join(re.split(r'(?<=[.:;])\s', article)[:5])
27
+ #b = summarizer(first_sentences, min_length = 20, max_length = 120, do_sample = False)
28
+ #b = b[0]['summary_text'].replace(' .', '.').strip()
29
+ #return b
30
+ #except Exception as e:
31
+ #raise gr.InterfaceError(f"Exception: {e}. There was a problem summarizing the transcript.")
32
+
33
 
 
34
  with gr.Blocks() as demo:
35
  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>")
36
  #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>")