Spaces:
Runtime error
Runtime error
import gradio as gr | |
import tensorflow as tf | |
import numpy as np | |
# Load the pre-trained model | |
model = tf.keras.applications.MobileNetV2() | |
labels_path = tf.keras.utils.get_file( | |
'ImageNetLabels.txt', 'https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt') | |
imagenet_labels = np.array(open(labels_path).read().splitlines()) | |
# Define the prediction function | |
def classify_image(image): | |
image = tf.keras.applications.mobilenet_v2.preprocess_input(image) | |
predictions = model.predict(np.expand_dims(image, axis=0)) | |
return {imagenet_labels[i]: float(predictions[0][i]) for i in range(1000)} | |
# Create a Gradio interface | |
inputs = gr.inputs.Image(shape=(224, 224)) | |
outputs = gr.outputs.Label(num_top_classes=3) | |
interface = gr.Interface(fn=classify_image, inputs=inputs, outputs=outputs, capture_session=True) | |
# Launch the interface | |
interface.launch() | |