File size: 1,341 Bytes
251791b ba5d188 251791b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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()
|