Spaces:
Build error
Build error
| import gradio as gr | |
| import numpy as np | |
| import tensorflow as tf | |
| from PIL import Image | |
| # Load the trained model | |
| model = tf.keras.models.load_model("BRAIINTUMORMODEL.h5") | |
| # Define class labels | |
| class_labels = ["Glioma", "Meningioma", "No Tumor", "Pituitary"] | |
| def predict_brain_tumor(img): | |
| # Convert to RGB if needed | |
| if img.mode != 'RGB': | |
| img = img.convert('RGB') | |
| # Preprocess image | |
| img = img.resize((224, 224)) | |
| img_array = np.array(img) / 255.0 | |
| img_array = np.expand_dims(img_array, axis=0) | |
| # Make prediction | |
| prediction = model.predict(img_array)[0] | |
| predicted_class = np.argmax(prediction) | |
| confidence = np.max(prediction) * 100 | |
| # Format output | |
| return f"Predicted Tumor Type: {class_labels[predicted_class]} (Confidence: {confidence:.2f}%)" | |
| # Example images | |
| example_images = [ | |
| ["example_glioma.jpg"], | |
| ["example_meningioma.jpeg"], | |
| ["example_notumor.jpeg"], | |
| ["example_pit.jpg"] | |
| ] | |
| # Create Gradio Interface | |
| iface = gr.Interface( | |
| fn=predict_brain_tumor, | |
| inputs=gr.Image(type="pil"), | |
| outputs=gr.Textbox(label="Prediction Result"), | |
| title="🧠 Brain Tumor Classification", | |
| description="Upload an MRI image to classify the tumor type. You can also click on the example images below.", | |
| examples=example_images | |
| ) | |
| iface.launch(share=True) |