Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
|
5 |
+
# Load your model
|
6 |
+
model = tf.keras.models.load_model("model.h5") # Ensure this is the correct path to your model
|
7 |
+
|
8 |
+
def predict(image):
|
9 |
+
# Preprocess the image for prediction
|
10 |
+
image = tf.image.resize(image, [224, 224]) # Change to your model's expected size
|
11 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension
|
12 |
+
predictions = model.predict(image) # Get model predictions
|
13 |
+
|
14 |
+
# Assuming your model outputs probabilities for binary classification
|
15 |
+
# The first output is the probability of class 0 (no tumor),
|
16 |
+
# and the second output is the probability of class 1 (tumor)
|
17 |
+
no_tumor_confidence = predictions[0][0] # Probability of no tumor
|
18 |
+
tumor_confidence = predictions[0][1] # Probability of tumor
|
19 |
+
|
20 |
+
# Create a response with confidence scores
|
21 |
+
if tumor_confidence > no_tumor_confidence:
|
22 |
+
result = {
|
23 |
+
"prediction": "Tumor Detected",
|
24 |
+
"confidence": float(tumor_confidence)
|
25 |
+
}
|
26 |
+
else:
|
27 |
+
result = {
|
28 |
+
"prediction": "No Tumor Detected",
|
29 |
+
"confidence": float(no_tumor_confidence)
|
30 |
+
}
|
31 |
+
|
32 |
+
return result
|
33 |
+
|
34 |
+
# Create a Gradio interface
|
35 |
+
iface = gr.Interface(fn=predict, inputs="image", outputs="json")
|
36 |
+
|
37 |
+
# Launch the Gradio interface
|
38 |
+
iface.launch()
|