Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,67 @@
|
|
1 |
import gradio as gr
|
2 |
import whisper
|
3 |
from pytube import YouTube
|
|
|
|
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def get_audio(url):
|
6 |
yt = YouTube(url)
|
7 |
return yt.streams.filter(only_audio=True)[0].download(filename="tmp.mp4")
|
8 |
|
9 |
def get_transcript(url, model_size, lang, format):
|
10 |
-
|
|
|
|
|
|
|
|
|
11 |
model = whisper.load_model(model_size)
|
12 |
|
13 |
if lang == "None":
|
14 |
lang = None
|
15 |
|
16 |
-
result = model.transcribe(
|
17 |
|
18 |
if format == "None":
|
19 |
return result["text"]
|
@@ -36,9 +84,6 @@ def format_timestamp(t):
|
|
36 |
return f"{int(hh):02d}:{int(mm):02d}:{int(ss):02d},{int(mi):03d}"
|
37 |
|
38 |
|
39 |
-
langs = ["None"] + sorted(list(whisper.tokenizer.LANGUAGES.values()))
|
40 |
-
model_size = list(whisper._MODELS.keys())
|
41 |
-
|
42 |
with gr.Blocks() as demo:
|
43 |
|
44 |
with gr.Row():
|
|
|
1 |
import gradio as gr
|
2 |
import whisper
|
3 |
from pytube import YouTube
|
4 |
+
from fastapi import FastAPI, Response, Request
|
5 |
+
import yt_dlp
|
6 |
|
7 |
+
langs = ["None"] + sorted(list(whisper.tokenizer.LANGUAGES.values()))
|
8 |
+
model_size = list(whisper._MODELS.keys())
|
9 |
+
|
10 |
+
def get_subtitles(url, langs=['en']):
|
11 |
+
# Download subtitles if available
|
12 |
+
ydl_opts = {
|
13 |
+
'writesubtitles': True,
|
14 |
+
'outtmpl': '%(id)s.%(ext)s',
|
15 |
+
'subtitleslangs': langs,
|
16 |
+
'skip_download': True,
|
17 |
+
}
|
18 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
19 |
+
info_dict = ydl.extract_info(url, download=False)
|
20 |
+
subtitles = result.get("subtitles")
|
21 |
+
if subtitles and len(subtitles):
|
22 |
+
return subtitles
|
23 |
+
return None
|
24 |
+
|
25 |
+
def download_audio(video_url, quality: str = '128', speed: float = None):
|
26 |
+
ydl_opts = {
|
27 |
+
'format': 'bestaudio/best',
|
28 |
+
'outtmpl': '%(title)s.%(ext)s',
|
29 |
+
'quiet': True,
|
30 |
+
'postprocessors': [{
|
31 |
+
'key': 'FFmpegExtractAudio',
|
32 |
+
'preferredcodec': 'mp3', #'opus',
|
33 |
+
'preferredquality': quality,
|
34 |
+
}]
|
35 |
+
}
|
36 |
+
|
37 |
+
if speed:
|
38 |
+
ydl_opts['postprocessors'].append({
|
39 |
+
'key': 'FFmpegFilter',
|
40 |
+
'filter_complex': f"atempo={speed}"
|
41 |
+
})
|
42 |
+
|
43 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
44 |
+
ydl.download([video_url])
|
45 |
+
audio_file = ydl.prepare_filename(ydl.extract_info(video_url, download=False))
|
46 |
+
return audio_file
|
47 |
+
|
48 |
+
|
49 |
def get_audio(url):
|
50 |
yt = YouTube(url)
|
51 |
return yt.streams.filter(only_audio=True)[0].download(filename="tmp.mp4")
|
52 |
|
53 |
def get_transcript(url, model_size, lang, format):
|
54 |
+
subtitles = get_subtitles(url, langs)
|
55 |
+
if(subtitles){
|
56 |
+
print(subtitles)
|
57 |
+
return subtitles.get(lang)
|
58 |
+
}
|
59 |
model = whisper.load_model(model_size)
|
60 |
|
61 |
if lang == "None":
|
62 |
lang = None
|
63 |
|
64 |
+
result = model.transcribe(download_audio(url), fp16=False, language=lang)
|
65 |
|
66 |
if format == "None":
|
67 |
return result["text"]
|
|
|
84 |
return f"{int(hh):02d}:{int(mm):02d}:{int(ss):02d},{int(mi):03d}"
|
85 |
|
86 |
|
|
|
|
|
|
|
87 |
with gr.Blocks() as demo:
|
88 |
|
89 |
with gr.Row():
|