Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,19 +17,12 @@ MODEL_CONFIGS = {
|
|
| 17 |
"max_length": 200, # 翻译输出的最大长度
|
| 18 |
"color": "#FF6B6B"
|
| 19 |
},
|
| 20 |
-
"Chinese-to-English (
|
| 21 |
-
"model_name": "
|
| 22 |
-
"description": "中文到英文的机器翻译模型 (
|
| 23 |
"max_length": 200, # 翻译输出的最大长度
|
| 24 |
"color": "#4ECDC4"
|
| 25 |
}
|
| 26 |
-
# 如果需要第三个模型,可以取消注释下面这个,或替换成您想要的
|
| 27 |
-
# "Chinese-to-English (Another Model)": {
|
| 28 |
-
# "model_name": "facebook/mbart-large-50-one-to-many-mmt", # 另一个多语言模型,需要指定 src_lang/tgt_lang
|
| 29 |
-
# "description": "中文到英文的机器翻译模型 (Facebook mBART-Large-50)",
|
| 30 |
-
# "max_length": 200,
|
| 31 |
-
# "color": "#45B7D1"
|
| 32 |
-
# }
|
| 33 |
}
|
| 34 |
|
| 35 |
class TranslationComparator:
|
|
@@ -43,38 +36,27 @@ class TranslationComparator:
|
|
| 43 |
for model_key, config in MODEL_CONFIGS.items():
|
| 44 |
try:
|
| 45 |
print(f"加载 {model_key} ({config['model_name']})...")
|
|
|
|
| 46 |
# 对于翻译任务,使用 "translation" pipeline
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
print(f"✓ {model_key} 加载成功 (自定义翻译模式)")
|
| 67 |
-
continue # 跳过pipeline加载
|
| 68 |
-
else: # 默认翻译任务
|
| 69 |
-
task = "translation"
|
| 70 |
-
|
| 71 |
-
self.models[model_key] = pipeline(
|
| 72 |
-
task,
|
| 73 |
-
model=config["model_name"],
|
| 74 |
-
tokenizer=config["model_name"],
|
| 75 |
-
device=-1, # 使用CPU
|
| 76 |
-
torch_dtype=torch.float32
|
| 77 |
-
)
|
| 78 |
print(f"✓ {model_key} 加载成功")
|
| 79 |
except Exception as e:
|
| 80 |
print(f"✗ {model_key} 加载失败: {e}")
|
|
|
|
| 17 |
"max_length": 200, # 翻译输出的最大长度
|
| 18 |
"color": "#FF6B6B"
|
| 19 |
},
|
| 20 |
+
"Chinese-to-English (mBART-Large-50)": { # 替换为mBART模型
|
| 21 |
+
"model_name": "facebook/mbart-large-50-many-to-one-mmt",
|
| 22 |
+
"description": "中文到英文的机器翻译模型 (Facebook mBART-Large-50)",
|
| 23 |
"max_length": 200, # 翻译输出的最大长度
|
| 24 |
"color": "#4ECDC4"
|
| 25 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
class TranslationComparator:
|
|
|
|
| 36 |
for model_key, config in MODEL_CONFIGS.items():
|
| 37 |
try:
|
| 38 |
print(f"加载 {model_key} ({config['model_name']})...")
|
| 39 |
+
|
| 40 |
# 对于翻译任务,使用 "translation" pipeline
|
| 41 |
+
# 注意:mBART模型需要指定 source_lang 和 target_lang
|
| 42 |
+
if "mbart-large-50" in config["model_name"]:
|
| 43 |
+
self.models[model_key] = pipeline(
|
| 44 |
+
"translation",
|
| 45 |
+
model=config["model_name"],
|
| 46 |
+
tokenizer=config["model_name"],
|
| 47 |
+
src_lang="zh_CN", # 源语言为中文
|
| 48 |
+
tgt_lang="en_US", # 目标语言为英文
|
| 49 |
+
device=-1, # 使用CPU
|
| 50 |
+
torch_dtype=torch.float32
|
| 51 |
+
)
|
| 52 |
+
else: # 对于Helsinki-NLP/opus-mt-zh-en等
|
| 53 |
+
self.models[model_key] = pipeline(
|
| 54 |
+
"translation", # 也可以用 "translation_zh_to_en" 如果 pipeline 支持
|
| 55 |
+
model=config["model_name"],
|
| 56 |
+
tokenizer=config["model_name"],
|
| 57 |
+
device=-1, # 使用CPU
|
| 58 |
+
torch_dtype=torch.float32
|
| 59 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
print(f"✓ {model_key} 加载成功")
|
| 61 |
except Exception as e:
|
| 62 |
print(f"✗ {model_key} 加载失败: {e}")
|