File size: 1,573 Bytes
effa7b6
 
 
 
c004a97
effa7b6
78faf07
 
1d49584
effa7b6
b74b3c5
c004a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78faf07
 
c004a97
78faf07
 
c004a97
 
78faf07
 
c004a97
 
 
 
 
effa7b6
c004a97
effa7b6
 
c004a97
78faf07
c004a97
 
 
8e2f3c0
 
 
9a391ca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import gradio as gr
import xgboost as xgb
import numpy as np
from PIL import Image
from image_functions import extract_features_for_ml

# Load XGBoost model (sklearn wrapper)
model = xgb.XGBClassifier()
model.load_model("XGBoost_Artifact_OpenFake.json")

def predict(image):
    """
    Classify image as real or fake.
    
    Args:
        image: PIL Image from Gradio
        threshold: Classification threshold
    
    Returns:
        Dictionary with probabilities for Gradio Label component
    """
    try:
        # Convert PIL Image to numpy array (RGB)
        img_array = np.array(image.convert("RGB"))
        
        # Extract features using your pipeline
        features = extract_features_for_ml(img_array)
        
        # Handle NaN values
        features = np.nan_to_num(features, nan=0.0)
        
        # Reshape for prediction
        X = features.reshape(1, -1)
        
        # Get prediction probabilities
        y_proba = model.predict_proba(X)[0]
        
        return {
            "Real": float(y_proba[0]),
            "Fake": float(y_proba[1])
        }
        
    except Exception as e:
        print(f"Error during prediction: {e}")
        return {"Error": 1.0}

# Create Gradio interface
demo = gr.Interface(
    fn=predict,
    inputs=[
        gr.Image(type="pil", label="Upload Image"),
    ],
    outputs=gr.Label(num_top_classes=2, label="Prediction"),
    title="Image Fake Detector",
    description="Upload an image to classify it as real or fake using image statistics with XGBoost."
)

demo.launch(show_error=True)