imbecome commited on
Commit
1271a1e
·
verified ·
1 Parent(s): ad1e9e4

Update download_models.py

Browse files
Files changed (1) hide show
  1. download_models.py +31 -34
download_models.py CHANGED
@@ -1,40 +1,37 @@
1
  import os
2
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
- from faster_whisper import WhisperModel
4
 
5
- MODEL_CACHE_DIR = "./models_cache"
6
- DISTILBERT_PATH = os.path.join(MODEL_CACHE_DIR, "distilbert")
7
-
8
- # Pastikan folder cache ada
9
  os.makedirs(MODEL_CACHE_DIR, exist_ok=True)
10
 
11
- print("=== MODEL INITIALIZER ===")
12
-
13
- # ================= DISTILBERT =================
14
- # Catatan: Hapus bagian ini jika app.py tidak menggunakan fitur klasifikasi teks
15
- DISTILBERT_HF = "distilbert-base-uncased-finetuned-sst-2-english"
16
-
17
- if not os.path.exists(DISTILBERT_PATH):
18
- print("Downloading DistilBERT model...")
19
- tokenizer = AutoTokenizer.from_pretrained(DISTILBERT_HF)
20
- model = AutoModelForSequenceClassification.from_pretrained(DISTILBERT_HF)
21
-
22
- tokenizer.save_pretrained(DISTILBERT_PATH)
23
- model.save_pretrained(DISTILBERT_PATH)
24
- print("DistilBERT downloaded successfully.")
25
- else:
26
- print("DistilBERT already exists.")
27
-
28
- # ================= WHISPER =================
29
- print("Downloading Whisper model (tiny)...")
30
-
31
- # Kita paksa download ke folder cache agar tidak hilang saat restart
32
- whisper_model = WhisperModel(
33
- "tiny",
34
- device="cpu",
35
- compute_type="int8",
36
- download_root=MODEL_CACHE_DIR # PENTING: simpan di folder yang sama
37
- )
 
38
 
39
- print("Whisper model downloaded & verified.")
40
  print("=== ALL MODELS READY ===")
 
1
  import os
2
+ import sys
 
3
 
4
+ # Gunakan path absolut agar tidak salah lokasi di Docker
5
+ MODEL_CACHE_DIR = "/home/user/app/models_cache"
 
 
6
  os.makedirs(MODEL_CACHE_DIR, exist_ok=True)
7
 
8
+ print("=== STARTING MODEL DOWNLOAD ===")
9
+
10
+ try:
11
+ from faster_whisper import WhisperModel
12
+ print("Downloading Whisper model (tiny)...")
13
+ # Simpan langsung ke folder cache
14
+ WhisperModel("tiny", device="cpu", compute_type="int8", download_root=MODEL_CACHE_DIR)
15
+ print("Whisper model downloaded successfully.")
16
+ except Exception as e:
17
+ print(f"ERROR downloading Whisper: {e}")
18
+ sys.exit(1) # Beritahu Docker build ini gagal
19
+
20
+ try:
21
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
22
+ # Jika Anda pakai DistilBERT
23
+ model_name = "distilbert-base-uncased-finetuned-sst-2-english"
24
+ save_path = os.path.join(MODEL_CACHE_DIR, "distilbert")
25
+
26
+ print(f"Downloading {model_name}...")
27
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
28
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
29
+
30
+ tokenizer.save_pretrained(save_path)
31
+ model.save_pretrained(save_path)
32
+ print("Transformers models saved.")
33
+ except Exception as e:
34
+ print(f"ERROR downloading Transformers: {e}")
35
+ sys.exit(1)
36
 
 
37
  print("=== ALL MODELS READY ===")