File size: 883 Bytes
76c6334
de1b425
 
76c6334
de1b425
 
 
 
 
76c6334
de1b425
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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()