File size: 1,667 Bytes
3342236 a553364 3342236 bbaf2e8 a553364 d35aa5f 3342236 a553364 86e570a a553364 86e570a bbaf2e8 a553364 86e570a a553364 86e570a a553364 86e570a d35aa5f a553364 86e570a bbaf2e8 a553364 d35aa5f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import os
import requests
import zipfile
MODEL_URL = "https://alphacephei.com/vosk/models/vosk-model-vn-0.4.zip"
MODEL_DIR = "model"
MODEL_PATH = os.path.join(MODEL_DIR, "vosk-model-vn-0.4") # Đổi lại cho đúng với thư mục sau khi giải nén
if not os.path.exists(MODEL_PATH):
os.makedirs(MODEL_DIR, exist_ok=True)
zip_path = os.path.join(MODEL_DIR, "model.zip")
print("📥 Đang tải model Vosk tiếng Việt...")
response = requests.get(MODEL_URL, stream=True)
if response.status_code != 200:
print(f"❌ Lỗi tải model! Mã lỗi: {response.status_code}")
exit(1)
with open(zip_path, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
print("📦 Kiểm tra file ZIP...")
if not zipfile.is_zipfile(zip_path):
print("❌ File tải về không phải là file ZIP hợp lệ!")
os.remove(zip_path)
exit(1)
print("📦 Giải nén model...")
with zipfile.ZipFile(zip_path, "r") as zip_ref:
zip_ref.extractall(MODEL_DIR)
# Kiểm tra xem thư mục giải nén có tồn tại không
extracted_path = os.path.join(MODEL_DIR, "vosk-model-vn-0.4")
if os.path.exists(extracted_path):
print(f"✅ Model đã được giải nén tại: {extracted_path}")
else:
print(f"❌ Lỗi: Model không tồn tại sau khi giải nén! Kiểm tra lại.")
exit(1)
os.remove(zip_path) # Xóa file zip sau khi giải nén
print("✅ Model tiếng Việt đã tải thành công!")
else:
print(f"✅ Model đã có sẵn tại {MODEL_PATH}, bỏ qua tải về.")
|