Spaces:
Sleeping
Sleeping
Download app.py from cormort/Ivod_to_text: direct link, hf CLI and curl.
- Browser
- Download file 3.98 kB
-
https://huggingface.co/spaces/cormort/Ivod_to_text/resolve/main/app.py
- Command line
-
hf download hf://spaces/cormort/Ivod_to_text/app.py
-
curl -L -o app.py https://huggingface.co/spaces/cormort/Ivod_to_text/resolve/main/app.py
3.98 kB
| import gradio as gr | |
| import os | |
| import subprocess | |
| import time | |
| from faster_whisper import WhisperModel | |
| # --- 1. 初始化 faster-whisper 模型 --- | |
| # 為了避免每次呼叫都重新載入模型,我們將其放在全域變數 | |
| try: | |
| model_size = "medium" | |
| # 在 Hugging Face 免費層 (CPU) 上,使用 int8 量化是速度與精度的最佳平衡 | |
| model = WhisperModel(model_size, device="cpu", compute_type="int8") | |
| print(f"Faster Whisper model {model_size} loaded (INT8).") | |
| except Exception as e: | |
| print(f"Error loading Faster Whisper model: {e}") | |
| model = None | |
| # --- 2. 合併的處理函式 (關鍵邏輯) --- | |
| def transcribe_from_m3u8(m3u8_url): | |
| if model is None: | |
| return "錯誤:Whisper 模型載入失敗 (可能記憶體不足或下載失敗)。" | |
| if not m3u8_url: | |
| return "請輸入 M3U8 網址。" | |
| timestamp = int(time.time()) | |
| # 策略變更:我們直接將 AAC 音訊流複製出來,存成 .aac 檔案 | |
| temp_audio_file = f"temp_audio_{timestamp}.aac" | |
| try: | |
| # --- 步驟 A: 執行 FFmpeg 下載 (新策略) --- | |
| print(f"開始下載音訊: {m3u8_url}") | |
| # -i: 輸入 (M3U8 URL) | |
| # -vn: (Video No) 忽略影片 | |
| # -c:a copy: (Audio Codec: Copy) 直接複製音訊流,不重新編碼 (速度最快) | |
| # -y: 若檔案存在則覆蓋 | |
| command = [ | |
| 'ffmpeg', | |
| '-y', | |
| '-i', m3u8_url, | |
| '-vn', | |
| '-c:a', 'copy', | |
| temp_audio_file | |
| ] | |
| # 執行下載指令 | |
| subprocess.run(command, check=True, capture_output=True, text=True) | |
| if not os.path.exists(temp_audio_file): | |
| return "下載失敗:未生成音訊檔案。" | |
| print(f"音訊下載完成: {temp_audio_file}") | |
| # --- 步驟 B: 執行 Whisper 轉錄 --- | |
| print(f"開始轉錄: {temp_audio_file}") | |
| # Whisper (faster-whisper) 可以直接處理 .aac 檔案 | |
| # beam_size=5 提升準確度 | |
| segments, info = model.transcribe( | |
| temp_audio_file, | |
| beam_size=5, | |
| word_timestamps=False | |
| ) | |
| # 組合所有片段的文字 | |
| full_transcript = "".join(segment.text for segment in segments) | |
| print("轉錄完成。") | |
| return full_transcript | |
| except subprocess.CalledProcessError as e: | |
| error_message = f"下載/轉碼失敗。FFmpeg 錯誤: {e.stderr}" | |
| print(error_message) | |
| return error_message | |
| except FileNotFoundError: | |
| return "錯誤: 找不到 FFmpeg 指令。請確認 packages.txt 是否已建立。" | |
| except Exception as e: | |
| error_message = f"轉錄過程中發生錯誤: {str(e)}" | |
| print(error_message) | |
| return error_message | |
| finally: | |
| # --- 步驟 C: 清理暫存檔案 --- | |
| if os.path.exists(temp_audio_file): | |
| try: | |
| os.remove(temp_audio_file) | |
| print(f"已刪除暫存檔案: {temp_audio_file}") | |
| except: | |
| pass | |
| # --- 3. 建立新的 Gradio 介面 --- | |
| # 注意:這裡一定要設定 api_name="predict", | |
| # 這樣我們的 main_automation.py 才能正確呼叫它! | |
| iface = gr.Interface( | |
| fn=transcribe_from_m3u8, | |
| inputs=gr.Textbox( | |
| label="請輸入 M3U8 串流網址", | |
| placeholder="例如: https://ivod-lyvod.../playlist.m3u8" | |
| ), | |
| outputs=[ | |
| gr.Textbox(label="完整逐字稿 (Whisper Medium 模型)", lines=15) | |
| ], | |
| title="M3U8 串流轉逐字稿工具", | |
| description=( | |
| "輸入 M3U8 串流網址,後台將自動下載音訊、執行 Whisper 轉錄,並回傳結果。\n" | |
| "後台使用 FFmpeg (僅複製音訊流) 和 Faster-Whisper (Medium INT8)。" | |
| ), | |
| ) | |
| # 啟動時開啟 API 功能 | |
| if __name__ == "__main__": | |
| iface.launch() |