Spaces:
Runtime error
Runtime error
File size: 1,213 Bytes
6606691 |
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 |
import gradio as gr
import numpy as np
import urllib
from keras.preprocessing import image
from keras.models import load_model
# Load the pre-trained model
model = load_model('my_model.h5')
CATEGORIES = ("NSFW", "SFW")
def classify_image(img):
# Preprocess the input image
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img /= 255.0
# Use the model to make a prediction
prediction = model.predict(img)
print(prediction)
# Map the predicted class to a label
if prediction[0][0] >= 0.5:
label = "SFW"
else:
label = "NSFW"
return label
def classify_url(url):
# Load the image from the URL
response = urllib.request.urlopen(url)
img = image.load_img(response, target_size=(224, 224))
return classify_image(img)
# Define the GRADIO input interface
#inputs = gr.inputs.Image(shape=(224, 224, 3))
# Define the GRADIO output interface
#outputs = gr.outputs.Textbox(lines=1, default="SFW")
# Define the GRADIO app
app = gr.Interface(classify_image, gr.Image(shape=(224, 224)), outputs=gr.Label(label="Type of Image"), allow_flagging="never", title="NSFW/SFW Classifier")
# Start the GRADIO app
app.launch()
|