Spaces:
Runtime error
Runtime error
Neel kamal sahu
commited on
Commit
·
6606691
1
Parent(s):
bed9229
first commit of NSFW classification
Browse files- app.py +45 -0
- my_model.h5 +3 -0
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import urllib
|
4 |
+
from keras.preprocessing import image
|
5 |
+
from keras.models import load_model
|
6 |
+
|
7 |
+
# Load the pre-trained model
|
8 |
+
model = load_model('my_model.h5')
|
9 |
+
CATEGORIES = ("NSFW", "SFW")
|
10 |
+
def classify_image(img):
|
11 |
+
# Preprocess the input image
|
12 |
+
img = image.img_to_array(img)
|
13 |
+
img = np.expand_dims(img, axis=0)
|
14 |
+
img /= 255.0
|
15 |
+
|
16 |
+
# Use the model to make a prediction
|
17 |
+
prediction = model.predict(img)
|
18 |
+
print(prediction)
|
19 |
+
# Map the predicted class to a label
|
20 |
+
if prediction[0][0] >= 0.5:
|
21 |
+
label = "SFW"
|
22 |
+
else:
|
23 |
+
label = "NSFW"
|
24 |
+
|
25 |
+
return label
|
26 |
+
|
27 |
+
def classify_url(url):
|
28 |
+
# Load the image from the URL
|
29 |
+
response = urllib.request.urlopen(url)
|
30 |
+
img = image.load_img(response, target_size=(224, 224))
|
31 |
+
|
32 |
+
return classify_image(img)
|
33 |
+
|
34 |
+
# Define the GRADIO input interface
|
35 |
+
#inputs = gr.inputs.Image(shape=(224, 224, 3))
|
36 |
+
|
37 |
+
# Define the GRADIO output interface
|
38 |
+
#outputs = gr.outputs.Textbox(lines=1, default="SFW")
|
39 |
+
|
40 |
+
# Define the GRADIO app
|
41 |
+
app = gr.Interface(classify_image, gr.Image(shape=(224, 224)), outputs=gr.Label(label="Type of Image"), allow_flagging="never", title="NSFW/SFW Classifier")
|
42 |
+
|
43 |
+
# Start the GRADIO app
|
44 |
+
app.launch()
|
45 |
+
|
my_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7117e488bc01eca0c8dbb644cafa4d7f0b0b559b873500e507ad2be32dd8d6a3
|
3 |
+
size 2245216
|