guoyu/Test/python/download_model.py
2025-12-11 23:28:07 +08:00

93 lines
2.9 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.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Whisper 模型离线下载工具
用于在有网络的机器上下载模型,然后复制到内网服务器
使用方法:
1. 在有网络的机器上运行此脚本
2. 将下载的模型文件复制到服务器
3. 放到指定目录:~/.cache/whisper/ (Linux) 或 C:\Users\用户名\.cache\whisper\ (Windows)
"""
import whisper
import os
import shutil
def download_model(model_name="base"):
"""
下载 Whisper 模型
可选模型:
- tiny: 39M, 最快,准确度一般
- base: 74M, 快速,准确度好 ✅ 推荐
- small: 244M, 较慢,准确度高
- medium: 769M, 很慢,准确度很高
- large: 1550M, 非常慢,准确度最高
"""
print("=" * 60)
print(f" 下载 Whisper {model_name} 模型")
print("=" * 60)
print("")
print(f"[1/2] 开始下载模型:{model_name}...")
print("⏳ 请耐心等待,根据网络速度可能需要几分钟...")
try:
model = whisper.load_model(model_name)
print(f"✅ 模型下载成功!")
print("")
# 获取模型路径
import whisper
cache_dir = os.path.join(os.path.expanduser("~"), ".cache", "whisper")
model_file = os.path.join(cache_dir, f"{model_name}.pt")
print("[2/2] 模型文件信息:")
print(f" 路径: {model_file}")
if os.path.exists(model_file):
size = os.path.getsize(model_file) / (1024 * 1024)
print(f" 大小: {size:.1f} MB")
print("")
print("=" * 60)
print("📝 后续步骤(内网部署):")
print("=" * 60)
print(f"1. 复制模型文件到服务器:")
print(f" scp {model_file} user@server:~/.cache/whisper/")
print("")
print(f"2. Windows 服务器路径:")
print(f" C:\\Users\\用户名\\.cache\\whisper\\{model_name}.pt")
print("")
print(f"3. Linux 服务器路径:")
print(f" ~/.cache/whisper/{model_name}.pt")
print("")
print("✅ 完成!")
except Exception as e:
print(f"❌ 下载失败:{e}")
print("")
print("💡 解决方法:")
print("1. 检查网络连接")
print("2. 尝试使用代理")
print("3. 手动下载https://openaipublic.azureedge.net/main/whisper/models/")
if __name__ == '__main__':
import sys
# 支持命令行参数指定模型
model_name = sys.argv[1] if len(sys.argv) > 1 else "base"
print("")
print("📦 Whisper 模型下载工具")
print("")
print("可用模型:")
print(" tiny - 39M (最快,准确度一般)")
print(" base - 74M (推荐,快速且准确) ✅")
print(" small - 244M (准确度高)")
print(" medium - 769M (准确度很高)")
print(" large - 1550M (准确度最高)")
print("")
download_model(model_name)