Spaces:
Sleeping
Sleeping
Create spaces_optimizer.py
Browse files- spaces_optimizer.py +27 -0
spaces_optimizer.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# spaces_optimizer.py - Spaces optimizasyonu
|
| 2 |
+
import gc
|
| 3 |
+
import psutil
|
| 4 |
+
import threading
|
| 5 |
+
from functools import lru_cache
|
| 6 |
+
|
| 7 |
+
class SpacesOptimizer:
|
| 8 |
+
"""Hugging Face Spaces için optimizasyon sınıfı"""
|
| 9 |
+
|
| 10 |
+
def __init__(self):
|
| 11 |
+
self.memory_limit = 16 * 1024 * 1024 * 1024 # 16GB
|
| 12 |
+
self.optimize_memory()
|
| 13 |
+
|
| 14 |
+
def optimize_memory(self):
|
| 15 |
+
"""Bellek optimizasyonu"""
|
| 16 |
+
# Garbage collection
|
| 17 |
+
gc.collect()
|
| 18 |
+
if torch.cuda.is_available():
|
| 19 |
+
torch.cuda.empty_cache()
|
| 20 |
+
|
| 21 |
+
# Thread sayısını optimize et
|
| 22 |
+
torch.set_num_threads(2)
|
| 23 |
+
|
| 24 |
+
@lru_cache(maxsize=1)
|
| 25 |
+
def load_model_cached(self):
|
| 26 |
+
"""Modeli cache'le"""
|
| 27 |
+
return ProfessionalGeoReferencingSystem()
|