hb-setosys commited on
Commit
e1504de
·
verified ·
1 Parent(s): 42ed001

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py CHANGED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from tensorflow.keras.applications.resnet50 import preprocess_input
5
+ from tensorflow.keras.utils import load_img, img_to_array
6
+
7
+ # Load your trained model
8
+ model = tf.keras.models.load_model("denis_mnist_cnn_model_resnet50_v1.h5") # Ensure you upload this file to Hugging Face Spaces
9
+
10
+ # Define a function to preprocess the image
11
+ def preprocess_image(image):
12
+ """
13
+ Preprocesses the uploaded image for prediction.
14
+ """
15
+ image = image.resize((128, 128)) # Resize to match the model input size
16
+ image = img_to_array(image) # Convert PIL image to NumPy array
17
+ image = preprocess_input(image) # Normalize for ResNet50
18
+ image = np.expand_dims(image, axis=0) # Add batch dimension
19
+ return image
20
+
21
+ # Define the prediction function
22
+ def predict(image):
23
+ """
24
+ Accepts an image, preprocesses it, and returns the predicted label.
25
+ """
26
+ processed_image = preprocess_image(image)
27
+ predictions = model.predict(processed_image)
28
+ predicted_class = np.argmax(predictions, axis=1)[0] # Get the class index
29
+ confidence = np.max(predictions) # Get confidence score
30
+ return f"Predicted Class: {predicted_class}, Confidence: {confidence:.2f}"
31
+
32
+ # Create a Gradio interface
33
+ interface = gr.Interface(
34
+ fn=predict, # The prediction function
35
+ inputs=gr.inputs.Image(type="pil", label="Upload an Image"), # Input: Image
36
+ outputs="text", # Output: Text
37
+ title="MNIST ResNet50 Classifier",
38
+ description="Upload an image to classify it using the trained ResNet50 model.",
39
+ examples=[
40
+ ["example_images/example1.png"], # Add paths to example images in your Hugging Face repository
41
+ ["example_images/example2.png"]
42
+ ],
43
+ )
44
+
45
+ # Launch the app
46
+ if __name__ == "__main__":
47
+ interface.launch()