File size: 3,188 Bytes
f991906
 
 
5d6587e
cef1466
 
 
 
 
 
f991906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cef1466
 
 
f991906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cef1466
 
 
 
 
 
 
 
 
 
 
 
 
0d3952c
 
cef1466
 
 
0d3952c
 
cef1466
 
 
0d3952c
cef1466
7b8159f
 
0d3952c
cef1466
 
 
 
 
f991906
5d6587e
 
 
 
 
 
 
cef1466
f991906
 
7b8159f
f991906
0d3952c
d31fde5
cef1466
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import gradio as gr
import torch
import torch.nn as nn
from torch.utils.data import Dataset
import torchvision

from torchvision import transforms

#from torchvision import transforms
from PIL import Image

class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1)
        self.relu1 = nn.ReLU()
        self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
        
        self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1)
        self.relu2 = nn.ReLU()
        self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
        
        self.conv3 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
        self.relu3 = nn.ReLU()
        self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2)
        
        #self.fc1 = nn.Linear(in_features=262144, out_features=512)
        #self.fc1 = nn.Linear(in_features=4096, out_features=512) # hr_pytorch_model.py
        self.fc1 = nn.Linear(in_features=784, out_features=512)
        self.relu4 = nn.ReLU()
        
        self.fc2 = nn.Linear(in_features=512, out_features=2)
    
    def forward(self, x):
        x = self.conv1(x)
        x = self.relu1(x)
        x = self.pool1(x)
        
        x = self.conv2(x)
        x = self.relu2(x)
        x = self.pool2(x)
        
        x = self.conv3(x)
        x = self.relu3(x)
        x = self.pool3(x)
        
        # Flatten
        x = x.reshape(x.shape[0], -1) #this work
        
        x = self.fc1(x)
        x = self.relu4(x)
        
        x = self.fc2(x)
        
        return x
"""
transform = transforms.Compose(
    [transforms.Pad(2),
     transforms.ToTensor(),
     transforms.Normalize((0.5,), (0.5,))])
"""
# other transform
transform = transforms.Compose([
                transforms.Resize(256),
                transforms.CenterCrop(224),
                transforms.ToTensor(),
                transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
            ])

model = CNN()
#model.load_state_dict(torch.load('./best_model.nn'))
state_dict = torch.load('./pytorch_model.bin', map_location=torch.device('cpu'))
model.load_state_dict(state_dict, strict=False)
model.eval()

def predict(image):
    img = Image.open(image)
    img = transform(img)
    
    print("===============", img.shape)
    with torch.no_grad():
        pred = model(img)
        
    #is_ai = torch.max(pred.data, 0)[1]
    #print("===============", is_ai)
    probabilities = model(img).softmax(-1)[0,1].item()
    print("=============== prob", probabilities)
    return "AI" if probabilities > 0.3 else "Not AI"
    
"""  
gr.Interface.load(
    "huggingface/diallomama/AiorNot/blob/main/bes_model.nn",
    inputs=gr.Textbox(lines=5, label="Input Text"),
    outputs = "text"
).launch()
"""

gr.Interface(
    predict,
    inputs = gr.Image(label="Uploat an image", type="filepath"),
    #outputs = gr.outputs.Label(num_top_classes=2)
    outputs = "text"
).launch()
"""
gr.Interface(predict, inputs=gr.inputs.Image(shape=(512,512,3)), outputs="text").launch()
"""