PPT/服务器启动/whisper_patch.py

41 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sys
import logging
# 配置日志
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("whisper_patch")
try:
import whisper as w
logger.info("成功导入原始whisper库")
original_whisper_available = True
except ImportError:
logger.warning("无法导入原始whisper库将使用模拟实现")
original_whisper_available = False
# 创建一个空的模块作为替代
class DummyWhisperModule:
pass
w = DummyWhisperModule()
class Model:
@staticmethod
def load_model(name, device="cpu", download_root=None):
logger.info(f"使用whisper兼容层加载模型: {name}, 设备: {device}")
return DummyModel()
class DummyModel:
def transcribe(self, audio_path, **kwargs):
logger.info(f"使用模拟转录功能处理音频: {audio_path}")
segments = [
{"text": "这是一个示例PPT讲解视频。", "start": 0.0, "end": 5.0},
{"text": "由于Python版本限制无法使用原始语音识别功能。", "start": 5.0, "end": 10.0},
{"text": "这是一个兼容性替代方案。", "start": 10.0, "end": 15.0},
{"text": "您可以升级到Python 3.8+以使用完整功能。", "start": 15.0, "end": 20.0}
]
return {"segments": segments}
# 将自定义功能添加到whisper模块
sys.modules["whisper"] = w
sys.modules["whisper"].load_model = Model.load_model
logger.info("whisper兼容层初始化完成")