import torch import gradio as gr from pytube import YouTube from transformers import pipeline MODEL_NAME = "yuweiiizz/whisper-small-taiwanese" lang = "chinese" # 根據是否有可用的 CUDA 設備來選擇設備 device = 0 if torch.cuda.is_available() else "cpu" # 初始化 pipeline,指定任務、模型和設備 pipe = pipeline( task="automatic-speech-recognition", chunk_length_s=15, model=MODEL_NAME, device=device, ) # 設置模型的語言和任務 pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language=lang, task="transcribe") # 定義轉錄功能 def transcribe(microphone=None, file_upload=None): warn_output = "" if microphone is not None and file_upload is not None: warn_output = "警告:您同時使用了麥克風與上傳音訊檔案,將只會使用麥克風錄製的檔案。\n" elif microphone is None and file_upload is None: return "錯誤:您必須至少使用麥克風或上傳一個音頻檔案。" file = microphone if microphone is not None else file_upload text = pipe(file)["text"] return warn_output + text # 定義 YouTube 轉寫功能 def yt_transcribe(yt_url): yt = YouTube(yt_url) stream = yt.streams.filter(only_audio=True).first() stream.download(filename="audio.mp3") text = pipe("audio.mp3")["text"] # 嵌入 YouTube 影片 video_id = yt_url.split("?v=")[-1] html_embed = f'