Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,23 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
|
5 |
+
# Load the pre-trained model
|
6 |
+
model = tf.keras.applications.MobileNetV2()
|
7 |
+
labels_path = tf.keras.utils.get_file(
|
8 |
+
'ImageNetLabels.txt', 'https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt')
|
9 |
+
imagenet_labels = np.array(open(labels_path).read().splitlines())
|
10 |
|
11 |
+
# Define the prediction function
|
12 |
+
def classify_image(image):
|
13 |
+
image = tf.keras.applications.mobilenet_v2.preprocess_input(image)
|
14 |
+
predictions = model.predict(np.expand_dims(image, axis=0))
|
15 |
+
return {imagenet_labels[i]: float(predictions[0][i]) for i in range(1000)}
|
16 |
+
|
17 |
+
# Create a Gradio interface
|
18 |
+
inputs = gr.inputs.Image(shape=(224, 224))
|
19 |
+
outputs = gr.outputs.Label(num_top_classes=3)
|
20 |
+
interface = gr.Interface(fn=classify_image, inputs=inputs, outputs=outputs, capture_session=True)
|
21 |
+
|
22 |
+
# Launch the interface
|
23 |
+
interface.launch()
|