| import gradio as gr |
| import tensorflow as tf |
| import numpy as np |
| from PIL import Image |
| import os |
|
|
| |
| |
| model_path = 'best_swin_model.h5' |
| model = None |
|
|
| if not os.path.exists(model_path): |
| print(f"Error: Model file '{model_path}' not found. Please ensure it's in the same directory.") |
| print("Pastikan file model 'best_swin_model.h5' berada di direktori yang sama dengan script ini.") |
| else: |
| try: |
| model = tf.keras.models.load_model(model_path) |
| print("Model 'best_swin_model.h5' berhasil dimuat.") |
| except Exception as e: |
| print(f"Error loading model from '{model_path}': {e}") |
| print(f"Terjadi kesalahan saat memuat model dari '{model_path}': {e}") |
| |
| |
| |
|
|
| |
| w, h = 150, 150 |
|
|
| |
| class_names = { |
| 0: 'glioma_tumor', |
| 1: 'no_tumor', |
| 2: 'meningioma_tumor', |
| 3: 'pituitary_tumor' |
| } |
|
|
| |
| def predict(image): |
| |
| if model is None: |
| return "Error: Model tidak dimuat. Pastikan file model (Skema2.h5) ada dan tidak rusak." \ |
| " (Error: Model not loaded. Please ensure the model file (Skema2.h5) exists and is not corrupted.)" |
|
|
| |
| if image is None: |
| return "Tidak ada gambar yang diunggah. Silakan unggah gambar untuk prediksi." \ |
| " (No image uploaded. Please upload an image for prediction.)" |
|
|
| try: |
| |
| img = image.convert("RGB").resize((w, h)) |
| |
| img_array = np.array(img) / 255.0 |
| |
| img_array = np.expand_dims(img_array, axis=0) |
|
|
| |
| pred = model.predict(img_array) |
| |
| predicted_class_index = np.argmax(pred) |
| |
| return class_names[predicted_class_index] |
| except Exception as e: |
| |
| return f"Terjadi kesalahan saat prediksi: {e}. Pastikan gambar valid dan sesuai." \ |
| f" (An error occurred during prediction: {e}. Please ensure the image is valid and appropriate.)" |
|
|
| |
| with gr.Blocks( |
| |
| css=""" |
| .gr-button { |
| border-radius: 1rem !important; /* Rounded corners for buttons */ |
| background-color: #4CAF50; /* Green background */ |
| color: white; /* White text */ |
| padding: 0.75rem 1.5rem; /* Padding */ |
| font-size: 1.1rem; /* Larger font size */ |
| border: none; /* No border */ |
| cursor: pointer; /* Pointer cursor on hover */ |
| transition: background-color 0.3s ease; /* Smooth transition */ |
| } |
| .gr-button:hover { |
| background-color: #45a049; /* Darker green on hover */ |
| } |
| .gr-image-preview { |
| border-radius: 0.5rem; /* Slightly rounded corners for image preview */ |
| box-shadow: 0 4px 6px rgba(0,0,0,0.1); /* Subtle shadow */ |
| border: 1px solid #ddd; /* Light border */ |
| } |
| .gradio-container { |
| font-family: 'Inter', sans-serif; /* Use Inter font */ |
| max-width: 800px; /* Max width for the container */ |
| margin: auto; /* Center the container */ |
| padding: 2rem; /* Padding around the content */ |
| background-color: #f9f9f9; /* Light background */ |
| border-radius: 1rem; /* Rounded container */ |
| box-shadow: 0 8px 16px rgba(0,0,0,0.15); /* More prominent shadow */ |
| } |
| h2 { |
| color: #2c3e50; /* Darker heading color */ |
| text-align: center; /* Center align heading */ |
| margin-bottom: 1.5rem; /* Space below heading */ |
| } |
| .gr-markdown p { |
| text-align: center; /* Center align instructions */ |
| color: #555; /* Slightly darker text */ |
| margin-bottom: 2rem; /* Space below instructions */ |
| } |
| .gr-textbox { |
| border-radius: 0.5rem; /* Rounded text box */ |
| border: 1px solid #ccc; /* Light border */ |
| padding: 0.75rem; /* Padding inside text box */ |
| font-size: 1rem; /* Font size */ |
| } |
| """ |
| ) as demo: |
| |
| gr.Markdown("## 🧠 Deteksi Tumor Otak") |
| |
| gr.Markdown("Unggah gambar Tumor Otak, lalu klik tombol di bawah untuk mendapatkan prediksinya.") |
|
|
| |
| with gr.Column(): |
| |
| image = gr.Image(label="Unggah Gambar", type="pil") |
| |
| result = gr.Textbox(label="Hasil Prediksi", max_lines=1, interactive=False) |
| |
| predict_btn = gr.Button("Kirim & Prediksi") |
|
|
| |
| |
| predict_btn.click(fn=predict, inputs=image, outputs=result) |
|
|
| |
| |
| |
| demo.launch() |