RimsJ commited on
Commit
a442896
Β·
1 Parent(s): 4e799b3

Fix prediction function error handling and remove auto-trigger

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -84,7 +84,7 @@ def load_model():
84
  raise
85
 
86
 
87
- def predict_image(image: Image.Image) -> Tuple[Dict, str]:
88
  """
89
  Predict batik class from image
90
 
@@ -94,9 +94,14 @@ def predict_image(image: Image.Image) -> Tuple[Dict, str]:
94
  Returns:
95
  Tuple of (top_k_dict, formatted_text)
96
  """
 
 
97
  try:
98
  if image is None:
99
- return {}, "❌ Silakan upload gambar batik terlebih dahulu"
 
 
 
100
 
101
  # Convert to RGB if needed
102
  if image.mode != 'RGB':
@@ -140,7 +145,9 @@ def predict_image(image: Image.Image) -> Tuple[Dict, str]:
140
  return results, result_text
141
 
142
  except Exception as e:
143
- return {}, f"❌ Error: {str(e)}"
 
 
144
 
145
 
146
  # Load model at startup
@@ -197,13 +204,6 @@ with gr.Blocks(
197
  outputs=[output_label, output_text]
198
  )
199
 
200
- # Also trigger on image upload
201
- input_image.change(
202
- fn=predict_image,
203
- inputs=input_image,
204
- outputs=[output_label, output_text]
205
- )
206
-
207
  gr.Markdown("""
208
  ---
209
  ### πŸ“‹ Tentang Model
 
84
  raise
85
 
86
 
87
+ def predict_image(image):
88
  """
89
  Predict batik class from image
90
 
 
94
  Returns:
95
  Tuple of (top_k_dict, formatted_text)
96
  """
97
+ global model, transform, class_names
98
+
99
  try:
100
  if image is None:
101
+ return None, "❌ Silakan upload gambar batik terlebih dahulu"
102
+
103
+ if model is None:
104
+ return None, "❌ Model belum dimuat. Silakan refresh halaman."
105
 
106
  # Convert to RGB if needed
107
  if image.mode != 'RGB':
 
145
  return results, result_text
146
 
147
  except Exception as e:
148
+ import traceback
149
+ traceback.print_exc()
150
+ return None, f"❌ Error: {str(e)}"
151
 
152
 
153
  # Load model at startup
 
204
  outputs=[output_label, output_text]
205
  )
206
 
 
 
 
 
 
 
 
207
  gr.Markdown("""
208
  ---
209
  ### πŸ“‹ Tentang Model