Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
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 |
-
#
|
12 |
-
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# Function for prediction
|
15 |
def predict(img):
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
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:
|