neuralworm commited on
Commit
f4bd310
·
verified ·
1 Parent(s): 9d9f408

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -49
app.py CHANGED
@@ -1,61 +1,49 @@
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>")
@@ -66,11 +54,10 @@ with gr.Blocks() as demo:
66
  input_text_url = gr.Textbox(placeholder='Youtube video URL', label='URL')
67
  result_button_transcribe = gr.Button('1. Transcribe')
68
  output_text_transcribe = gr.Textbox(placeholder='Transcript of the YouTube video.', label='Transcript')
69
-
70
  #result_button_summary = gr.Button('2. Create Summary')
71
  #output_text_summary = gr.Textbox(placeholder='Summary of the YouTube video transcript.', label='Summary')
72
-
73
  result_button_transcribe.click(get_text, inputs = input_text_url, outputs = output_text_transcribe)
74
  #result_button_summary.click(get_summary, inputs = output_text_transcribe, outputs = output_text_summary)
75
 
76
- demo.queue(default_enabled = True).launch(debug = True)
 
1
  import whisper
2
+ import yt_dlp
 
3
  import gradio as gr
4
  import os
5
  import re
6
 
7
  model = whisper.load_model("base")
 
8
 
9
+ def get_audio(url):
10
+ try:
11
+ ydl_opts = {
12
+ 'format': 'bestaudio/best',
13
+ 'outtmpl': '%(id)s.%(ext)s',
14
+ 'noplaylist': True,
15
+ 'quiet': True
16
+ }
17
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
18
+ info = ydl.extract_info(url, download=True)
19
+ audio_file = os.path.join(ydl.outtmpl % info)
20
+ return audio_file
21
+ except Exception as e:
22
+ raise gr.Error(f"Exception: {e}")
 
 
23
 
24
  def get_text(url):
25
+ try:
26
+ if url != '':
27
+ audio_file = get_audio(url)
28
+ result = model.transcribe(audio_file)
29
+ return result['text'].strip()
30
+ else:
31
+ return "Please enter a YouTube video URL."
32
+ except Exception as e:
33
+ raise gr.Error(f"Exception: {e}")
 
 
 
 
 
 
 
 
 
 
34
 
35
  def get_summary(article):
36
+ try:
37
+ first_sentences = ' '.join(re.split(r'(?<=[.:;])\s', article)[:5])
38
+ # Assuming you have a summarizer pipeline set up
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
+ # Since no summarizer is defined, return the first sentences for now
43
+ return first_sentences
44
+ except Exception as e:
45
+ raise gr.Error(f"Exception: {e}")
46
 
 
47
  with gr.Blocks() as demo:
48
  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>")
49
  #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>")
 
54
  input_text_url = gr.Textbox(placeholder='Youtube video URL', label='URL')
55
  result_button_transcribe = gr.Button('1. Transcribe')
56
  output_text_transcribe = gr.Textbox(placeholder='Transcript of the YouTube video.', label='Transcript')
 
57
  #result_button_summary = gr.Button('2. Create Summary')
58
  #output_text_summary = gr.Textbox(placeholder='Summary of the YouTube video transcript.', label='Summary')
59
+
60
  result_button_transcribe.click(get_text, inputs = input_text_url, outputs = output_text_transcribe)
61
  #result_button_summary.click(get_summary, inputs = output_text_transcribe, outputs = output_text_summary)
62
 
63
+ demo.queue(default_enabled = True).launch(debug = True)