brxerq commited on
Commit
6c71344
1 Parent(s): c1b2c58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -16
app.py CHANGED
@@ -3,25 +3,26 @@ import gradio as gr
3
  import numpy as np
4
  import tensorflow as tf
5
  import cv2
 
6
 
7
  # Load class labels from the text file
8
  train_info = []
9
  with open('labelwithspace.txt', 'r') as file:
10
  train_info = [line.strip() for line in file.readlines()]
11
 
12
- # Construct a simple mock model using the Functional API
13
- def create_mock_model():
14
- inputs = tf.keras.Input(shape=(224, 224, 3))
15
- # A simple convolutional base (for demonstration purposes)
16
- x = tf.keras.layers.Conv2D(32, (3, 3), activation='relu')(inputs)
17
- x = tf.keras.layers.MaxPooling2D((2, 2))(x)
18
- x = tf.keras.layers.Flatten()(x)
19
- outputs = tf.keras.layers.Dense(len(train_info), activation='softmax')(x)
20
- model = tf.keras.Model(inputs, outputs)
21
  return model
22
 
23
- # Initialize the mock model
24
- model = create_mock_model()
25
 
26
  # Function to preprocess the image and make predictions
27
  def predict_image(image):
@@ -29,16 +30,16 @@ def predict_image(image):
29
  img = cv2.resize(image, (224, 224))
30
  img = img / 255.0 # Normalize to [0, 1] range
31
 
32
- # Make predictions using the mock model
33
  predictions = model.predict(img[np.newaxis, ...])[0]
34
  top_classes = np.argsort(predictions)[-3:][::-1] # Indices of top 3 predictions
35
  top_class = top_classes[0] # Index of the highest probability class
36
  label = train_info[top_class] # Get the corresponding label
37
  return label
38
 
39
- # Define the Gradio interface without the 'shape' argument
40
- input_image = gr.Image() # Removed 'shape' argument
41
  output_label = gr.Label()
42
 
43
- # Launch the Gradio interface
44
- gr.Interface(fn=predict_image, inputs=input_image, outputs=output_label, capture_session=True).launch()
 
3
  import numpy as np
4
  import tensorflow as tf
5
  import cv2
6
+ import tensorflow_hub as hub
7
 
8
  # Load class labels from the text file
9
  train_info = []
10
  with open('labelwithspace.txt', 'r') as file:
11
  train_info = [line.strip() for line in file.readlines()]
12
 
13
+ # Load your actual model from the .h5 file
14
+ def load_real_model():
15
+ try:
16
+ # Register KerasLayer from TensorFlow Hub if used
17
+ custom_objects = {'KerasLayer': hub.KerasLayer}
18
+ model = tf.keras.models.load_model('bird_model4.h5', custom_objects=custom_objects)
19
+ except Exception as e:
20
+ print("Error loading the model:", e)
21
+ exit()
22
  return model
23
 
24
+ # Initialize the real model
25
+ model = load_real_model()
26
 
27
  # Function to preprocess the image and make predictions
28
  def predict_image(image):
 
30
  img = cv2.resize(image, (224, 224))
31
  img = img / 255.0 # Normalize to [0, 1] range
32
 
33
+ # Make predictions using the loaded model
34
  predictions = model.predict(img[np.newaxis, ...])[0]
35
  top_classes = np.argsort(predictions)[-3:][::-1] # Indices of top 3 predictions
36
  top_class = top_classes[0] # Index of the highest probability class
37
  label = train_info[top_class] # Get the corresponding label
38
  return label
39
 
40
+ # Define the Gradio interface
41
+ input_image = gr.Image()
42
  output_label = gr.Label()
43
 
44
+ # Launch the Gradio interface for Hugging Face deployment
45
+ gr.Interface(fn=predict_image, inputs=input_image, outputs=output_label).launch()