bổ sung tải model luôn

This commit is contained in:
2026-04-20 20:39:43 +07:00
parent b53bb4c8a8
commit dee7e6e960
+57 -7
View File
@@ -22,18 +22,45 @@ TEMPLATE_DIR = Path(__file__).parent / "templates"
AVAILABLE_MODELS = { AVAILABLE_MODELS = {
"gemma-4-E2B-it": { "gemma-4-E2B-it": {
"file": "gemma-4-E2B-it.litertlm", "file": "gemma-4-E2B-it.litertlm",
"repo": "google/gemma-4-E2B-it", "repo": "litert-community/gemma-4-E2B-it-litert-lm",
"desc": "Gemma 4 Edge 2B — nhỏ hơn, nhanh hơn", "desc": "Gemma 4 Edge 2B — nhỏ hơn, nhanh hơn",
}, },
"gemma-4-E4B-it": { "gemma-4-E4B-it": {
"file": "gemma-4-E4B-it.litertlm", "file": "gemma-4-E4B-it.litertlm",
"repo": "google/gemma-4-E4B-it", "repo": "litert-community/gemma-4-E4B-it-litert-lm",
"desc": "Gemma 4 Edge 4B — thông minh hơn, chậm hơn", "desc": "Gemma 4 Edge 4B — thông minh hơn, chậm hơn",
}, },
} }
# ── CLI: chọn model khi khởi động ──────────────────────────────────────────── # ── CLI: chọn model khi khởi động ────────────────────────────────────────────
def download_model(repo: str, local_dir: Path) -> bool:
"""Tải model từ Hugging Face về local."""
try:
import subprocess
print(f"\n Đang tải model từ {repo}...")
print(f" Vui lòng đợi, quá trình này có thể mất vài phút...\n")
cmd = [
"huggingface-cli", "download", repo,
"--include", "*.litertlm",
"--local-dir", str(local_dir)
]
result = subprocess.run(cmd, check=True, capture_output=False, text=True)
print(f"\n ✓ Tải model thành công!")
return True
except subprocess.CalledProcessError as e:
print(f"\n ✗ Lỗi khi tải model: {e}")
return False
except FileNotFoundError:
print(f"\n ✗ Không tìm thấy huggingface-cli.")
print(f" Cài đặt bằng lệnh: pip install huggingface-hub")
return False
except Exception as e:
print(f"\n ✗ Lỗi không xác định: {e}")
return False
def select_model() -> Path: def select_model() -> Path:
print("\n" + "="*52) print("\n" + "="*52)
print(" LiteRT-LM Server — Chọn model") print(" LiteRT-LM Server — Chọn model")
@@ -58,15 +85,38 @@ def select_model() -> Path:
if not model_path.exists(): if not model_path.exists():
print(f"\n Model chưa có trong thư mục models/") print(f"\n Model chưa có trong thư mục models/")
print(f" Tải về bằng lệnh:\n") print(f" Lệnh tải thủ công:\n")
print(f" huggingface-cli download {info['repo']} \\") print(f" huggingface-cli download {info['repo']} \\")
print(f" --include '*.litertlm' \\") print(f" --include '*.litertlm' \\")
print(f" --local-dir models/\n") print(f" --local-dir models/\n")
retry = input(" Chọn model khác? (y/n): ").strip().lower()
if retry == "y": download_choice = input(" Bạn muốn tải model ngay bây giờ? (y/n): ").strip().lower()
continue if download_choice == "y":
if download_model(info['repo'], MODELS_DIR):
# Kiểm tra lại xem file đã tồn tại chưa
if model_path.exists():
print(f"\n Đã chọn: {key}")
print(f" Path: {model_path}\n")
return model_path
else:
print(f"\n ✗ Không tìm thấy file model sau khi tải.")
retry = input(" Chọn model khác? (y/n): ").strip().lower()
if retry == "y":
continue
else:
sys.exit(0)
else:
retry = input("\n Chọn model khác? (y/n): ").strip().lower()
if retry == "y":
continue
else:
sys.exit(0)
else: else:
sys.exit(0) retry = input(" Chọn model khác? (y/n): ").strip().lower()
if retry == "y":
continue
else:
sys.exit(0)
print(f"\n Đã chọn: {key}") print(f"\n Đã chọn: {key}")
print(f" Path: {model_path}\n") print(f" Path: {model_path}\n")