|
import gradio as gr |
|
import numpy as np |
|
from PIL import Image |
|
from keras.models import load_model |
|
import json |
|
|
|
|
|
model = load_model('brain_tumor_model.h5') |
|
|
|
|
|
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: |
|
|
|
img = image.resize((128, 128)) |
|
img = np.array(img) |
|
|
|
|
|
if img.shape == (128, 128): |
|
img = np.stack((img,) * 3, axis=-1) |
|
|
|
|
|
img = img / 255.0 |
|
img = np.expand_dims(img, axis=0) |
|
|
|
|
|
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)}" |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
iface.launch(share=True, debug=True) |
|
|