Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,31 @@
|
|
1 |
-
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
|
|
4 |
|
5 |
-
# Load the
|
6 |
model = tf.keras.models.load_model("denis_mnist_cnn_model.h5")
|
7 |
|
8 |
-
#
|
9 |
-
def
|
10 |
-
#
|
11 |
image = np.array(image)
|
12 |
-
|
13 |
-
|
14 |
-
image =
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
# Add batch dimension
|
24 |
-
image = np.expand_dims(image, axis=0) # Add batch dimension to match the model's expected input shape (1, 28, 28, 3)
|
25 |
-
|
26 |
-
# Perform prediction
|
27 |
-
prediction = model.predict(image)
|
28 |
-
|
29 |
-
# Get the predicted class (index of the highest probability)
|
30 |
-
predicted_class = np.argmax(prediction)
|
31 |
-
|
32 |
-
# Return prediction as JSON (with the predicted class label)
|
33 |
return {"prediction": int(predicted_class)}
|
34 |
|
35 |
# Create a Gradio interface
|
36 |
-
interface = gr.Interface(
|
37 |
-
fn=predict,
|
38 |
-
inputs="image", # Image input for testing
|
39 |
-
outputs="json" # JSON output for prediction results
|
40 |
-
)
|
41 |
|
42 |
-
# Launch the interface
|
43 |
-
|
|
|
|
1 |
+
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
|
6 |
+
# Load the trained model
|
7 |
model = tf.keras.models.load_model("denis_mnist_cnn_model.h5")
|
8 |
|
9 |
+
# Preprocessing function for images
|
10 |
+
def preprocess_image(image):
|
11 |
+
# Resize the image to 28x28 as expected by the model
|
12 |
image = np.array(image)
|
13 |
+
image = tf.image.resize(image, (28, 28)) # Resize to 28x28
|
14 |
+
image = tf.image.grayscale_to_rgb(image) # Convert grayscale to RGB (3 channels)
|
15 |
+
image = image / 255.0 # Normalize pixel values to [0, 1]
|
16 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension
|
17 |
+
return image
|
18 |
+
|
19 |
+
# Function to make predictions
|
20 |
+
def predict(image):
|
21 |
+
image = preprocess_image(image)
|
22 |
+
prediction = model.predict(image) # Predict
|
23 |
+
predicted_class = np.argmax(prediction, axis=-1)[0] # Get the predicted class
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
return {"prediction": int(predicted_class)}
|
25 |
|
26 |
# Create a Gradio interface
|
27 |
+
interface = gr.Interface(fn=predict, inputs="image", outputs="json")
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
# Launch the Gradio interface
|
30 |
+
if __name__ == "__main__":
|
31 |
+
interface.launch()
|