Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
|
|
4 |
|
5 |
-
# Load
|
6 |
model = tf.keras.models.load_model("denis_mnist_cnn_model.h5")
|
7 |
|
8 |
-
# Define
|
9 |
def predict(image):
|
10 |
-
|
11 |
-
image = tf.image.resize(image, (224, 224)) #
|
12 |
-
image = np.expand_dims(image, axis=0) # Add batch dimension
|
13 |
image = image / 255.0 # Normalize pixel values
|
14 |
-
|
15 |
-
#
|
|
|
|
|
|
|
16 |
prediction = model.predict(image)
|
17 |
return {"prediction": prediction.tolist()}
|
18 |
-
|
19 |
|
20 |
-
# Create
|
21 |
interface = gr.Interface(
|
22 |
-
fn=predict,
|
23 |
-
inputs="image", #
|
24 |
-
outputs="json" #
|
25 |
)
|
26 |
|
27 |
-
# Launch the
|
28 |
-
|
29 |
-
interface.launch(share=True)
|
30 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
|
6 |
+
# Load your model
|
7 |
model = tf.keras.models.load_model("denis_mnist_cnn_model.h5")
|
8 |
|
9 |
+
# Define the prediction function
|
10 |
def predict(image):
|
11 |
+
image = np.array(image) # Convert to numpy array
|
12 |
+
image = tf.image.resize(image, (224, 224)) # Resize to the model's expected input size
|
13 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension (model expects a batch of images)
|
14 |
image = image / 255.0 # Normalize pixel values
|
15 |
+
|
16 |
+
# Check if the model needs flattening
|
17 |
+
if len(image.shape) == 4: # Check if image has a batch dimension
|
18 |
+
image = tf.keras.layers.Flatten()(image) # Flatten the image if necessary
|
19 |
+
|
20 |
prediction = model.predict(image)
|
21 |
return {"prediction": prediction.tolist()}
|
|
|
22 |
|
23 |
+
# Create the Gradio interface
|
24 |
interface = gr.Interface(
|
25 |
+
fn=predict,
|
26 |
+
inputs="image", # Image input
|
27 |
+
outputs="json", # Output as JSON
|
28 |
)
|
29 |
|
30 |
+
# Launch the interface
|
31 |
+
interface.launch(share=True)
|
|
|
|