Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,49 @@
|
|
1 |
import whisper
|
2 |
-
|
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 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
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 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
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 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
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)
|