Spaces:
Sleeping
Sleeping
import gradio as gr | |
import numpy as np | |
from PIL import Image | |
from keras.models import load_model | |
import json | |
# Load your pre-trained model | |
model = load_model('brain_tumor_model.h5') | |
# Load examples from JSON file | |
with open('examples.json', 'r') as f: | |
examples_data = json.load(f) | |
examples = [[example['image']] for example in examples_data] | |
def predict_image(image): | |
try: | |
# Resize and preprocess the image | |
img = image.resize((128, 128)) | |
img = np.array(img) | |
# Check and convert grayscale to RGB | |
if img.shape == (128, 128): # Grayscale | |
img = np.stack((img,) * 3, axis=-1) | |
# Normalize the image | |
img = img / 255.0 | |
img = np.expand_dims(img, axis=0) | |
# Make the prediction | |
prediction = model.predict(img) | |
predicted_class = np.argmax(prediction) | |
confidence = np.max(prediction) | |
return f'{"No tumor detected" if predicted_class == 0 else "Tumor detected"}. Confidence: {confidence:.2f}' | |
except Exception as e: | |
return f"Error: {str(e)}" | |
# Create the Gradio interface | |
iface = gr.Interface( | |
fn=predict_image, | |
inputs=gr.Image(type="pil"), | |
outputs=gr.Textbox(), | |
title="Brain Tumor Detection AI App", | |
description="Upload an image to detect brain tumors.", | |
theme="monochrome", | |
flagging_options=["Incorrect Diagnosis", "Image Not Clear", "Model Error"], | |
examples=examples | |
) | |
# Launch the interface | |
iface.launch(share=True, debug=True) | |