brain_tumor_det / app.py
pavankm96's picture
Update app.py
ba5d188 verified
import gradio as gr
import numpy as np
import tensorflow as tf
# Load your model
model = tf.keras.models.load_model("model/Brain_tumor_pred.h5") # Ensure this is the correct path to your model
def predict(image):
# Preprocess the image for prediction
image = tf.image.resize(image, [224, 224]) # Change to your model's expected size
image = np.expand_dims(image, axis=0) # Add batch dimension
predictions = model.predict(image) # Get model predictions
# Assuming your model outputs probabilities for binary classification
# The first output is the probability of class 0 (no tumor),
# and the second output is the probability of class 1 (tumor)
no_tumor_confidence = predictions[0][0] # Probability of no tumor
tumor_confidence = predictions[0][1] # Probability of tumor
# Create a response with confidence scores
if tumor_confidence > no_tumor_confidence:
result = {
"prediction": "Tumor Detected",
"confidence": float(tumor_confidence)
}
else:
result = {
"prediction": "No Tumor Detected",
"confidence": float(no_tumor_confidence)
}
return result
# Create a Gradio interface
iface = gr.Interface(fn=predict, inputs="image", outputs="json")
# Launch the Gradio interface
iface.launch()