Ferdinann commited on
Commit
0009ac5
Β·
verified Β·
1 Parent(s): 9abe915

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +19 -0
  2. app.py +125 -0
  3. best_model.h5 +3 -0
  4. requirements.txt +12 -0
Dockerfile ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Gunakan image Python 3.9 sebagai base
2
+ FROM python:3.9
3
+
4
+ # Set working directory
5
+ WORKDIR /code
6
+
7
+ # Copy requirements dan install dependencies
8
+ COPY ./requirements.txt /code/requirements.txt
9
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
10
+
11
+ # Copy seluruh file ke dalam container
12
+ COPY . /code
13
+
14
+ # Berikan hak akses ke folder cache (penting untuk Hugging Face)
15
+ RUN mkdir -p /code/cache && chmod -R 777 /code/cache
16
+ ENV GRADIO_TEMP_DIR="/code/cache"
17
+
18
+ # Jalankan aplikasi
19
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # --- 1. Konfigurasi & Load Model ---
9
+ # Sesuaikan nama file model dengan yang ada di screenshot Anda
10
+ MODEL_PATH = "best_model"
11
+ class_names = ['layak', 'rusak']
12
+ history_data = []
13
+
14
+ try:
15
+ best_model = tf.keras.models.load_model(MODEL_PATH)
16
+ print("βœ… Model berhasil dimuat.")
17
+ except Exception as e:
18
+ print(f"❌ Gagal memuat model: {e}")
19
+ # Dummy model untuk mencegah crash saat build jika file belum ada
20
+ best_model = None
21
+
22
+ # --- 2. Fungsi Helper ---
23
+ def get_detailed_info(label, confidence):
24
+ if label == 'layak':
25
+ if confidence > 0.85:
26
+ return "### βœ… STATUS: SANGAT LAYAK\n**Analisis:** Bangunan dalam kondisi prima. Struktur utama terlihat utuh dan sangat aman untuk dihuni."
27
+ return "### ⚠️ STATUS: LAYAK (DENGAN CATATAN)\n**Analisis:** Bangunan aman dihuni, namun ditemukan indikasi kerusakan minor. Disarankan pengecekan rutin pada area retakan."
28
+ else:
29
+ if confidence > 0.85:
30
+ return "### 🚨 STATUS: RUSAK PARAH\n**Analisis:** BAHAYA! Ditemukan kerusakan struktur fatal. Segera kosongkan area dan hubungi pihak berwenang."
31
+ return "### 🚧 STATUS: RUSAK RINGAN\n**Analisis:** Terdeteksi kerusakan fisik pada beberapa bagian. Perlu perbaikan teknis sebelum bangunan dinyatakan aman sepenuhnya."
32
+
33
+ def predict_image(img):
34
+ if best_model is None:
35
+ return {}, "Model belum dimuat."
36
+
37
+ # Preprocessing
38
+ img = img.resize((224, 224))
39
+ img_array = tf.keras.preprocessing.image.img_to_array(img)
40
+ img_array = tf.expand_dims(img_array, 0) / 255.0
41
+
42
+ # Prediksi
43
+ predictions = best_model.predict(img_array)[0]
44
+ result = {class_names[i]: float(predictions[i]) for i in range(len(class_names))}
45
+
46
+ top_label = max(result, key=result.get)
47
+ description = get_detailed_info(top_label, result[top_label])
48
+ return result, description
49
+
50
+ # --- 3. Fungsi Logic Dashboard ---
51
+ def handle_upload(img):
52
+ if img is None: return {}, "_Menunggu foto bangunan..._"
53
+ return predict_image(img)
54
+
55
+ def handle_report(img, location):
56
+ if img is None:
57
+ return {}, pd.DataFrame(history_data, columns=["Waktu", "Status", "Lokasi"]), None, "❌ Gagal: Foto kosong."
58
+
59
+ output_dict, desc = predict_image(img)
60
+ status = max(output_dict, key=output_dict.get).upper()
61
+ now = datetime.now().strftime("%H:%M | %d-%m-%Y")
62
+
63
+ history_data.insert(0, [now, status, location if location else "Pusat Kota"])
64
+ df = pd.DataFrame(history_data, columns=["Waktu", "Status", "Lokasi"])
65
+
66
+ return output_dict, df, None, "βœ… Laporan berhasil disimpan ke riwayat!"
67
+
68
+ # --- 4. UI Layout HomeCheck ---
69
+ with gr.Blocks(title="HomeCheck AI") as demo:
70
+ # Header Area
71
+ with gr.Row():
72
+ with gr.Column(scale=8):
73
+ gr.Markdown("# 🏠 HomeCheck AI")
74
+ gr.Markdown("### *Sistem Deteksi Kelayakan Bangunan Cerdas*")
75
+ with gr.Column(scale=2):
76
+ gr.Markdown("![Logo](https://img.icons8.com/fluency/96/home.png)")
77
+
78
+ gr.Markdown("---")
79
+
80
+ with gr.Tabs():
81
+ with gr.TabItem("πŸ” Analisis Baru"):
82
+ with gr.Row():
83
+ # Kolom Kiri: Input
84
+ with gr.Column(variant="panel"):
85
+ gr.Markdown("#### πŸ“₯ Input Data")
86
+ input_img = gr.Image(sources=["upload", "webcam"], type="pil", label="Foto Bangunan")
87
+ input_loc = gr.Textbox(
88
+ label="Titik Lokasi",
89
+ placeholder="Contoh: Perumahan Indah Blok A, Medan",
90
+ lines=1
91
+ )
92
+ btn_report = gr.Button("πŸš€ SIMPAN LAPORAN", variant="primary")
93
+
94
+ # Kolom Kanan: Hasil
95
+ with gr.Column():
96
+ gr.Markdown("#### πŸ“Š Hasil Diagnosa")
97
+ output_label = gr.Label(num_top_classes=2, label="Probabilitas Akurasi")
98
+
99
+ with gr.Group():
100
+ output_description = gr.Markdown(
101
+ "**Instruksi:**\nSilakan ambil atau upload foto bagian bangunan yang ingin diperiksa.",
102
+ )
103
+
104
+ with gr.TabItem("πŸ“œ Riwayat Pemeriksaan"):
105
+ gr.Markdown("#### πŸ“‘ Log Laporan Tersimpan")
106
+ output_history = gr.Dataframe(
107
+ headers=["Waktu", "Status", "Lokasi"],
108
+ datatype=["str", "str", "str"],
109
+ interactive=False
110
+ )
111
+
112
+ gr.Markdown("---")
113
+ gr.Markdown("Β© 2026 HomeCheck AI")
114
+
115
+ # --- Interaction Logic ---
116
+ input_img.change(fn=handle_upload, inputs=input_img, outputs=[output_label, output_description])
117
+
118
+ btn_report.click(
119
+ fn=handle_report,
120
+ inputs=[input_img, input_loc],
121
+ outputs=[output_label, output_history, input_img, output_description]
122
+ )
123
+
124
+ if __name__ == "__main__":
125
+ demo.launch(server_name="0.0.0.0", server_port=7860)
best_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:08c871f17d1d189036893355d29cbed48e7e31ee279a6349c1448c316ddb490b
3
+ size 16673000
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ tensorflow==2.19.0
2
+ keras==3.10.0
3
+ numpy==2.0.2
4
+ matplotlib==3.10.0
5
+ seaborn==0.13.2
6
+ gradio
7
+ tensorflow-hub==0.16.1
8
+ kagglehub==0.3.13
9
+ scikit-learn==1.6.1
10
+ tensorflowjs==4.22.0
11
+ Pillow==12.1.0
12
+ pandas==2.2.2