Spaces:
Sleeping
Sleeping
Update app/configs.py
Browse files- app/configs.py +12 -1
app/configs.py
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import zipfile
|
| 3 |
import shutil
|
| 4 |
import threading
|
|
@@ -23,10 +28,12 @@ request_history: List[Dict[str, Any]] = []
|
|
| 23 |
|
| 24 |
|
| 25 |
def sigmoid_to_class(confidence: float) -> str:
|
|
|
|
| 26 |
return "malicious" if confidence >= CLASSIFIER_THRESHOLD else "benign"
|
| 27 |
|
| 28 |
|
| 29 |
def _download_and_extract(zip_path_in_repo: str, extract_subdir: str):
|
|
|
|
| 30 |
extract_dir = MODEL_CACHE_DIR / extract_subdir
|
| 31 |
if extract_dir.exists():
|
| 32 |
return str(extract_dir)
|
|
@@ -51,6 +58,7 @@ def _download_and_extract(zip_path_in_repo: str, extract_subdir: str):
|
|
| 51 |
|
| 52 |
|
| 53 |
def _load_tf_model(zip_path: str, subdir: str):
|
|
|
|
| 54 |
import tensorflow as tf
|
| 55 |
|
| 56 |
model_dir = _download_and_extract(zip_path, subdir)
|
|
@@ -59,6 +67,7 @@ def _load_tf_model(zip_path: str, subdir: str):
|
|
| 59 |
|
| 60 |
|
| 61 |
def get_classification_model():
|
|
|
|
| 62 |
global _classification_model
|
| 63 |
if _classification_model is None:
|
| 64 |
with _model_lock:
|
|
@@ -69,6 +78,7 @@ def get_classification_model():
|
|
| 69 |
|
| 70 |
|
| 71 |
def get_segmentation_model():
|
|
|
|
| 72 |
global _segmentation_model
|
| 73 |
if _segmentation_model is None:
|
| 74 |
with _model_lock:
|
|
@@ -79,6 +89,7 @@ def get_segmentation_model():
|
|
| 79 |
|
| 80 |
|
| 81 |
def get_loaded_versions() -> List[str]:
|
|
|
|
| 82 |
return ["hf_savedmodel"]
|
| 83 |
|
| 84 |
|
|
@@ -91,4 +102,4 @@ SEGMENTS_DIR = STORAGE_DIR / "segments"
|
|
| 91 |
RESULTS_DIR = STORAGE_DIR / "results"
|
| 92 |
|
| 93 |
for dir_path in [STORAGE_DIR, IMAGES_DIR, SEGMENTS_DIR, RESULTS_DIR]:
|
| 94 |
-
dir_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 1 |
+
# app/configs.py
|
| 2 |
import os
|
| 3 |
+
|
| 4 |
+
# 🔥 تعطيل oneDNN لتجنب أخطاء conv2d_transpose في Segmentation
|
| 5 |
+
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
|
| 6 |
+
|
| 7 |
import zipfile
|
| 8 |
import shutil
|
| 9 |
import threading
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
def sigmoid_to_class(confidence: float) -> str:
|
| 31 |
+
"""Convert sigmoid output to class label"""
|
| 32 |
return "malicious" if confidence >= CLASSIFIER_THRESHOLD else "benign"
|
| 33 |
|
| 34 |
|
| 35 |
def _download_and_extract(zip_path_in_repo: str, extract_subdir: str):
|
| 36 |
+
"""Download and extract model from HuggingFace Hub"""
|
| 37 |
extract_dir = MODEL_CACHE_DIR / extract_subdir
|
| 38 |
if extract_dir.exists():
|
| 39 |
return str(extract_dir)
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
def _load_tf_model(zip_path: str, subdir: str):
|
| 61 |
+
"""Load TensorFlow SavedModel"""
|
| 62 |
import tensorflow as tf
|
| 63 |
|
| 64 |
model_dir = _download_and_extract(zip_path, subdir)
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
def get_classification_model():
|
| 70 |
+
"""Get classification model (lazy loading)"""
|
| 71 |
global _classification_model
|
| 72 |
if _classification_model is None:
|
| 73 |
with _model_lock:
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
def get_segmentation_model():
|
| 81 |
+
"""Get segmentation model (lazy loading)"""
|
| 82 |
global _segmentation_model
|
| 83 |
if _segmentation_model is None:
|
| 84 |
with _model_lock:
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
def get_loaded_versions() -> List[str]:
|
| 92 |
+
"""Get list of loaded model versions"""
|
| 93 |
return ["hf_savedmodel"]
|
| 94 |
|
| 95 |
|
|
|
|
| 102 |
RESULTS_DIR = STORAGE_DIR / "results"
|
| 103 |
|
| 104 |
for dir_path in [STORAGE_DIR, IMAGES_DIR, SEGMENTS_DIR, RESULTS_DIR]:
|
| 105 |
+
dir_path.mkdir(parents=True, exist_ok=True)
|