Update app.py
Browse files
app.py
CHANGED
|
@@ -1,125 +1,123 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import tensorflow as tf
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import numpy as np
|
| 5 |
-
from datetime import datetime
|
| 6 |
-
from PIL import Image
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
print("
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
now
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
#
|
| 69 |
-
with gr.
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
gr.Markdown("
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
gr.
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
gr.
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
if __name__ == "__main__":
|
| 125 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
MODEL_PATH = "best_model.h5"
|
| 9 |
+
class_names = ['layak', 'rusak']
|
| 10 |
+
history_data = []
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
best_model = tf.keras.models.load_model(MODEL_PATH)
|
| 14 |
+
print("β
Model berhasil dimuat.")
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"β Gagal memuat model: {e}")
|
| 17 |
+
# Dummy model untuk mencegah crash saat build jika file belum ada
|
| 18 |
+
best_model = None
|
| 19 |
+
|
| 20 |
+
# --- 2. Fungsi Helper ---
|
| 21 |
+
def get_detailed_info(label, confidence):
|
| 22 |
+
if label == 'layak':
|
| 23 |
+
if confidence > 0.85:
|
| 24 |
+
return "### β
STATUS: SANGAT LAYAK\n**Analisis:** Bangunan dalam kondisi prima. Struktur utama terlihat utuh dan sangat aman untuk dihuni."
|
| 25 |
+
return "### β οΈ STATUS: LAYAK (DENGAN CATATAN)\n**Analisis:** Bangunan aman dihuni, namun ditemukan indikasi kerusakan minor. Disarankan pengecekan rutin pada area retakan."
|
| 26 |
+
else:
|
| 27 |
+
if confidence > 0.85:
|
| 28 |
+
return "### π¨ STATUS: RUSAK PARAH\n**Analisis:** BAHAYA! Ditemukan kerusakan struktur fatal. Segera kosongkan area dan hubungi pihak berwenang."
|
| 29 |
+
return "### π§ STATUS: RUSAK RINGAN\n**Analisis:** Terdeteksi kerusakan fisik pada beberapa bagian. Perlu perbaikan teknis sebelum bangunan dinyatakan aman sepenuhnya."
|
| 30 |
+
|
| 31 |
+
def predict_image(img):
|
| 32 |
+
if best_model is None:
|
| 33 |
+
return {}, "Model belum dimuat."
|
| 34 |
+
|
| 35 |
+
# Preprocessing
|
| 36 |
+
img = img.resize((224, 224))
|
| 37 |
+
img_array = tf.keras.preprocessing.image.img_to_array(img)
|
| 38 |
+
img_array = tf.expand_dims(img_array, 0) / 255.0
|
| 39 |
+
|
| 40 |
+
# Prediksi
|
| 41 |
+
predictions = best_model.predict(img_array)[0]
|
| 42 |
+
result = {class_names[i]: float(predictions[i]) for i in range(len(class_names))}
|
| 43 |
+
|
| 44 |
+
top_label = max(result, key=result.get)
|
| 45 |
+
description = get_detailed_info(top_label, result[top_label])
|
| 46 |
+
return result, description
|
| 47 |
+
|
| 48 |
+
# --- 3. Fungsi Logic Dashboard ---
|
| 49 |
+
def handle_upload(img):
|
| 50 |
+
if img is None: return {}, "_Menunggu foto bangunan..._"
|
| 51 |
+
return predict_image(img)
|
| 52 |
+
|
| 53 |
+
def handle_report(img, location):
|
| 54 |
+
if img is None:
|
| 55 |
+
return {}, pd.DataFrame(history_data, columns=["Waktu", "Status", "Lokasi"]), None, "β Gagal: Foto kosong."
|
| 56 |
+
|
| 57 |
+
output_dict, desc = predict_image(img)
|
| 58 |
+
status = max(output_dict, key=output_dict.get).upper()
|
| 59 |
+
now = datetime.now().strftime("%H:%M | %d-%m-%Y")
|
| 60 |
+
|
| 61 |
+
history_data.insert(0, [now, status, location if location else "Pusat Kota"])
|
| 62 |
+
df = pd.DataFrame(history_data, columns=["Waktu", "Status", "Lokasi"])
|
| 63 |
+
|
| 64 |
+
return output_dict, df, None, "β
Laporan berhasil disimpan ke riwayat!"
|
| 65 |
+
|
| 66 |
+
# --- 4. UI Layout HomeCheck ---
|
| 67 |
+
with gr.Blocks(title="HomeCheck AI") as demo:
|
| 68 |
+
# Header Area
|
| 69 |
+
with gr.Row():
|
| 70 |
+
with gr.Column(scale=8):
|
| 71 |
+
gr.Markdown("# π HomeCheck AI")
|
| 72 |
+
gr.Markdown("### *Sistem Deteksi Kelayakan Bangunan Cerdas*")
|
| 73 |
+
with gr.Column(scale=2):
|
| 74 |
+
gr.Markdown("")
|
| 75 |
+
|
| 76 |
+
gr.Markdown("---")
|
| 77 |
+
|
| 78 |
+
with gr.Tabs():
|
| 79 |
+
with gr.TabItem("π Analisis Baru"):
|
| 80 |
+
with gr.Row():
|
| 81 |
+
# Kolom Kiri: Input
|
| 82 |
+
with gr.Column(variant="panel"):
|
| 83 |
+
gr.Markdown("#### π₯ Input Data")
|
| 84 |
+
input_img = gr.Image(sources=["upload", "webcam"], type="pil", label="Foto Bangunan")
|
| 85 |
+
input_loc = gr.Textbox(
|
| 86 |
+
label="Titik Lokasi",
|
| 87 |
+
placeholder="Contoh: Perumahan Indah Blok A, Medan",
|
| 88 |
+
lines=1
|
| 89 |
+
)
|
| 90 |
+
btn_report = gr.Button("π SIMPAN LAPORAN", variant="primary")
|
| 91 |
+
|
| 92 |
+
# Kolom Kanan: Hasil
|
| 93 |
+
with gr.Column():
|
| 94 |
+
gr.Markdown("#### π Hasil Diagnosa")
|
| 95 |
+
output_label = gr.Label(num_top_classes=2, label="Probabilitas Akurasi")
|
| 96 |
+
|
| 97 |
+
with gr.Group():
|
| 98 |
+
output_description = gr.Markdown(
|
| 99 |
+
"**Instruksi:**\nSilakan ambil atau upload foto bagian bangunan yang ingin diperiksa.",
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
with gr.TabItem("π Riwayat Pemeriksaan"):
|
| 103 |
+
gr.Markdown("#### π Log Laporan Tersimpan")
|
| 104 |
+
output_history = gr.Dataframe(
|
| 105 |
+
headers=["Waktu", "Status", "Lokasi"],
|
| 106 |
+
datatype=["str", "str", "str"],
|
| 107 |
+
interactive=False
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
gr.Markdown("---")
|
| 111 |
+
gr.Markdown("Β© 2026 HomeCheck AI")
|
| 112 |
+
|
| 113 |
+
# --- Interaction Logic ---
|
| 114 |
+
input_img.change(fn=handle_upload, inputs=input_img, outputs=[output_label, output_description])
|
| 115 |
+
|
| 116 |
+
btn_report.click(
|
| 117 |
+
fn=handle_report,
|
| 118 |
+
inputs=[input_img, input_loc],
|
| 119 |
+
outputs=[output_label, output_history, input_img, output_description]
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
| 123 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|