yuragoithf commited on
Commit
4e2ea8a
·
1 Parent(s): 0de8536

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -28
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
- import numpy as np
4
  import gdown
5
  from PIL import Image
6
 
@@ -36,41 +35,22 @@ model = tf.keras.models.load_model(model_file)
36
  def predict_class(image):
37
  img = tf.cast(image, tf.float32)
38
  img = tf.image.resize(img, [input_shape[0], input_shape[1]])
39
- img = np.expand_dims(img, axis=0)
40
  prediction = model.predict(img)
41
- return prediction[0]
 
 
42
 
43
  # UI Design
44
  def classify_image(image):
45
- pred = predict_class(image)
46
- class_names = [
47
- "plane",
48
- "car",
49
- "bird",
50
- "cat",
51
- "deer",
52
- "dog",
53
- "frog",
54
- "horse",
55
- "ship",
56
- "truck",
57
- ]
58
-
59
- probabilities = tf.nn.softmax(pred)
60
- top_indices = tf.argsort(probabilities, direction='DESCENDING')
61
- top_classes = [class_names[idx] for idx in top_indices]
62
- top_probs = [probabilities[idx] for idx in top_indices]
63
-
64
- output = "<h3>Top 3 Predictions:</h3>"
65
- for i in range(3):
66
- output += f"<p>{top_classes[i]}: {top_probs[i]*100:.2f}%</p>"
67
-
68
  return output
69
 
70
  inputs = gr.inputs.Image(label="Upload an image")
71
  outputs = gr.outputs.HTML()
72
 
73
  title = "<h1 style='text-align: center;'>Image Classifier</h1>"
74
- description = "Upload an image and get the top 3 predictions."
75
 
76
- gr.Interface(fn=classify_image, inputs=inputs, outputs=outputs, title=title, description=description).launch()
 
1
  import gradio as gr
2
  import tensorflow as tf
 
3
  import gdown
4
  from PIL import Image
5
 
 
35
  def predict_class(image):
36
  img = tf.cast(image, tf.float32)
37
  img = tf.image.resize(img, [input_shape[0], input_shape[1]])
38
+ img = tf.expand_dims(img, axis=0)
39
  prediction = model.predict(img)
40
+ class_index = tf.argmax(prediction[0]).numpy()
41
+ predicted_class = labels[class_index]
42
+ return predicted_class
43
 
44
  # UI Design
45
  def classify_image(image):
46
+ predicted_class = predict_class(image)
47
+ output = f"<h2>Predicted Class:</h2><p>{predicted_class}</p>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  return output
49
 
50
  inputs = gr.inputs.Image(label="Upload an image")
51
  outputs = gr.outputs.HTML()
52
 
53
  title = "<h1 style='text-align: center;'>Image Classifier</h1>"
54
+ description = "Upload an image and get the predicted class."
55
 
56
+ gr.Interface(fn=classify_image, inputs=inputs, outputs=outputs, title=title, description=description).launch()