jerrybwang commited on
Commit ·
c2fa8b4
1
Parent(s): c348eb5
111
Browse files
app.py
CHANGED
|
@@ -33,57 +33,92 @@ def load_cosyvoice_model():
|
|
| 33 |
print("="*60)
|
| 34 |
|
| 35 |
try:
|
| 36 |
-
# 使用
|
| 37 |
-
print("\n尝试
|
| 38 |
-
from
|
| 39 |
import torch
|
| 40 |
|
| 41 |
model_name = "FunAudioLLM/CosyVoice-300M"
|
| 42 |
-
print(f"从 {model_name}
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
model_name,
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
low_cpu_mem_usage=True
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
model.eval()
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
print(f"推理方法检查:")
|
| 64 |
-
print(f" - inference_sft: {has_inference_sft}")
|
| 65 |
-
print(f" - inference: {has_inference}")
|
| 66 |
-
print(f" - generate: {has_generate}")
|
| 67 |
-
|
| 68 |
-
# 如果模型有这些方法,说明加载成功
|
| 69 |
-
if has_inference_sft or has_inference or has_generate:
|
| 70 |
cosyvoice_model = {
|
| 71 |
'model': model,
|
| 72 |
-
'type': '
|
| 73 |
'has_inference': True
|
| 74 |
}
|
| 75 |
model_loaded = True
|
| 76 |
-
print("✓ 成功加载CosyVoice模型")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
print("="*60 + "\n")
|
| 78 |
return cosyvoice_model
|
| 79 |
else:
|
| 80 |
-
print("⚠ 模型
|
|
|
|
| 81 |
cosyvoice_model = {
|
| 82 |
-
'model':
|
| 83 |
-
'type': '
|
|
|
|
| 84 |
'has_inference': False
|
| 85 |
}
|
| 86 |
model_loaded = True
|
|
|
|
| 87 |
print("="*60 + "\n")
|
| 88 |
return cosyvoice_model
|
| 89 |
|
|
@@ -95,9 +130,10 @@ def load_cosyvoice_model():
|
|
| 95 |
# 演示模式(加载失败)
|
| 96 |
print("\n⚠ 使用演示模式")
|
| 97 |
print("提示: 要使用完整功能,请确保:")
|
| 98 |
-
print(" 1. 网络连接正常,可以访问Hugging Face")
|
| 99 |
print(" 2. 有足够的磁盘空间(约2GB)")
|
| 100 |
-
print(" 3.
|
|
|
|
| 101 |
print("="*60 + "\n")
|
| 102 |
|
| 103 |
cosyvoice_model = None
|
|
|
|
| 33 |
print("="*60)
|
| 34 |
|
| 35 |
try:
|
| 36 |
+
# 方法1: 尝试使用huggingface_hub下载模型文件
|
| 37 |
+
print("\n尝试从Hugging Face Hub下载模型...")
|
| 38 |
+
from huggingface_hub import snapshot_download
|
| 39 |
import torch
|
| 40 |
|
| 41 |
model_name = "FunAudioLLM/CosyVoice-300M"
|
| 42 |
+
print(f"从 {model_name} 下载模型文件...")
|
| 43 |
|
| 44 |
+
# 下载模型到本地缓存
|
| 45 |
+
model_dir = snapshot_download(
|
| 46 |
+
repo_id=model_name,
|
| 47 |
+
allow_patterns=["*.pt", "*.pth", "*.bin", "*.json", "*.yaml", "*.txt"],
|
| 48 |
+
ignore_patterns=["*.md", "*.gitattributes"]
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
+
print(f"✓ 模型文件已下载到: {model_dir}")
|
|
|
|
| 52 |
|
| 53 |
+
# 方法1.1: 尝试导入CosyVoice官方包
|
| 54 |
+
try:
|
| 55 |
+
print("\n尝试使用CosyVoice官方包加载...")
|
| 56 |
+
from cosyvoice.cli.cosyvoice import CosyVoice
|
| 57 |
+
model = CosyVoice(model_dir)
|
| 58 |
+
print("✓ 使用CosyVoice官方包加载成功")
|
| 59 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
cosyvoice_model = {
|
| 61 |
'model': model,
|
| 62 |
+
'type': 'official',
|
| 63 |
'has_inference': True
|
| 64 |
}
|
| 65 |
model_loaded = True
|
| 66 |
+
print("✓ 成功加载CosyVoice模型 (官方包)")
|
| 67 |
+
print("="*60 + "\n")
|
| 68 |
+
return cosyvoice_model
|
| 69 |
+
|
| 70 |
+
except ImportError:
|
| 71 |
+
print("⚠ CosyVoice官方包未安装")
|
| 72 |
+
print("提示: pip install cosyvoice")
|
| 73 |
+
|
| 74 |
+
# 方法1.2: 尝试直接加载PyTorch模型
|
| 75 |
+
print("\n尝试直接加载PyTorch模型文件...")
|
| 76 |
+
import glob
|
| 77 |
+
|
| 78 |
+
# 查找模型文件
|
| 79 |
+
model_files = glob.glob(os.path.join(model_dir, "**/*.pt"), recursive=True)
|
| 80 |
+
model_files += glob.glob(os.path.join(model_dir, "**/*.pth"), recursive=True)
|
| 81 |
+
model_files += glob.glob(os.path.join(model_dir, "**/*.bin"), recursive=True)
|
| 82 |
+
|
| 83 |
+
if model_files:
|
| 84 |
+
print(f"找到模型文件: {model_files[0]}")
|
| 85 |
+
model = torch.load(model_files[0], map_location='cpu')
|
| 86 |
+
|
| 87 |
+
# 检查模型是否可用
|
| 88 |
+
if isinstance(model, dict):
|
| 89 |
+
print("✓ PyTorch模型字典加载成功")
|
| 90 |
+
cosyvoice_model = {
|
| 91 |
+
'model': model,
|
| 92 |
+
'type': 'pytorch_dict',
|
| 93 |
+
'model_dir': model_dir,
|
| 94 |
+
'has_inference': False
|
| 95 |
+
}
|
| 96 |
+
else:
|
| 97 |
+
if hasattr(model, 'eval'):
|
| 98 |
+
model.eval()
|
| 99 |
+
print(f"✓ PyTorch模型加载成功: {type(model).__name__}")
|
| 100 |
+
cosyvoice_model = {
|
| 101 |
+
'model': model,
|
| 102 |
+
'type': 'pytorch',
|
| 103 |
+
'model_dir': model_dir,
|
| 104 |
+
'has_inference': hasattr(model, 'inference_sft') or hasattr(model, 'inference')
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
model_loaded = True
|
| 108 |
+
print("✓ 模型文件已下载并缓存")
|
| 109 |
print("="*60 + "\n")
|
| 110 |
return cosyvoice_model
|
| 111 |
else:
|
| 112 |
+
print("⚠ 未找到模型权重文件")
|
| 113 |
+
# 模型文件已下载,但没有找到权重文件
|
| 114 |
cosyvoice_model = {
|
| 115 |
+
'model': None,
|
| 116 |
+
'type': 'downloaded',
|
| 117 |
+
'model_dir': model_dir,
|
| 118 |
'has_inference': False
|
| 119 |
}
|
| 120 |
model_loaded = True
|
| 121 |
+
print("✓ 模型文件已下载(但未找到权重文件)")
|
| 122 |
print("="*60 + "\n")
|
| 123 |
return cosyvoice_model
|
| 124 |
|
|
|
|
| 130 |
# 演示模式(加载失败)
|
| 131 |
print("\n⚠ 使用演示模式")
|
| 132 |
print("提示: 要使用完整功能,请确保:")
|
| 133 |
+
print(" 1. 网络连接正常,可以访问Hugging Face Hub")
|
| 134 |
print(" 2. 有足够的磁盘空间(约2GB)")
|
| 135 |
+
print(" 3. 已安装 huggingface_hub: pip install huggingface_hub")
|
| 136 |
+
print(" 4. (可选) 安装CosyVoice官方包: pip install cosyvoice")
|
| 137 |
print("="*60 + "\n")
|
| 138 |
|
| 139 |
cosyvoice_model = None
|