Spaces:
Build error
Build error
Commit ·
d790c0b
1
Parent(s): 66efbc3
Update app.py
Browse files2 unmodified lines3456789101112135 unmodified lines192021222324252627282913 unmodified lines4344454647484950516 unmodified lines585960616263646566676869707172737475767778798081822 unmodified linesimport gradio as grimport pytube as ptfrom transformers import pipelineMODEL_NAME = "openai/whisper-large-v2"BATCH_SIZE = 8FILE_LIMIT_MB = 1000YT_ATTEMPT_LIMIT = 3device = 0 if torch.cuda.is_available() else "cpu"5 unmodified lines)all_special_ids = pipe.tokenizer.all_special_idstranscribe_token_id = all_special_ids[-5]translate_token_id = all_special_ids[-6]def transcribe(microphone, file_upload, task):warn_output = ""if (microphone is not None) and (file_upload is not None):13 unmodified linesfile = microphone if microphone is not None else file_uploadpipe.model.config.forced_decoder_ids = [[2, transcribe_token_id if task=="transcribe" else translate_token_id]]text = pipe(file, batch_size=BATCH_SIZE)["text"]return warn_output + text6 unmodified lines)return HTML_strdef yt_transcribe(yt_url, task, max_filesize=75.0):yt = pt.YouTube(yt_url)html_embed_str = _return_yt_html_embed(yt_url)for attempt in range(YT_ATTEMPT_LIMIT):try:yt = pytube.YouTube(yt_url)stream = yt.streams.filter(only_audio=True)[0]breakexcept KeyError:if attempt + 1 == YT_ATTEMPT_LIMIT:raise gr.Error("An error occurred while loading the YouTube video. Please try again.")if stream.filesize_mb > max_filesize:raise gr.Error(f"Maximum YouTube file size is {max_filesize}MB, got {stream.filesize_mb:.2f}MB.")pipe.model.config.forced_decoder_ids = [[2, transcribe_token_id if task=="transcribe" else translate_token_id]]text = pipe("audio.mp3", batch_size=BATCH_SIZE)["text"]return html_embed_str, text2 unmodified lines3456789101112131415165 unmodified lines22232425262713 unmodified lines414243444546476 unmodified lines545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798992 unmodified linesimport gradio as grimport pytube as ptfrom transformers import pipelinefrom transformers.pipelines.audio_utils import ffmpeg_readimport tempfileMODEL_NAME = "openai/whisper-large-v2"BATCH_SIZE = 8FILE_LIMIT_MB = 1000YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube filesdevice = 0 if torch.cuda.is_available() else "cpu"5 unmodified lines)def transcribe(microphone, file_upload, task):warn_output = ""if (microphone is not None) and (file_upload is not None):13 unmodified linesfile = microphone if microphone is not None else file_uploadtext = pipe(file, batch_size=BATCH_SIZE, generate_kwargs={"task": task})["text"]return warn_output + text6 unmodified lines)return HTML_strdef download_yt_audio(yt_url, filename):info_loader = youtube_dl.YoutubeDL()try:info = info_loader.extract_info(yt_url, download=False)except youtube_dl.utils.DownloadError as err:raise gr.Error(str(err))file_length = info["duration_string"]file_h_m_s = file_length.split(":")file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]if len(file_h_m_s) == 1:file_h_m_s.insert(0, 0)if len(file_h_m_s) == 2:file_h_m_s.insert(0, 0)file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]if file_length_s > YT_LENGTH_LIMIT_S:yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}with youtube_dl.YoutubeDL(ydl_opts) as ydl:try:ydl.download([yt_url])except youtube_dl.utils.ExtractorError as err:raise gr.Error(str(err))def yt_transcribe(yt_url, task, max_filesize=75.0):yt = pt.YouTube(yt_url)html_embed_str = _return_yt_html_embed(yt_url)with tempfile.TemporaryDirectory() as tmpdirname:filepath = os.path.join(tmpdirname, "video.mp4")download_yt_audio(yt_url, filepath)with open(filepath, "rb") as f:inputs = f.read()inputs = ffmpeg_read(inputs, pipeline.feature_extractor.sampling_rate)inputs = {"array": inputs, "sampling_rate": pipeline.feature_extractor.sampling_rate}text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task})["text"]return html_embed_str, text
app.py+38-21