{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":413763,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":337729,"modelId":358696}],"dockerImageVersionId":31041,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"pip install gradio","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-05-27T04:09:58.380696Z","iopub.execute_input":"2025-05-27T04:09:58.380932Z","iopub.status.idle":"2025-05-27T04:10:08.760635Z","shell.execute_reply.started":"2025-05-27T04:09:58.380891Z","shell.execute_reply":"2025-05-27T04:10:08.759838Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import gradio as gr\nimport numpy as np\nimport tensorflow as tf\nfrom PIL import Image\n\n# Cấu hình\ninput_shape = (256, 256, 3)\nnum_classes = 5\nclass_names = ['Level 0 - No DR', 'Level 1 - Mild', 'Level 2 - Moderate', 'Level 3 - Severe', 'Level 4 - Proliferative DR']\n\n# Load mô hình đã lưu\nmodel = tf.keras.models.load_model('/kaggle/input/modeldemo/other/default/1/best_dqn_model.h5', custom_objects={'mse': tf.keras.losses.MeanSquaredError()})\n\ndef predict_retinopathy(image):\n    # Chuyển ảnh PIL sang array, resize và chuẩn hóa\n    img = image.convert('RGB').resize(input_shape[:2])\n    img_array = np.array(img) / 255.0\n    img_array = img_array.reshape((1,) + img_array.shape)\n\n    # Dự đoán Q-values và lấy nhãn cao nhất\n    q_values = model.predict(img_array, verbose=0)[0]\n    pred_class = np.argmax(q_values)\n    confidence = q_values[pred_class]\n\n    return f\"{class_names[pred_class]}\", confidence\n\n# Tạo giao diện Gradio\ninterface = gr.Interface(\n    fn=predict_retinopathy,\n    inputs=gr.Image(type='pil', label=\"Upload Retinal Image\"),\n    outputs=[gr.Textbox(label=\"Prediction\"), gr.Number(label=\"Confidence (Q-value)\")],\n    title=\"Diabetic Retinopathy Detection\",\n    description=\"Upload a retinal fundus image and get diabetic retinopathy diagnosis.\"\n)\n\n\nif __name__ == \"__main__\":\n    interface.launch()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-27T04:12:31.726142Z","iopub.execute_input":"2025-05-27T04:12:31.726797Z","iopub.status.idle":"2025-05-27T04:12:55.106093Z","shell.execute_reply.started":"2025-05-27T04:12:31.726762Z","shell.execute_reply":"2025-05-27T04:12:55.105547Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}