psinha823 commited on
Commit
a2b62f9
·
verified ·
1 Parent(s): d88b542

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -4
app.py CHANGED
@@ -37,9 +37,17 @@ import gradio as gr
37
  import tensorflow as tf
38
  from tensorflow.keras.preprocessing import image
39
  import numpy as np
 
 
 
 
40
 
41
  # Load the trained model
42
- model = tf.keras.models.load_model('./model12_acc99_kera.h5')
 
 
 
 
43
 
44
  # Define the class names
45
  classes = ['Colon Adenocarcinoma', 'Colon Benign Tissue', 'Lung Adenocarcinoma', 'Lung Benign Tissue', 'Lung Squamous Cell Carcinoma']
@@ -47,20 +55,27 @@ classes = ['Colon Adenocarcinoma', 'Colon Benign Tissue', 'Lung Adenocarcinoma',
47
  # Function to preprocess the uploaded image and make predictions
48
  def predict(img):
49
  try:
 
 
50
  # Resize and preprocess the image
51
  img = img.resize((224, 224))
52
  img_array = image.img_to_array(img)
53
  img_array = np.expand_dims(img_array, axis=0)
54
 
 
 
55
  # Make predictions
56
  predictions = model.predict(img_array)
57
  predicted_class_index = np.argmax(predictions[0])
58
  predicted_class = classes[predicted_class_index]
59
 
 
 
60
  # Return the predicted class and the raw predictions
61
- return predicted_class, predictions[0]
62
  except Exception as e:
63
- # Print the error message
 
64
  return str(e), str(e)
65
 
66
  # Create a Gradio interface
@@ -76,4 +91,4 @@ iface = gr.Interface(
76
  )
77
 
78
  # Launch the interface
79
- iface.launch()
 
37
  import tensorflow as tf
38
  from tensorflow.keras.preprocessing import image
39
  import numpy as np
40
+ import logging
41
+
42
+ # Set up logging
43
+ logging.basicConfig(level=logging.DEBUG)
44
 
45
  # Load the trained model
46
+ try:
47
+ model = tf.keras.models.load_model('path/to/your/model.h5')
48
+ logging.info("Model loaded successfully.")
49
+ except Exception as e:
50
+ logging.error(f"Error loading model: {e}")
51
 
52
  # Define the class names
53
  classes = ['Colon Adenocarcinoma', 'Colon Benign Tissue', 'Lung Adenocarcinoma', 'Lung Benign Tissue', 'Lung Squamous Cell Carcinoma']
 
55
  # Function to preprocess the uploaded image and make predictions
56
  def predict(img):
57
  try:
58
+ logging.debug("Received image for prediction.")
59
+
60
  # Resize and preprocess the image
61
  img = img.resize((224, 224))
62
  img_array = image.img_to_array(img)
63
  img_array = np.expand_dims(img_array, axis=0)
64
 
65
+ logging.debug("Image preprocessed successfully.")
66
+
67
  # Make predictions
68
  predictions = model.predict(img_array)
69
  predicted_class_index = np.argmax(predictions[0])
70
  predicted_class = classes[predicted_class_index]
71
 
72
+ logging.debug(f"Prediction successful: {predicted_class}")
73
+
74
  # Return the predicted class and the raw predictions
75
+ return predicted_class, predictions[0].tolist()
76
  except Exception as e:
77
+ logging.error(f"Error during prediction: {e}")
78
+ # Print the error message in the output
79
  return str(e), str(e)
80
 
81
  # Create a Gradio interface
 
91
  )
92
 
93
  # Launch the interface
94
+ iface.launch()