cisemh commited on
Commit
35d6a79
·
verified ·
1 Parent(s): 3ee64d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -4
app.py CHANGED
@@ -1,7 +1,38 @@
 
 
1
  import gradio as gr
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ import numpy as np
3
  import gradio as gr
4
+ from PIL import Image
5
 
6
+ # Load the saved model
7
+ model = tf.keras.models.load_model('cifar10_cnn_model.keras')
8
 
9
+ # Define the CIFAR-10 class names
10
+ class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
11
+
12
+ # Define a function to preprocess the input image
13
+ def preprocess_image(image):
14
+ image = image.resize((32, 32)) # Resize image to 32x32
15
+ image = np.array(image) / 255.0 # Normalize pixel values
16
+ image = np.expand_dims(image, axis=0) # Add batch dimension
17
+ return image
18
+
19
+ # Define the prediction function
20
+ def classify_image(image):
21
+ preprocessed_image = preprocess_image(image)
22
+ predictions = model.predict(preprocessed_image)
23
+ predicted_class = class_names[np.argmax(predictions)]
24
+ confidence = np.max(predictions)
25
+ return f"Prediction: {predicted_class} (Confidence: {confidence:.2f})"
26
+
27
+ # Create the Gradio interface
28
+ interface = gr.Interface(
29
+ fn=classify_image,
30
+ inputs=gr.Image(type="pil"),
31
+ outputs="text",
32
+ title="CIFAR-10 Image Classifier",
33
+ description="Upload an image of a CIFAR-10 category (e.g., airplane, cat, dog), and the model will classify it."
34
+ )
35
+
36
+ # Launch the app
37
+ if __name__ == "__main__":
38
+ interface.launch()