File size: 1,687 Bytes
aeea6fb
 
 
 
 
 
0fe1a7a
aeea6fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fe1a7a
 
 
 
 
aeea6fb
 
0fe1a7a
 
aeea6fb
0fe1a7a
aeea6fb
 
 
 
0fe1a7a
 
 
aeea6fb
 
 
 
0fe1a7a
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
59
60
61
import gradio as gr
import torch
from torchvision import models, transforms
from PIL import Image
import torch.nn.functional as F
import torch.nn as nn
import numpy as np

# Class labels
class_names = ['fake', 'real']

# Image transform
data_transforms = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

# Load model
def load_model():
    model = models.densenet121(weights='IMAGENET1K_V1')
    model.classifier = nn.Sequential(
        nn.Linear(1024, 512),
        nn.ReLU(),
        nn.Dropout(0.5),
        nn.Linear(512, 2)
    )
    device = torch.device('cpu')  # Use CPU for Hugging Face
    model = model.to(device)
    checkpoint = torch.load("best_model.pth", map_location=device)
    model.load_state_dict(checkpoint['model_state_dict'])
    model.eval()
    return model, device

model, device = load_model()

# Inference function
def predict(image):
    # Convert numpy array to PIL Image if needed
    if isinstance(image, np.ndarray):
        image = Image.fromarray(image)
    
    # Convert RGBA to RGB if needed
    if image.mode == "RGBA":
        image = image.convert("RGB")
    
    # Apply transforms
    image = data_transforms(image).unsqueeze(0).to(device)
    
    with torch.no_grad():
        outputs = model(image)
        probs = F.softmax(outputs, dim=1)
        conf, pred = torch.max(probs, 1)
        label = class_names[pred.item()]
        confidence = f"{conf.item() * 100:.2f}%"
    
    return f"{label} ({confidence})"

# Gradio interface
demo = gr.Interface(fn=predict, inputs="image", outputs="text", api_name="predict")
demo.launch()