neuralworm
commited on
Commit
•
6101aae
1
Parent(s):
4a71c2c
implement yt-dlp
Browse files
app.py
CHANGED
@@ -1,61 +1,55 @@
|
|
1 |
import whisper
|
2 |
-
|
3 |
import gradio as gr
|
4 |
import os
|
5 |
import re
|
6 |
-
import logging
|
7 |
|
8 |
-
logging.basicConfig(level=logging.INFO)
|
9 |
model = whisper.load_model("base")
|
10 |
|
11 |
-
def
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
return result['text'].strip()
|
33 |
-
else:
|
34 |
-
logging.error('Videos for transcription on this space are limited to about 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.')
|
35 |
-
#finally:
|
36 |
-
# raise gr.Error("Exception: There was a problem transcribing the audio.")
|
37 |
|
38 |
def get_summary(article):
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
43 |
|
44 |
with gr.Blocks() as demo:
|
45 |
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>")
|
46 |
-
#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>")
|
47 |
gr.Markdown("<center>Enter the link of any YouTube video to generate a text transcript of the video.</center>")
|
48 |
gr.Markdown("<center><b>'Whisper is a neural net that approaches human level robustness and accuracy on English speech recognition.'</b></center>")
|
49 |
-
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,
|
50 |
|
51 |
-
input_text_url = gr.Textbox(placeholder='Youtube video URL', label='
|
52 |
-
result_button_transcribe = gr.Button('Transcribe')
|
53 |
output_text_transcribe = gr.Textbox(placeholder='Transcript of the YouTube video.', label='Transcript')
|
54 |
-
|
55 |
-
#result_button_summary = gr.Button('2. Create Summary')
|
56 |
-
#output_text_summary = gr.Textbox(placeholder='Summary of the YouTube video transcript.', label='Summary')
|
57 |
-
|
58 |
-
result_button_transcribe.click(get_text, inputs = input_text_url, outputs = output_text_transcribe)
|
59 |
-
#result_button_summary.click(get_summary, inputs = output_text_transcribe, outputs = output_text_summary)
|
60 |
|
61 |
-
|
|
|
|
|
|
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 |
+
'noplaylist': True,
|
14 |
+
'quiet': True,
|
15 |
+
'outtmpl': '%(title)s.%(ext)s' # Specify output template to get the file path
|
16 |
+
}
|
17 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
18 |
+
info = ydl.extract_info(url, download=True)
|
19 |
+
# Use 'requested_downloads' to get the downloaded file path
|
20 |
+
audio_file = ydl.prepare_filename(info)
|
21 |
+
return audio_file
|
22 |
+
except Exception as e:
|
23 |
+
raise gr.Error(f"Exception: {e}")
|
24 |
|
25 |
+
def get_text(url):
|
26 |
+
try:
|
27 |
+
if url != '':
|
28 |
+
audio_file = get_audio(url)
|
29 |
+
result = model.transcribe(audio_file)
|
30 |
+
return result['text'].strip()
|
31 |
+
else:
|
32 |
+
return "Please enter a YouTube video URL."
|
33 |
+
except Exception as e:
|
34 |
+
raise gr.Error(f"Exception: {e}")
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
def get_summary(article):
|
37 |
+
try:
|
38 |
+
first_sentences = ' '.join(re.split(r'(?<=[.:;])\s', article)[:5])
|
39 |
+
return first_sentences
|
40 |
+
except Exception as e:
|
41 |
+
raise gr.Error(f"Exception: {e}")
|
42 |
|
43 |
with gr.Blocks() as demo:
|
44 |
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>")
|
|
|
45 |
gr.Markdown("<center>Enter the link of any YouTube video to generate a text transcript of the video.</center>")
|
46 |
gr.Markdown("<center><b>'Whisper is a neural net that approaches human level robustness and accuracy on English speech recognition.'</b></center>")
|
47 |
+
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>")
|
48 |
|
49 |
+
input_text_url = gr.Textbox(placeholder='Youtube video URL', label='URL')
|
50 |
+
result_button_transcribe = gr.Button('1. Transcribe')
|
51 |
output_text_transcribe = gr.Textbox(placeholder='Transcript of the YouTube video.', label='Transcript')
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
result_button_transcribe.click(get_text, inputs=input_text_url, outputs=output_text_transcribe)
|
54 |
+
|
55 |
+
demo.queue(default_enabled=True).launch(debug=True)
|