Ferdinann commited on
Commit
3e67b50
·
verified ·
1 Parent(s): 63cc014

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -22
app.py CHANGED
@@ -99,30 +99,34 @@ def get_first_aid_recommendation(wound_class):
99
 
100
  # --- SCRIPT JAVASCRIPT UNTUK GPS ---
101
  get_location_js = """
102
- function getGeoLocation() {
103
- return new Promise((resolve, reject) => {
104
- navigator.geolocation.getCurrentPosition(
105
- (position) => {
106
- resolve(`${position.coords.latitude},${position.coords.longitude}`);
107
- },
108
- (error) => {
109
- resolve("Lokasi tidak diizinkan");
110
- }
111
- );
112
- });
 
113
  }
114
  """
115
 
116
  def predict_image(image_input, location_data):
117
  if best_model is None:
118
- return {}, "⚠️ Model tidak ditemukan.", ""
119
  if image_input is None:
120
  return {}, "Silakan upload gambar.", ""
121
 
122
- # Info Lokasi
123
- info_lokasi = f"📍 **Koordinat Pengunggahan:** {location_data}" if location_data else "📍 Lokasi tidak terdeteksi."
 
 
 
124
 
125
- # Preprocessing & Prediksi
126
  img = image_input.resize(IMG_SIZE)
127
  img_array = tf.keras.utils.img_to_array(img) / 255.0
128
  img_array = tf.expand_dims(img_array, 0)
@@ -142,7 +146,7 @@ def predict_image(image_input, location_data):
142
  THRESHOLD = 0.20
143
  if top_confidence < THRESHOLD:
144
  top_label_id = "Normal (Tidak Terdeteksi)"
145
- rekomendasi_teks = "Model kurang akurat. Pastikan foto jelas."
146
  else:
147
  top_label_id = translation_map.get(top_label_en, top_label_en)
148
  rekomendasi_teks = get_first_aid_recommendation(top_label_en)
@@ -153,31 +157,29 @@ def predict_image(image_input, location_data):
153
  f"--- \n*Tingkat Keyakinan AI: {top_confidence:.2%}*"
154
  )
155
 
156
- return translated_output_dict, formatted_output, info_lokasi
157
 
158
  # --- UI INTERFACE ---
159
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="red", secondary_hue="slate")) as demo:
160
  gr.Markdown("# 🚨 FirstAidLens")
161
 
162
- # Input tersembunyi untuk menampung data dari JS
163
  location_hidden = gr.Textbox(visible=False)
164
 
165
  with gr.Row():
166
  with gr.Column(scale=1):
167
- input_img = gr.Image(sources=["upload", "webcam"], type="pil", label="Ambil Foto Luka")
168
  btn_predict = gr.Button("Analisis Luka & Ambil Lokasi", variant="primary")
169
 
170
  with gr.Column(scale=1):
171
  output_label = gr.Label(num_top_classes=3, label="Hasil Analisis")
172
  output_markdown = gr.Markdown("### Panduan Pertolongan Pertama")
173
- output_location = gr.Markdown("") # Menampilkan lokasi di sini
174
 
175
- # Alur Klik: Jalankan JS dulu untuk isi location_hidden, lalu jalankan predict_image
176
  btn_predict.click(
177
  fn=predict_image,
178
  inputs=[input_img, location_hidden],
179
  outputs=[output_label, output_markdown, output_location],
180
- js=get_location_js
181
  )
182
 
183
  if __name__ == "__main__":
 
99
 
100
  # --- SCRIPT JAVASCRIPT UNTUK GPS ---
101
  get_location_js = """
102
+ async () => {
103
+ if (!navigator.geolocation) {
104
+ return "Browser tidak mendukung GPS";
105
+ }
106
+ try {
107
+ const pos = await new Promise((res, rej) => {
108
+ navigator.geolocation.getCurrentPosition(res, rej, {timeout: 5000});
109
+ });
110
+ return `${pos.coords.latitude}, ${pos.coords.longitude}`;
111
+ } catch (err) {
112
+ return "Lokasi tidak dapat diakses (Izin ditolak/Timeout)";
113
+ }
114
  }
115
  """
116
 
117
  def predict_image(image_input, location_data):
118
  if best_model is None:
119
+ return {}, "⚠️ Model tidak ditemukan.", "Lokasi tidak dicek"
120
  if image_input is None:
121
  return {}, "Silakan upload gambar.", ""
122
 
123
+ # Logika pesan lokasi: Tetap munculkan hasil meskipun lokasi gagal
124
+ if not location_data or "tidak" in location_data.lower():
125
+ status_lokasi = f"⚠️ **{location_data if location_data else 'Lokasi tidak terdeteksi'}**"
126
+ else:
127
+ status_lokasi = f"📍 **Lokasi:** {location_data}"
128
 
129
+ # --- Preprocessing & Prediksi ---
130
  img = image_input.resize(IMG_SIZE)
131
  img_array = tf.keras.utils.img_to_array(img) / 255.0
132
  img_array = tf.expand_dims(img_array, 0)
 
146
  THRESHOLD = 0.20
147
  if top_confidence < THRESHOLD:
148
  top_label_id = "Normal (Tidak Terdeteksi)"
149
+ rekomendasi_teks = "Model kurang akurat. Pastikan foto jelas dan terang."
150
  else:
151
  top_label_id = translation_map.get(top_label_en, top_label_en)
152
  rekomendasi_teks = get_first_aid_recommendation(top_label_en)
 
157
  f"--- \n*Tingkat Keyakinan AI: {top_confidence:.2%}*"
158
  )
159
 
160
+ return translated_output_dict, formatted_output, status_lokasi
161
 
162
  # --- UI INTERFACE ---
163
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="red", secondary_hue="slate")) as demo:
164
  gr.Markdown("# 🚨 FirstAidLens")
165
 
 
166
  location_hidden = gr.Textbox(visible=False)
167
 
168
  with gr.Row():
169
  with gr.Column(scale=1):
170
+ input_img = gr.Image(sources=["upload", "webcam"], type="pil", label="Foto Luka")
171
  btn_predict = gr.Button("Analisis Luka & Ambil Lokasi", variant="primary")
172
 
173
  with gr.Column(scale=1):
174
  output_label = gr.Label(num_top_classes=3, label="Hasil Analisis")
175
  output_markdown = gr.Markdown("### Panduan Pertolongan Pertama")
176
+ output_location = gr.Markdown("")
177
 
 
178
  btn_predict.click(
179
  fn=predict_image,
180
  inputs=[input_img, location_hidden],
181
  outputs=[output_label, output_markdown, output_location],
182
+ js=get_location_js # JS akan mengisi location_hidden sebelum fn dipanggil
183
  )
184
 
185
  if __name__ == "__main__":