hb-setosys commited on
Commit
e7ea0c7
·
verified ·
1 Parent(s): e79bd64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -16
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 the Keras model
6
  model = tf.keras.models.load_model("denis_mnist_cnn_model.h5")
7
 
8
- # Define a function to preprocess input and make predictions
9
  def predict(image):
10
- # Preprocess the image (resize, normalize, etc.)
11
- image = tf.image.resize(image, (224, 224)) # Example: Resize to 224x224
12
- image = np.expand_dims(image, axis=0) # Add batch dimension
13
  image = image / 255.0 # Normalize pixel values
14
-
15
- # Perform prediction
 
 
 
16
  prediction = model.predict(image)
17
  return {"prediction": prediction.tolist()}
18
-
19
 
20
- # Create a Gradio interface
21
  interface = gr.Interface(
22
- fn=predict,
23
- inputs="image", # Text input for comma-separated values
24
- outputs="json" # JSON output for prediction results
25
  )
26
 
27
- # Launch the Gradio app
28
- if __name__ == "__main__":
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)