Spaces:
Runtime error
Runtime error
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() | |