fakpppyopppppp
Browse files
app.py
CHANGED
|
@@ -5,10 +5,12 @@ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
import tensorflow as tf
|
| 8 |
-
from tensorflow.keras.models import load_model
|
|
|
|
| 9 |
import joblib
|
| 10 |
import gradio as gr
|
| 11 |
import cv2
|
|
|
|
| 12 |
|
| 13 |
from custom_objects import get_custom_objects
|
| 14 |
|
|
@@ -18,10 +20,118 @@ from custom_objects import get_custom_objects
|
|
| 18 |
IMG_SIZE = 224
|
| 19 |
|
| 20 |
# ======================================================
|
| 21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# ======================================================
|
| 23 |
def create_fallback_extractor():
|
| 24 |
-
"""Create
|
|
|
|
|
|
|
| 25 |
base_model = tf.keras.applications.MobileNetV2(
|
| 26 |
input_shape=(IMG_SIZE, IMG_SIZE, 3),
|
| 27 |
include_top=False,
|
|
@@ -30,245 +140,197 @@ def create_fallback_extractor():
|
|
| 30 |
)
|
| 31 |
base_model.trainable = False
|
| 32 |
|
| 33 |
-
|
| 34 |
-
inputs = tf.keras.Input(shape=(IMG_SIZE, IMG_SIZE, 3))
|
| 35 |
-
|
| 36 |
-
# MobileNetV2 preprocessing
|
| 37 |
x = tf.keras.applications.mobilenet_v2.preprocess_input(inputs)
|
| 38 |
-
|
| 39 |
-
# Get features from base model
|
| 40 |
features = base_model(x, training=False)
|
| 41 |
|
| 42 |
-
# Add
|
| 43 |
x = tf.keras.layers.Dense(512, activation="relu")(features)
|
| 44 |
x = tf.keras.layers.Dropout(0.3)(x)
|
| 45 |
x = tf.keras.layers.Dense(256, activation="relu")(x)
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
model = tf.keras.Model(inputs, outputs, name="fallback_extractor")
|
| 51 |
return model
|
| 52 |
|
| 53 |
# ======================================================
|
| 54 |
-
# LOAD MODELS
|
| 55 |
# ======================================================
|
| 56 |
extractor, classifier = None, None
|
| 57 |
|
| 58 |
def load_models():
|
| 59 |
global extractor, classifier
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
print("✓ Feature extractor loaded with custom objects")
|
| 72 |
-
|
| 73 |
-
except Exception as e1:
|
| 74 |
-
print(f"Strategy 1 failed: {e1}")
|
| 75 |
-
|
| 76 |
-
# Strategy 2: Try loading just weights
|
| 77 |
-
print("Attempting to load architecture from JSON...")
|
| 78 |
-
try:
|
| 79 |
-
# Create a simple model architecture first
|
| 80 |
-
from tensorflow.keras.models import model_from_json
|
| 81 |
-
with open("model_architecture.json", "r") as f:
|
| 82 |
-
model_json = f.read()
|
| 83 |
-
extractor = model_from_json(model_json, custom_objects=get_custom_objects())
|
| 84 |
-
extractor.load_weights("model_weights.h5")
|
| 85 |
-
print("✓ Feature extractor loaded from JSON + weights")
|
| 86 |
-
except:
|
| 87 |
-
print("JSON loading failed, using fallback...")
|
| 88 |
-
raise e1 # Re-raise original error to trigger fallback
|
| 89 |
-
|
| 90 |
-
except Exception as e:
|
| 91 |
-
print(f"✗ All loading strategies failed: {e}")
|
| 92 |
-
print("Creating reliable fallback extractor...")
|
| 93 |
extractor = create_fallback_extractor()
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
print("
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
for
|
| 104 |
-
if os.path.exists(
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
break
|
| 109 |
-
else:
|
| 110 |
-
raise FileNotFoundError("No classifier file found")
|
| 111 |
-
|
| 112 |
except Exception as e:
|
| 113 |
-
print(f"✗
|
| 114 |
-
print("Creating simple Random Forest classifier as fallback...")
|
| 115 |
|
|
|
|
| 116 |
from sklearn.ensemble import RandomForestClassifier
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
dummy_features = np.random.randn(100, output_dim)
|
| 128 |
-
dummy_labels = np.random.randint(0, 2, 100)
|
| 129 |
-
classifier.fit(dummy_features, dummy_labels)
|
| 130 |
-
print("✓ Dummy classifier trained on random data")
|
| 131 |
-
|
| 132 |
-
# Save it for future use
|
| 133 |
-
joblib.dump(classifier, "fallback_classifier.pkl")
|
| 134 |
-
print("✓ Fallback classifier saved as 'fallback_classifier.pkl'")
|
| 135 |
-
else:
|
| 136 |
-
raise RuntimeError("Extractor not available for dummy classifier training")
|
| 137 |
|
| 138 |
# ======================================================
|
| 139 |
-
#
|
| 140 |
# ======================================================
|
| 141 |
def preprocess_image(img):
|
| 142 |
-
"""Preprocess image for
|
| 143 |
-
# Convert
|
| 144 |
if isinstance(img, Image.Image):
|
| 145 |
img = np.array(img)
|
| 146 |
|
| 147 |
-
# Handle different
|
| 148 |
if len(img.shape) == 2: # Grayscale
|
| 149 |
img = np.stack([img] * 3, axis=-1)
|
| 150 |
elif img.shape[2] == 4: # RGBA
|
| 151 |
img = img[:, :, :3]
|
| 152 |
-
elif img.shape[2] == 1: # Single channel
|
| 153 |
-
img = np.concatenate([img] * 3, axis=-1)
|
| 154 |
|
| 155 |
-
#
|
| 156 |
if img.shape[2] == 3:
|
| 157 |
-
#
|
| 158 |
-
|
| 159 |
-
# Check if it's likely BGR by testing color channels
|
| 160 |
-
if img[0, 0, 0] > img[0, 0, 2]: # If blue > red, might be BGR
|
| 161 |
-
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 162 |
-
else:
|
| 163 |
-
img = cv2.cvtColor(img, cv2.COLOR_RGB2RGB) # Ensure RGB
|
| 164 |
-
except:
|
| 165 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 166 |
|
| 167 |
-
# Resize
|
| 168 |
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
|
| 169 |
|
| 170 |
-
# Normalize to [0, 1]
|
| 171 |
-
img = img.astype(
|
| 172 |
|
| 173 |
-
|
| 174 |
-
return np.expand_dims(img, axis=0)
|
| 175 |
|
| 176 |
# ======================================================
|
| 177 |
-
#
|
| 178 |
# ======================================================
|
| 179 |
def predict(img):
|
| 180 |
-
"""Make prediction
|
| 181 |
try:
|
| 182 |
-
# Preprocess
|
| 183 |
-
|
|
|
|
| 184 |
|
| 185 |
# Extract features
|
| 186 |
-
features = extractor.predict(
|
| 187 |
|
| 188 |
-
# Flatten
|
| 189 |
if len(features.shape) > 2:
|
| 190 |
features = features.reshape(features.shape[0], -1)
|
| 191 |
-
else:
|
| 192 |
-
features = features.reshape(1, -1)
|
| 193 |
|
| 194 |
-
#
|
| 195 |
pred = classifier.predict(features)[0]
|
| 196 |
|
| 197 |
-
# Get confidence
|
| 198 |
try:
|
| 199 |
proba = classifier.predict_proba(features)[0]
|
| 200 |
confidence = proba[pred] * 100
|
| 201 |
-
except
|
| 202 |
-
|
| 203 |
-
decision = classifier.decision_function(features)[0] if hasattr(classifier, 'decision_function') else 0
|
| 204 |
-
confidence = min(95.0, max(50.0, 50 + abs(decision) * 10))
|
| 205 |
|
| 206 |
-
#
|
| 207 |
label = "Real" if pred == 0 else "Fake"
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
result = {
|
| 211 |
-
"Real": 100 - confidence if label == "Fake" else confidence,
|
| 212 |
"Fake": confidence if label == "Fake" else 100 - confidence
|
| 213 |
}
|
| 214 |
|
| 215 |
-
return result
|
| 216 |
-
|
| 217 |
except Exception as e:
|
| 218 |
print(f"Prediction error: {e}")
|
| 219 |
-
# Return neutral prediction in case of error
|
| 220 |
return {"Real": 50.0, "Fake": 50.0}
|
| 221 |
|
| 222 |
# ======================================================
|
| 223 |
-
#
|
| 224 |
# ======================================================
|
| 225 |
-
def
|
| 226 |
-
"""Create
|
| 227 |
-
# Load models
|
| 228 |
-
print("Loading models...")
|
| 229 |
load_models()
|
| 230 |
-
print("✓ Models loaded successfully!")
|
| 231 |
|
| 232 |
# Create interface
|
| 233 |
iface = gr.Interface(
|
| 234 |
fn=predict,
|
| 235 |
inputs=gr.Image(
|
| 236 |
-
type="pil",
|
| 237 |
label="Upload Image",
|
| 238 |
image_mode="RGB"
|
| 239 |
),
|
| 240 |
outputs=gr.Label(
|
| 241 |
-
num_top_classes=2,
|
| 242 |
-
label="Prediction
|
| 243 |
-
show_label=True
|
| 244 |
),
|
| 245 |
-
title="
|
| 246 |
-
description=""
|
| 247 |
-
|
| 248 |
-
The model analyzes image artifacts and patterns to determine authenticity.
|
| 249 |
-
""",
|
| 250 |
-
examples=[
|
| 251 |
-
["real_0.jpg"] if os.path.exists("real_0.jpg") else None,
|
| 252 |
-
["fake_0.jpg"] if os.path.exists("fake_0.jpg") else None
|
| 253 |
-
],
|
| 254 |
-
theme="soft",
|
| 255 |
-
allow_flagging="never"
|
| 256 |
)
|
| 257 |
|
| 258 |
return iface
|
| 259 |
|
| 260 |
# ======================================================
|
| 261 |
-
# MAIN
|
| 262 |
# ======================================================
|
| 263 |
if __name__ == "__main__":
|
| 264 |
-
|
| 265 |
-
|
|
|
|
|
|
|
| 266 |
|
| 267 |
-
|
| 268 |
-
demo = create_demo()
|
| 269 |
-
demo.launch(
|
| 270 |
server_name="0.0.0.0",
|
| 271 |
server_port=7860,
|
| 272 |
-
share=False
|
| 273 |
-
show_error=True
|
| 274 |
)
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
import tensorflow as tf
|
| 8 |
+
from tensorflow.keras.models import load_model, Model
|
| 9 |
+
from tensorflow.keras.layers import Input
|
| 10 |
import joblib
|
| 11 |
import gradio as gr
|
| 12 |
import cv2
|
| 13 |
+
import h5py
|
| 14 |
|
| 15 |
from custom_objects import get_custom_objects
|
| 16 |
|
|
|
|
| 20 |
IMG_SIZE = 224
|
| 21 |
|
| 22 |
# ======================================================
|
| 23 |
+
# DEBUG HYBRID MODEL
|
| 24 |
+
# ======================================================
|
| 25 |
+
def debug_hybrid_model():
|
| 26 |
+
"""Debug the hybrid_model.keras file"""
|
| 27 |
+
print("\n🔍 Debugging hybrid_model.keras...")
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
# Method 1: Inspect the file directly
|
| 31 |
+
print("Method 1: Inspecting HDF5 structure...")
|
| 32 |
+
with h5py.File('hybrid_model.keras', 'r') as f:
|
| 33 |
+
print("Keys in file:", list(f.keys()))
|
| 34 |
+
if 'model_weights' in f:
|
| 35 |
+
print("Model weights groups:", list(f['model_weights'].keys()))
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"HDF5 inspection failed: {e}")
|
| 38 |
+
|
| 39 |
+
# Method 2: Try to load with different approaches
|
| 40 |
+
print("\nMethod 2: Trying different loading strategies...")
|
| 41 |
+
|
| 42 |
+
# Strategy A: Load without custom objects first
|
| 43 |
+
try:
|
| 44 |
+
model = tf.keras.models.load_model('hybrid_model.keras', compile=False)
|
| 45 |
+
print("✓ Loaded without custom objects")
|
| 46 |
+
return model
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"✗ Strategy A failed: {e}")
|
| 49 |
+
|
| 50 |
+
# Strategy B: Try to rebuild from config
|
| 51 |
+
try:
|
| 52 |
+
print("\nTrying to rebuild from JSON config...")
|
| 53 |
+
# Check if there's a JSON config
|
| 54 |
+
with h5py.File('hybrid_model.keras', 'r') as f:
|
| 55 |
+
if 'model_config' in f:
|
| 56 |
+
config = f['model_config'][()]
|
| 57 |
+
config_str = config.decode('utf-8') if isinstance(config, bytes) else config
|
| 58 |
+
|
| 59 |
+
# Try to load from JSON
|
| 60 |
+
import json
|
| 61 |
+
model_config = json.loads(config_str)
|
| 62 |
+
|
| 63 |
+
# Try to create model from config
|
| 64 |
+
model = tf.keras.models.model_from_json(
|
| 65 |
+
config_str,
|
| 66 |
+
custom_objects=get_custom_objects()
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Try to load weights
|
| 70 |
+
model.load_weights('hybrid_model.keras', by_name=True, skip_mismatch=True)
|
| 71 |
+
print("✓ Rebuilt from config with custom objects")
|
| 72 |
+
return model
|
| 73 |
+
except Exception as e:
|
| 74 |
+
print(f"✗ Strategy B failed: {e}")
|
| 75 |
+
|
| 76 |
+
# Strategy C: Extract just the feature extraction part
|
| 77 |
+
try:
|
| 78 |
+
print("\nTrying to extract feature extractor submodel...")
|
| 79 |
+
# Load the full model first
|
| 80 |
+
full_model = tf.keras.models.load_model(
|
| 81 |
+
'hybrid_model.keras',
|
| 82 |
+
custom_objects=get_custom_objects(),
|
| 83 |
+
compile=False
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Try to find the feature extractor layer
|
| 87 |
+
# Common patterns for feature extractors
|
| 88 |
+
layer_names = [layer.name for layer in full_model.layers]
|
| 89 |
+
print(f"Available layers: {layer_names}")
|
| 90 |
+
|
| 91 |
+
# Look for feature/dense/flatten layers
|
| 92 |
+
feature_layer_names = []
|
| 93 |
+
for name in layer_names:
|
| 94 |
+
if 'feature' in name.lower() or 'dense' in name or 'flatten' in name or 'global' in name:
|
| 95 |
+
feature_layer_names.append(name)
|
| 96 |
+
|
| 97 |
+
if feature_layer_names:
|
| 98 |
+
print(f"Potential feature layers: {feature_layer_names}")
|
| 99 |
+
# Use the last dense layer before classification
|
| 100 |
+
for layer_name in reversed(feature_layer_names):
|
| 101 |
+
try:
|
| 102 |
+
extractor = Model(
|
| 103 |
+
inputs=full_model.input,
|
| 104 |
+
outputs=full_model.get_layer(layer_name).output
|
| 105 |
+
)
|
| 106 |
+
print(f"✓ Created extractor from layer: {layer_name}")
|
| 107 |
+
return extractor
|
| 108 |
+
except:
|
| 109 |
+
continue
|
| 110 |
+
|
| 111 |
+
# If no specific layer found, try to remove classification layers
|
| 112 |
+
# Assuming the model ends with Dense layers for classification
|
| 113 |
+
for i, layer in enumerate(reversed(full_model.layers)):
|
| 114 |
+
if isinstance(layer, tf.keras.layers.Dense) and layer.units <= 2: # Classification layer
|
| 115 |
+
# Get output from layer before classification
|
| 116 |
+
extractor = Model(
|
| 117 |
+
inputs=full_model.input,
|
| 118 |
+
outputs=full_model.layers[-i-2].output
|
| 119 |
+
)
|
| 120 |
+
print(f"✓ Created extractor by removing last {i+1} classification layers")
|
| 121 |
+
return extractor
|
| 122 |
+
|
| 123 |
+
except Exception as e:
|
| 124 |
+
print(f"✗ Strategy C failed: {e}")
|
| 125 |
+
|
| 126 |
+
return None
|
| 127 |
+
|
| 128 |
+
# ======================================================
|
| 129 |
+
# FALLBACK EXTRACTOR
|
| 130 |
# ======================================================
|
| 131 |
def create_fallback_extractor():
|
| 132 |
+
"""Create fallback extractor if hybrid model fails"""
|
| 133 |
+
print("\nCreating fallback MobileNetV2 extractor...")
|
| 134 |
+
|
| 135 |
base_model = tf.keras.applications.MobileNetV2(
|
| 136 |
input_shape=(IMG_SIZE, IMG_SIZE, 3),
|
| 137 |
include_top=False,
|
|
|
|
| 140 |
)
|
| 141 |
base_model.trainable = False
|
| 142 |
|
| 143 |
+
inputs = Input(shape=(IMG_SIZE, IMG_SIZE, 3))
|
|
|
|
|
|
|
|
|
|
| 144 |
x = tf.keras.applications.mobilenet_v2.preprocess_input(inputs)
|
|
|
|
|
|
|
| 145 |
features = base_model(x, training=False)
|
| 146 |
|
| 147 |
+
# Add similar architecture to your hybrid model
|
| 148 |
x = tf.keras.layers.Dense(512, activation="relu")(features)
|
| 149 |
x = tf.keras.layers.Dropout(0.3)(x)
|
| 150 |
x = tf.keras.layers.Dense(256, activation="relu")(x)
|
| 151 |
+
x = tf.keras.layers.Dense(128, activation="relu")(x)
|
| 152 |
|
| 153 |
+
model = Model(inputs, x, name="fallback_extractor")
|
| 154 |
+
print(f"✓ Fallback extractor created. Output shape: {model.output_shape}")
|
|
|
|
|
|
|
| 155 |
return model
|
| 156 |
|
| 157 |
# ======================================================
|
| 158 |
+
# LOAD MODELS
|
| 159 |
# ======================================================
|
| 160 |
extractor, classifier = None, None
|
| 161 |
|
| 162 |
def load_models():
|
| 163 |
global extractor, classifier
|
| 164 |
+
|
| 165 |
+
print("\n" + "="*50)
|
| 166 |
+
print("LOADING HYBRID MODEL")
|
| 167 |
+
print("="*50)
|
| 168 |
+
|
| 169 |
+
# 1. Try to load hybrid model with debugging
|
| 170 |
+
extractor = debug_hybrid_model()
|
| 171 |
+
|
| 172 |
+
if extractor is None:
|
| 173 |
+
print("\n❌ Could not load hybrid_model.keras")
|
| 174 |
+
print("Creating fallback extractor...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
extractor = create_fallback_extractor()
|
| 176 |
+
else:
|
| 177 |
+
print(f"\n✅ Hybrid model loaded successfully!")
|
| 178 |
+
print(f" Input shape: {extractor.input_shape}")
|
| 179 |
+
print(f" Output shape: {extractor.output_shape}")
|
| 180 |
+
print(f" Number of layers: {len(extractor.layers)}")
|
| 181 |
|
| 182 |
+
# Test the extractor
|
| 183 |
+
print("\n🧪 Testing extractor with random input...")
|
| 184 |
+
test_input = np.random.randn(1, IMG_SIZE, IMG_SIZE, 3).astype(np.float32)
|
| 185 |
+
test_output = extractor.predict(test_input, verbose=0)
|
| 186 |
+
print(f" Test output shape: {test_output.shape}")
|
| 187 |
+
|
| 188 |
+
# 2. Load classifier
|
| 189 |
+
print("\n" + "="*50)
|
| 190 |
+
print("LOADING CLASSIFIER")
|
| 191 |
+
print("="*50)
|
| 192 |
+
|
| 193 |
+
try:
|
| 194 |
+
classifier_files = ["gbdt_model.pkl", "classifier.pkl", "rf_model.pkl"]
|
| 195 |
|
| 196 |
+
for cf in classifier_files:
|
| 197 |
+
if os.path.exists(cf):
|
| 198 |
+
classifier = joblib.load(cf)
|
| 199 |
+
print(f"✓ Loaded classifier: {cf}")
|
| 200 |
+
print(f" Type: {type(classifier).__name__}")
|
| 201 |
+
|
| 202 |
+
# Check if it's a pipeline
|
| 203 |
+
if hasattr(classifier, 'steps'):
|
| 204 |
+
print(f" Pipeline steps: {[name for name, _ in classifier.steps]}")
|
| 205 |
+
|
| 206 |
+
# Test classifier
|
| 207 |
+
if extractor is not None:
|
| 208 |
+
output_dim = extractor.output_shape[-1]
|
| 209 |
+
test_features = np.random.randn(1, output_dim)
|
| 210 |
+
test_pred = classifier.predict(test_features)
|
| 211 |
+
print(f" Test prediction: {test_pred[0]}")
|
| 212 |
break
|
|
|
|
|
|
|
|
|
|
| 213 |
except Exception as e:
|
| 214 |
+
print(f"✗ Classifier loading failed: {e}")
|
|
|
|
| 215 |
|
| 216 |
+
# Create simple fallback
|
| 217 |
from sklearn.ensemble import RandomForestClassifier
|
| 218 |
+
output_dim = extractor.output_shape[-1] if extractor else 128
|
| 219 |
+
classifier = RandomForestClassifier(n_estimators=50, random_state=42)
|
| 220 |
+
dummy_features = np.random.randn(100, output_dim)
|
| 221 |
+
dummy_labels = np.random.randint(0, 2, 100)
|
| 222 |
+
classifier.fit(dummy_features, dummy_labels)
|
| 223 |
+
print("✓ Created fallback classifier")
|
| 224 |
+
|
| 225 |
+
print("\n" + "="*50)
|
| 226 |
+
print("MODELS READY FOR INFERENCE")
|
| 227 |
+
print("="*50)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
# ======================================================
|
| 230 |
+
# PREPROCESSING FOR HYBRID MODEL
|
| 231 |
# ======================================================
|
| 232 |
def preprocess_image(img):
|
| 233 |
+
"""Preprocess image for the hybrid model"""
|
| 234 |
+
# Convert to numpy
|
| 235 |
if isinstance(img, Image.Image):
|
| 236 |
img = np.array(img)
|
| 237 |
|
| 238 |
+
# Handle different formats
|
| 239 |
if len(img.shape) == 2: # Grayscale
|
| 240 |
img = np.stack([img] * 3, axis=-1)
|
| 241 |
elif img.shape[2] == 4: # RGBA
|
| 242 |
img = img[:, :, :3]
|
|
|
|
|
|
|
| 243 |
|
| 244 |
+
# Convert to RGB if needed
|
| 245 |
if img.shape[2] == 3:
|
| 246 |
+
# Check if BGR (OpenCV)
|
| 247 |
+
if img[0, 0, 0] > img[0, 0, 2]: # Blue > Red
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 249 |
|
| 250 |
+
# Resize
|
| 251 |
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
|
| 252 |
|
| 253 |
+
# Normalize to [0, 1] - common for custom models
|
| 254 |
+
img = img.astype(np.float32) / 255.0
|
| 255 |
|
| 256 |
+
return img
|
|
|
|
| 257 |
|
| 258 |
# ======================================================
|
| 259 |
+
# PREDICTION
|
| 260 |
# ======================================================
|
| 261 |
def predict(img):
|
| 262 |
+
"""Make prediction using hybrid model"""
|
| 263 |
try:
|
| 264 |
+
# Preprocess
|
| 265 |
+
img_processed = preprocess_image(img)
|
| 266 |
+
img_batch = np.expand_dims(img_processed, axis=0)
|
| 267 |
|
| 268 |
# Extract features
|
| 269 |
+
features = extractor.predict(img_batch, verbose=0)
|
| 270 |
|
| 271 |
+
# Flatten if needed
|
| 272 |
if len(features.shape) > 2:
|
| 273 |
features = features.reshape(features.shape[0], -1)
|
|
|
|
|
|
|
| 274 |
|
| 275 |
+
# Classify
|
| 276 |
pred = classifier.predict(features)[0]
|
| 277 |
|
| 278 |
+
# Get confidence
|
| 279 |
try:
|
| 280 |
proba = classifier.predict_proba(features)[0]
|
| 281 |
confidence = proba[pred] * 100
|
| 282 |
+
except:
|
| 283 |
+
confidence = 80.0 # Default confidence
|
|
|
|
|
|
|
| 284 |
|
| 285 |
+
# Return results
|
| 286 |
label = "Real" if pred == 0 else "Fake"
|
| 287 |
+
return {
|
| 288 |
+
"Real": confidence if label == "Real" else 100 - confidence,
|
|
|
|
|
|
|
| 289 |
"Fake": confidence if label == "Fake" else 100 - confidence
|
| 290 |
}
|
| 291 |
|
|
|
|
|
|
|
| 292 |
except Exception as e:
|
| 293 |
print(f"Prediction error: {e}")
|
|
|
|
| 294 |
return {"Real": 50.0, "Fake": 50.0}
|
| 295 |
|
| 296 |
# ======================================================
|
| 297 |
+
# CREATE INTERFACE
|
| 298 |
# ======================================================
|
| 299 |
+
def create_interface():
|
| 300 |
+
"""Create Gradio interface"""
|
| 301 |
+
# Load models first
|
|
|
|
| 302 |
load_models()
|
|
|
|
| 303 |
|
| 304 |
# Create interface
|
| 305 |
iface = gr.Interface(
|
| 306 |
fn=predict,
|
| 307 |
inputs=gr.Image(
|
| 308 |
+
type="pil",
|
| 309 |
label="Upload Image",
|
| 310 |
image_mode="RGB"
|
| 311 |
),
|
| 312 |
outputs=gr.Label(
|
| 313 |
+
num_top_classes=2,
|
| 314 |
+
label="Prediction"
|
|
|
|
| 315 |
),
|
| 316 |
+
title="Hybrid Model Fake Image Detector",
|
| 317 |
+
description="Using hybrid_model.keras + GBDT classifier",
|
| 318 |
+
theme=gr.themes.Soft()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
)
|
| 320 |
|
| 321 |
return iface
|
| 322 |
|
| 323 |
# ======================================================
|
| 324 |
+
# MAIN
|
| 325 |
# ======================================================
|
| 326 |
if __name__ == "__main__":
|
| 327 |
+
print("\n🚀 Starting Hybrid Model Detector...")
|
| 328 |
+
|
| 329 |
+
# Create and launch
|
| 330 |
+
interface = create_interface()
|
| 331 |
|
| 332 |
+
interface.launch(
|
|
|
|
|
|
|
| 333 |
server_name="0.0.0.0",
|
| 334 |
server_port=7860,
|
| 335 |
+
share=False
|
|
|
|
| 336 |
)
|