Spaces:
Runtime error
Runtime error
LPX55
commited on
Commit
·
f00c873
1
Parent(s):
fccbc0f
feat: implement model registration logic for ONNX, HuggingFace, and Gradio API
Browse files- app.py +4 -0
- utils/model_loader.py +151 -0
app.py
CHANGED
|
@@ -17,6 +17,7 @@ import torch
|
|
| 17 |
|
| 18 |
from utils.utils import softmax, augment_image, preprocess_resize_256, preprocess_resize_224, postprocess_pipeline, postprocess_logits, postprocess_binary_output, to_float_scalar, infer_gradio_api, preprocess_gradio_api, postprocess_gradio_api
|
| 19 |
from utils.onnx_helpers import preprocess_onnx_input, postprocess_onnx_output
|
|
|
|
| 20 |
from utils.onnx_model_loader import load_onnx_model_and_preprocessor, get_onnx_model_from_cache
|
| 21 |
from forensics.gradient import gradient_processing
|
| 22 |
from forensics.minmax import minmax_process
|
|
@@ -79,6 +80,9 @@ CLASS_NAMES = {
|
|
| 79 |
"model_8": ['Fake', 'Real'],
|
| 80 |
}
|
| 81 |
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
|
| 84 |
def register_model_with_metadata(model_id, model, preprocess, postprocess, class_names, display_name, contributor, model_path, architecture=None, dataset=None):
|
|
|
|
| 17 |
|
| 18 |
from utils.utils import softmax, augment_image, preprocess_resize_256, preprocess_resize_224, postprocess_pipeline, postprocess_logits, postprocess_binary_output, to_float_scalar, infer_gradio_api, preprocess_gradio_api, postprocess_gradio_api
|
| 19 |
from utils.onnx_helpers import preprocess_onnx_input, postprocess_onnx_output
|
| 20 |
+
from utils.model_loader import register_all_models
|
| 21 |
from utils.onnx_model_loader import load_onnx_model_and_preprocessor, get_onnx_model_from_cache
|
| 22 |
from forensics.gradient import gradient_processing
|
| 23 |
from forensics.minmax import minmax_process
|
|
|
|
| 80 |
"model_8": ['Fake', 'Real'],
|
| 81 |
}
|
| 82 |
|
| 83 |
+
# Register all models (ONNX, HuggingFace, Gradio API)
|
| 84 |
+
register_all_models(MODEL_PATHS, CLASS_NAMES, device, infer_onnx_model, preprocess_onnx_input, postprocess_onnx_output)
|
| 85 |
+
|
| 86 |
|
| 87 |
|
| 88 |
def register_model_with_metadata(model_id, model, preprocess, postprocess, class_names, display_name, contributor, model_path, architecture=None, dataset=None):
|
utils/model_loader.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Model loading and registration logic for OpenSight Deepfake Detection Playground.
|
| 3 |
+
Handles ONNX, HuggingFace, and Gradio API model registration and metadata.
|
| 4 |
+
"""
|
| 5 |
+
from utils.registry import register_model, MODEL_REGISTRY, ModelEntry
|
| 6 |
+
from utils.onnx_model_loader import load_onnx_model_and_preprocessor, get_onnx_model_from_cache
|
| 7 |
+
from utils.utils import preprocess_resize_256, postprocess_logits, infer_gradio_api, preprocess_gradio_api, postprocess_gradio_api
|
| 8 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
| 9 |
+
import torch
|
| 10 |
+
import numpy as np
|
| 11 |
+
from PIL import Image
|
| 12 |
+
|
| 13 |
+
# Cache for ONNX sessions and preprocessors
|
| 14 |
+
_onnx_model_cache = {}
|
| 15 |
+
|
| 16 |
+
def register_model_with_metadata(model_id, model, preprocess, postprocess, class_names, display_name, contributor, model_path, architecture=None, dataset=None):
|
| 17 |
+
entry = ModelEntry(model, preprocess, postprocess, class_names, display_name=display_name, contributor=contributor, model_path=model_path, architecture=architecture, dataset=dataset)
|
| 18 |
+
MODEL_REGISTRY[model_id] = entry
|
| 19 |
+
|
| 20 |
+
class ONNXModelWrapper:
|
| 21 |
+
def __init__(self, hf_model_id):
|
| 22 |
+
self.hf_model_id = hf_model_id
|
| 23 |
+
self._session = None
|
| 24 |
+
self._preprocessor_config = None
|
| 25 |
+
self._model_config = None
|
| 26 |
+
|
| 27 |
+
def load(self):
|
| 28 |
+
if self._session is None:
|
| 29 |
+
self._session, self._preprocessor_config, self._model_config = get_onnx_model_from_cache(
|
| 30 |
+
self.hf_model_id, _onnx_model_cache, load_onnx_model_and_preprocessor
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
def __call__(self, image_np):
|
| 34 |
+
self.load()
|
| 35 |
+
return infer_onnx_model(self.hf_model_id, image_np, self._model_config)
|
| 36 |
+
|
| 37 |
+
def preprocess(self, image: Image.Image):
|
| 38 |
+
self.load()
|
| 39 |
+
return preprocess_onnx_input(image, self._preprocessor_config)
|
| 40 |
+
|
| 41 |
+
def postprocess(self, onnx_output: dict, class_names_from_registry: list):
|
| 42 |
+
self.load()
|
| 43 |
+
return postprocess_onnx_output(onnx_output, self._model_config)
|
| 44 |
+
|
| 45 |
+
# The main registration function
|
| 46 |
+
|
| 47 |
+
def register_all_models(MODEL_PATHS, CLASS_NAMES, device, infer_onnx_model, preprocess_onnx_input, postprocess_onnx_output):
|
| 48 |
+
for model_key, hf_model_path in MODEL_PATHS.items():
|
| 49 |
+
model_num = model_key.replace("model_", "").upper()
|
| 50 |
+
contributor = "Unknown"
|
| 51 |
+
architecture = "Unknown"
|
| 52 |
+
dataset = "TBA"
|
| 53 |
+
current_class_names = CLASS_NAMES.get(model_key, [])
|
| 54 |
+
if "ONNX" in hf_model_path:
|
| 55 |
+
onnx_wrapper_instance = ONNXModelWrapper(hf_model_path)
|
| 56 |
+
if model_key == "model_1":
|
| 57 |
+
contributor = "haywoodsloan"
|
| 58 |
+
architecture = "SwinV2"
|
| 59 |
+
dataset = "DeepFakeDetection"
|
| 60 |
+
elif model_key == "model_2":
|
| 61 |
+
contributor = "Heem2"
|
| 62 |
+
architecture = "ViT"
|
| 63 |
+
dataset = "DeepFakeDetection"
|
| 64 |
+
elif model_key == "model_3":
|
| 65 |
+
contributor = "Organika"
|
| 66 |
+
architecture = "VIT"
|
| 67 |
+
dataset = "SDXL"
|
| 68 |
+
elif model_key == "model_5":
|
| 69 |
+
contributor = "prithivMLmods"
|
| 70 |
+
architecture = "VIT"
|
| 71 |
+
elif model_key == "model_6":
|
| 72 |
+
contributor = "ideepankarsharma2003"
|
| 73 |
+
architecture = "SWINv1"
|
| 74 |
+
dataset = "SDXL, Midjourney"
|
| 75 |
+
elif model_key == "model_7":
|
| 76 |
+
contributor = "date3k2"
|
| 77 |
+
architecture = "VIT"
|
| 78 |
+
display_name_parts = [model_num]
|
| 79 |
+
if architecture and architecture not in ["Unknown"]:
|
| 80 |
+
display_name_parts.append(architecture)
|
| 81 |
+
if dataset and dataset not in ["TBA"]:
|
| 82 |
+
display_name_parts.append(dataset)
|
| 83 |
+
display_name = "-".join(display_name_parts) + "_ONNX"
|
| 84 |
+
register_model_with_metadata(
|
| 85 |
+
model_id=model_key,
|
| 86 |
+
model=onnx_wrapper_instance,
|
| 87 |
+
preprocess=onnx_wrapper_instance.preprocess,
|
| 88 |
+
postprocess=onnx_wrapper_instance.postprocess,
|
| 89 |
+
class_names=current_class_names,
|
| 90 |
+
display_name=display_name,
|
| 91 |
+
contributor=contributor,
|
| 92 |
+
model_path=hf_model_path,
|
| 93 |
+
architecture=architecture,
|
| 94 |
+
dataset=dataset
|
| 95 |
+
)
|
| 96 |
+
elif model_key == "model_8":
|
| 97 |
+
contributor = "aiwithoutborders-xyz"
|
| 98 |
+
architecture = "ViT"
|
| 99 |
+
dataset = "DeepfakeDetection"
|
| 100 |
+
display_name_parts = [model_num]
|
| 101 |
+
if architecture and architecture not in ["Unknown"]:
|
| 102 |
+
display_name_parts.append(architecture)
|
| 103 |
+
if dataset and dataset not in ["TBA"]:
|
| 104 |
+
display_name_parts.append(dataset)
|
| 105 |
+
display_name = "-".join(display_name_parts)
|
| 106 |
+
register_model_with_metadata(
|
| 107 |
+
model_id=model_key,
|
| 108 |
+
model=infer_gradio_api,
|
| 109 |
+
preprocess=preprocess_gradio_api,
|
| 110 |
+
postprocess=postprocess_gradio_api,
|
| 111 |
+
class_names=current_class_names,
|
| 112 |
+
display_name=display_name,
|
| 113 |
+
contributor=contributor,
|
| 114 |
+
model_path=hf_model_path,
|
| 115 |
+
architecture=architecture,
|
| 116 |
+
dataset=dataset
|
| 117 |
+
)
|
| 118 |
+
elif model_key == "model_4":
|
| 119 |
+
contributor = "cmckinle"
|
| 120 |
+
architecture = "VIT"
|
| 121 |
+
dataset = "SDXL, FLUX"
|
| 122 |
+
display_name_parts = [model_num]
|
| 123 |
+
if architecture and architecture not in ["Unknown"]:
|
| 124 |
+
display_name_parts.append(architecture)
|
| 125 |
+
if dataset and dataset not in ["TBA"]:
|
| 126 |
+
display_name_parts.append(dataset)
|
| 127 |
+
display_name = "-".join(display_name_parts)
|
| 128 |
+
current_processor = AutoFeatureExtractor.from_pretrained(hf_model_path, device=device)
|
| 129 |
+
model_instance = AutoModelForImageClassification.from_pretrained(hf_model_path).to(device)
|
| 130 |
+
preprocess_func = preprocess_resize_256
|
| 131 |
+
postprocess_func = postprocess_logits
|
| 132 |
+
def custom_infer(image, processor_local=current_processor, model_local=model_instance):
|
| 133 |
+
inputs = processor_local(image, return_tensors="pt").to(device)
|
| 134 |
+
with torch.no_grad():
|
| 135 |
+
outputs = model_local(**inputs)
|
| 136 |
+
return outputs
|
| 137 |
+
model_instance = custom_infer
|
| 138 |
+
register_model_with_metadata(
|
| 139 |
+
model_id=model_key,
|
| 140 |
+
model=model_instance,
|
| 141 |
+
preprocess=preprocess_func,
|
| 142 |
+
postprocess=postprocess_func,
|
| 143 |
+
class_names=current_class_names,
|
| 144 |
+
display_name=display_name,
|
| 145 |
+
contributor=contributor,
|
| 146 |
+
model_path=hf_model_path,
|
| 147 |
+
architecture=architecture,
|
| 148 |
+
dataset=dataset
|
| 149 |
+
)
|
| 150 |
+
else:
|
| 151 |
+
pass # Fallback for any unhandled models
|