Plana-Archive commited on
Commit
2d13e84
ยท
verified ยท
1 Parent(s): f6a7985

Upload 3 files

Browse files
Mutsumi-Chan/Hololive-Style-Bert-VITS2/app.py CHANGED
@@ -27,6 +27,9 @@ from common.constants import (
27
  )
28
  from common.log import logger
29
 
 
 
 
30
  # --- BAGIAN DOWNLOAD OTOMATIS & FIX STRUKTUR FOLDER ---
31
  from huggingface_hub import snapshot_download
32
 
@@ -89,7 +92,12 @@ def download_assets():
89
  shutil.rmtree(TEMP_DIR)
90
  print("๐Ÿงน Temp directory cleaned up.")
91
 
 
 
 
92
  download_assets()
 
 
93
 
94
  from common.tts_model import ModelHolder
95
  from infer import InvalidToneError
 
27
  )
28
  from common.log import logger
29
 
30
+ # --- IMPORT CLEANER ---
31
+ import cleaner
32
+
33
  # --- BAGIAN DOWNLOAD OTOMATIS & FIX STRUKTUR FOLDER ---
34
  from huggingface_hub import snapshot_download
35
 
 
92
  shutil.rmtree(TEMP_DIR)
93
  print("๐Ÿงน Temp directory cleaned up.")
94
 
95
+
96
+ # Jalankan pembersihan cache sebelum dan sesudah unduhan
97
+ cleaner.cleanup()
98
  download_assets()
99
+ cleaner.cleanup() # bersihkan lagi setelah download selesai
100
+
101
 
102
  from common.tts_model import ModelHolder
103
  from infer import InvalidToneError
Mutsumi-Chan/Hololive-Style-Bert-VITS2/cleaner.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import tempfile
4
+ import time
5
+ from pathlib import Path
6
+
7
+ def clean_old_files(directory: str, days: int = 1):
8
+ """
9
+ Hapus file di dalam directory yang sudah lebih dari `days` hari.
10
+ """
11
+ if not os.path.exists(directory):
12
+ return
13
+ now = time.time()
14
+ cutoff = now - (days * 86400)
15
+ for root, dirs, files in os.walk(directory):
16
+ for f in files:
17
+ filepath = os.path.join(root, f)
18
+ try:
19
+ if os.path.getmtime(filepath) < cutoff:
20
+ os.remove(filepath)
21
+ print(f"๐Ÿ—‘๏ธ Deleted old file: {filepath}")
22
+ except Exception as e:
23
+ print(f"โš ๏ธ Gagal hapus {filepath}: {e}")
24
+ # Hapus direktori kosong
25
+ for d in dirs:
26
+ dirpath = os.path.join(root, d)
27
+ try:
28
+ if not os.listdir(dirpath):
29
+ os.rmdir(dirpath)
30
+ print(f"๐Ÿ—‘๏ธ Deleted empty dir: {dirpath}")
31
+ except Exception:
32
+ pass
33
+
34
+ def cleanup():
35
+ print("๐Ÿงน Mulai pembersihan cache dan file sementara...")
36
+
37
+ # 1. Bersihkan temp_download jika masih ada
38
+ temp_download = "./temp_download"
39
+ if os.path.exists(temp_download):
40
+ shutil.rmtree(temp_download, ignore_errors=True)
41
+ print("โœ… Dihapus: temp_download/")
42
+
43
+ # 2. Bersihkan folder sementara Gradio (biasanya di /tmp/gradio)
44
+ gradio_temp_dirs = [
45
+ "/tmp/gradio",
46
+ os.path.join(tempfile.gettempdir(), "gradio")
47
+ ]
48
+ for gradio_dir in gradio_temp_dirs:
49
+ clean_old_files(gradio_dir, days=0) # hapus semua file lama (lebih 0 hari = semua)
50
+ # Hapus direktori utama jika kosong
51
+ if os.path.exists(gradio_dir) and not os.listdir(gradio_dir):
52
+ try:
53
+ os.rmdir(gradio_dir)
54
+ print(f"โœ… Direktori gradio kosong dihapus: {gradio_dir}")
55
+ except Exception:
56
+ pass
57
+
58
+ # 3. Bersihkan file cache huggingface yang tidak digunakan (opsional, aman)
59
+ hf_cache = os.path.expanduser("~/.cache/huggingface/hub")
60
+ if os.path.exists(hf_cache):
61
+ # Hanya hapus file .lock yang menggantung (jika ada)
62
+ for lock_file in Path(hf_cache).rglob("*.lock"):
63
+ try:
64
+ lock_file.unlink()
65
+ print(f"๐Ÿ—‘๏ธ Hapus lock file: {lock_file}")
66
+ except Exception:
67
+ pass
68
+
69
+ # 4. Hapus folder __pycache__ di project (agar fresh)
70
+ for root, dirs, files in os.walk("."):
71
+ if "__pycache__" in dirs:
72
+ pycache_path = os.path.join(root, "__pycache__")
73
+ shutil.rmtree(pycache_path, ignore_errors=True)
74
+ print(f"โœ… Hapus __pycache__: {pycache_path}")
75
+
76
+ # 5. Bersihkan CUDA cache jika GPU tersedia
77
+ try:
78
+ import torch
79
+ if torch.cuda.is_available():
80
+ torch.cuda.empty_cache()
81
+ print("โœ… CUDA cache dibersihkan.")
82
+ except ImportError:
83
+ pass
84
+
85
+ # 6. Hapus file temporary dari proses sebelumnya (misal .npy, .pkl acak)
86
+ temp_patterns = ["*.tmp", "*.temp", "*.cache"]
87
+ for pattern in temp_patterns:
88
+ for p in Path(".").glob(f"**/{pattern}"):
89
+ if p.is_file():
90
+ try:
91
+ p.unlink()
92
+ print(f"๐Ÿ—‘๏ธ Hapus file sementara: {p}")
93
+ except Exception:
94
+ pass
95
+
96
+ print("โœจ Pembersihan selesai.\n")
97
+
98
+
99
+ if __name__ == "__main__":
100
+ # Jika dijalankan langsung sebagai skrip
101
+ cleanup()