{ "cells": [ { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", "from PIL import Image\n", "import numpy as np\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "model = tf.keras.models.load_model('./Trained_Model.keras')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "classes = ['glioma_tumor', 'meningioma_tumor', 'no_tumor', 'pituitary_tumor'] \n", "\n", "def preprocess_image(image_path):\n", " img = Image.open(image_path).convert('RGB') \n", " img_array = img.resize((128, 128)) \n", " return np.expand_dims(img_array, axis=0)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def predict_gradio(image):\n", " img_array = preprocess_image(image)\n", " predictions = model.predict(img_array)\n", " predicted_class = np.argmax(predictions, axis=1)[0]\n", " return classes[predicted_class]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7864\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 465ms/step\n" ] } ], "source": [ "interface = gr.Interface(\n", " fn=predict_gradio,\n", " inputs=gr.Image(type=\"filepath\"),\n", " outputs=\"text\",\n", " title=\"Brain Tumor Prediction\",\n", " description=\"Upload an MRI image, and the model will predict the class.\"\n", ")\n", "\n", "interface.launch(server_port=7864)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "anway", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.15" } }, "nbformat": 4, "nbformat_minor": 2 }