leynessa commited on
Commit
8445a65
·
verified ·
1 Parent(s): 4256e91

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +24 -19
streamlit_app.py CHANGED
@@ -263,27 +263,32 @@ transform = transforms.Compose([
263
 
264
  def predict_butterfly(image, threshold=0.5):
265
  """Predict butterfly species from image"""
266
- if image is None:
267
- return None, None
268
-
269
- if isinstance(image, np.ndarray):
270
- image = Image.fromarray(image)
271
- if image.mode != 'RGB':
272
- image = image.convert('RGB')
273
-
274
- input_tensor = transform(image).unsqueeze(0)
275
 
276
- with torch.no_grad():
277
- output = model(input_tensor)
278
- probabilities = torch.nn.functional.softmax(output[0], dim=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
 
280
- confidence, pred = torch.max(probabilities, 0)
281
- if confidence.item() < threshold:
282
- return None, confidence.item() # Too uncertain
283
-
284
- predicted_class = class_names[pred.item()]
285
-
286
- return predicted_class, confidence.item()
287
 
288
 
289
  except Exception as e:
 
263
 
264
  def predict_butterfly(image, threshold=0.5):
265
  """Predict butterfly species from image"""
266
+ try:
267
+ if image is None:
268
+ return None, None
 
 
 
 
 
 
269
 
270
+ if isinstance(image, np.ndarray):
271
+ image = Image.fromarray(image)
272
+ if image.mode != 'RGB':
273
+ image = image.convert('RGB')
274
+
275
+ input_tensor = transform(image).unsqueeze(0)
276
+
277
+ with torch.no_grad():
278
+ output = model(input_tensor)
279
+ probabilities = torch.nn.functional.softmax(output[0], dim=0)
280
+
281
+ confidence, pred = torch.max(probabilities, 0)
282
+ if confidence.item() < threshold:
283
+ return None, confidence.item()
284
+
285
+ predicted_class = class_names[pred.item()]
286
+ return predicted_class, confidence.item()
287
+
288
+ except Exception as e:
289
+ st.error(f"Prediction error: {str(e)}")
290
+ return None, None
291
 
 
 
 
 
 
 
 
292
 
293
 
294
  except Exception as e: