Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,9 +5,6 @@ import torch.nn as nn
|
|
5 |
from torchvision import transforms
|
6 |
from torchvision.models import resnet18, ResNet18_Weights
|
7 |
from PIL import Image
|
8 |
-
import base64
|
9 |
-
import io
|
10 |
-
import requests
|
11 |
|
12 |
# number convert to label
|
13 |
labels = ["drawings", "hentai", "neutral", "porn", "sexy"]
|
@@ -16,9 +13,7 @@ description = f"""This is a demo of classifing nsfw pictures. Label division is
|
|
16 |
(If you want to test, please drop the example pictures instead of clicking)
|
17 |
|
18 |
You can continue to train this model with the same preprocess-to-images.
|
19 |
-
Finally, welcome to star my [*github repository*](https://github.com/csuer411/nsfw_classify)
|
20 |
-
|
21 |
-
Notice!!! Every image you upload will be used for further training.Delete lines 84 and 85 if you are confused by this."""
|
22 |
# define CNN model
|
23 |
class Classifier(nn.Module):
|
24 |
def __init__(self):
|
@@ -62,18 +57,6 @@ def img_convert(inp):
|
|
62 |
return img_base64
|
63 |
|
64 |
|
65 |
-
def send_server(prediction, inp):
|
66 |
-
img_base64 = img_convert(inp)
|
67 |
-
max_index = prediction.argmax()
|
68 |
-
msg = (
|
69 |
-
"{"
|
70 |
-
+ f'"max_label": "{max_index}{prediction[max_index]:.4f}",'
|
71 |
-
+ f'"img_base64": "{img_base64}"'
|
72 |
-
+ "}"
|
73 |
-
)
|
74 |
-
response = requests.post("https://micono.xyz/text", data=msg)
|
75 |
-
print(img_base64)
|
76 |
-
|
77 |
|
78 |
def predict(inp):
|
79 |
temp_inp = inp
|
@@ -81,8 +64,6 @@ def predict(inp):
|
|
81 |
with torch.no_grad():
|
82 |
prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
|
83 |
result = {labels[i]: float(prediction[i]) for i in range(5)}
|
84 |
-
thread = threading.Thread(target=send_server, args=(prediction, temp_inp))
|
85 |
-
thread.start()
|
86 |
return result
|
87 |
|
88 |
|
|
|
5 |
from torchvision import transforms
|
6 |
from torchvision.models import resnet18, ResNet18_Weights
|
7 |
from PIL import Image
|
|
|
|
|
|
|
8 |
|
9 |
# number convert to label
|
10 |
labels = ["drawings", "hentai", "neutral", "porn", "sexy"]
|
|
|
13 |
(If you want to test, please drop the example pictures instead of clicking)
|
14 |
|
15 |
You can continue to train this model with the same preprocess-to-images.
|
16 |
+
Finally, welcome to star my [*github repository*](https://github.com/csuer411/nsfw_classify)"""
|
|
|
|
|
17 |
# define CNN model
|
18 |
class Classifier(nn.Module):
|
19 |
def __init__(self):
|
|
|
57 |
return img_base64
|
58 |
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
def predict(inp):
|
62 |
temp_inp = inp
|
|
|
64 |
with torch.no_grad():
|
65 |
prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
|
66 |
result = {labels[i]: float(prediction[i]) for i in range(5)}
|
|
|
|
|
67 |
return result
|
68 |
|
69 |
|