Aniketvok commited on
Commit
7649d32
·
verified ·
1 Parent(s): 53d238c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py CHANGED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import matplotlib.pyplot as plt
3
+ import tensorflow as tf
4
+ from tensorflow.keras import datasets, layers, models
5
+ import gradio as gr
6
+ from tensorflow.keras.preprocessing import image
7
+
8
+ # Load CIFAR-10 dataset
9
+ (x_train, y_train), (x_test, y_test) = datasets.cifar10.load_data()
10
+ x_train, x_test = x_train / 255.0, x_test / 255.0
11
+
12
+ # CIFAR-10 class names
13
+ class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
14
+ 'dog', 'frog', 'horse', 'ship', 'truck']
15
+
16
+ # Build model
17
+ model = models.Sequential([
18
+ layers.Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)),
19
+ layers.MaxPooling2D((2,2)),
20
+
21
+ layers.Conv2D(64, (3,3), activation='relu'),
22
+ layers.MaxPooling2D((2,2)),
23
+
24
+ layers.Conv2D(64, (3,3), activation='relu'),
25
+
26
+ layers.Flatten(),
27
+ layers.Dense(64, activation='relu'),
28
+ layers.Dense(10, activation='softmax')
29
+ ])
30
+
31
+ # Compile and train briefly (use more epochs for better accuracy)
32
+ model.compile(optimizer='adam',
33
+ loss='sparse_categorical_crossentropy',
34
+ metrics=['accuracy'])
35
+ model.fit(x_train, y_train, epochs=1, validation_data=(x_test, y_test))
36
+
37
+ # Prediction function
38
+ def predict(img):
39
+ img = image.smart_resize(img, (32, 32)) # Resize to CIFAR-10 size
40
+ img_array = np.expand_dims(img, axis=0) / 255.0
41
+ prediction = model.predict(img_array)
42
+ pred_class = np.argmax(prediction)
43
+ return {class_names[i]: float(prediction[0][i]) for i in range(10)}
44
+
45
+ # Gradio interface
46
+ demo = gr.Interface(
47
+ fn=predict,
48
+ inputs=gr.Image(type="numpy"),
49
+ outputs=gr.Label(num_top_classes=3),
50
+ title="CIFAR-10 Image Classifier",
51
+ description="Upload an image and the model will predict which CIFAR-10 class it belongs to."
52
+ )
53
+
54
+ demo.launch()