Suphawan commited on
Commit
43b0f1e
·
verified ·
1 Parent(s): faceedb

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -8,23 +8,30 @@ from PIL import Image
8
  # Define the path to your model file
9
  model_path = "best_model_InceptionV2.keras"
10
 
11
- # Load the trained InceptionV2 model
12
- model = tf.keras.models.load_model(model_path)
 
 
 
 
13
 
14
  # Function for prediction
15
  def predict(img):
16
- img_resized = img.resize((224, 224)) # Resize image to the target size
17
- img_array = image.img_to_array(img_resized) # Convert image to array
18
- img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
19
- img_array = preprocess_input(img_array) # Preprocess image according to model requirements
20
-
21
- predictions = model.predict(img_array)
22
- class_idx = np.argmax(predictions, axis=1)[0]
23
- class_labels = ['Benign', 'Malignant'] # Update according to your class labels
24
- class_label = class_labels[class_idx]
25
- confidence = float(predictions[0][class_idx])
26
-
27
- return f"Class: {class_label}, Confidence: {confidence:.2f}"
 
 
 
28
 
29
  # Define the Gradio app
30
  with gr.Blocks() as demo:
 
8
  # Define the path to your model file
9
  model_path = "best_model_InceptionV2.keras"
10
 
11
+ # Try to load the trained InceptionV2 model
12
+ try:
13
+ model = tf.keras.models.load_model(model_path)
14
+ print("Model loaded successfully.")
15
+ except Exception as e:
16
+ print(f"Error loading the model: {e}")
17
 
18
  # Function for prediction
19
  def predict(img):
20
+ try:
21
+ img_resized = img.resize((224, 224)) # Resize image to the target size
22
+ img_array = image.img_to_array(img_resized) # Convert image to array
23
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
24
+ img_array = preprocess_input(img_array) # Preprocess image according to model requirements
25
+
26
+ predictions = model.predict(img_array)
27
+ class_idx = np.argmax(predictions, axis=1)[0]
28
+ class_labels = ['Benign', 'Malignant'] # Update according to your class labels
29
+ class_label = class_labels[class_idx]
30
+ confidence = float(predictions[0][class_idx])
31
+
32
+ return f"Class: {class_label}, Confidence: {confidence:.2f}"
33
+ except Exception as e:
34
+ return f"Error in prediction: {e}"
35
 
36
  # Define the Gradio app
37
  with gr.Blocks() as demo: