leynessa commited on
Commit
605629e
·
verified ·
1 Parent(s): 6052c4b

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +25 -16
streamlit_app.py CHANGED
@@ -3,7 +3,8 @@ from PIL import Image
3
  import torch
4
  from torchvision import models, transforms
5
  import json
6
- import os # Add this import
 
7
 
8
  # Configure Streamlit
9
  st.set_page_config(
@@ -27,12 +28,10 @@ except:
27
  def load_model():
28
  MODEL_PATH = "butterfly_classifier.pth"
29
 
30
- # Check if model exists locally
31
  if not os.path.exists(MODEL_PATH):
32
  st.error("Model file not found. Please upload butterfly_classifier.pth to your space.")
33
  return None
34
 
35
- # Load the model
36
  model = models.resnet18(pretrained=False)
37
  model.fc = torch.nn.Linear(model.fc.in_features, len(class_names))
38
  model.load_state_dict(torch.load(MODEL_PATH, map_location="cpu"))
@@ -41,7 +40,6 @@ def load_model():
41
 
42
  model = load_model()
43
 
44
- # Add a check to ensure model loaded successfully
45
  if model is None:
46
  st.stop()
47
 
@@ -53,16 +51,23 @@ transform = transforms.Compose([
53
  st.title("🦋 Butterfly Identifier")
54
  st.write("Upload a butterfly image and I'll tell you what species it is!")
55
 
56
- # Use a different file uploader approach
57
- uploaded_file = st.file_uploader(
58
- "Choose an image...",
59
- type=["jpg", "jpeg", "png"],
60
- help="Upload a clear photo of a butterfly"
61
- )
62
-
63
- if uploaded_file is not None:
64
- try:
65
- image = Image.open(uploaded_file).convert("RGB")
 
 
 
 
 
 
 
66
  st.image(image, caption="Uploaded Image", use_column_width=True)
67
 
68
  # Preprocess
@@ -78,6 +83,10 @@ if uploaded_file is not None:
78
 
79
  if predicted_class in butterfly_info:
80
  st.info(butterfly_info[predicted_class]["description"])
 
 
 
81
 
82
- except Exception as e:
83
- st.error(f"Error processing image: {str(e)}")
 
 
3
  import torch
4
  from torchvision import models, transforms
5
  import json
6
+ import os
7
+ import tempfile
8
 
9
  # Configure Streamlit
10
  st.set_page_config(
 
28
  def load_model():
29
  MODEL_PATH = "butterfly_classifier.pth"
30
 
 
31
  if not os.path.exists(MODEL_PATH):
32
  st.error("Model file not found. Please upload butterfly_classifier.pth to your space.")
33
  return None
34
 
 
35
  model = models.resnet18(pretrained=False)
36
  model.fc = torch.nn.Linear(model.fc.in_features, len(class_names))
37
  model.load_state_dict(torch.load(MODEL_PATH, map_location="cpu"))
 
40
 
41
  model = load_model()
42
 
 
43
  if model is None:
44
  st.stop()
45
 
 
51
  st.title("🦋 Butterfly Identifier")
52
  st.write("Upload a butterfly image and I'll tell you what species it is!")
53
 
54
+ # Alternative file upload with better error handling
55
+ try:
56
+ uploaded_file = st.file_uploader(
57
+ "Choose an image...",
58
+ type=["jpg", "jpeg", "png"],
59
+ help="Upload a clear photo of a butterfly",
60
+ key="butterfly_image"
61
+ )
62
+
63
+ if uploaded_file is not None:
64
+ # Save uploaded file to temporary location
65
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
66
+ tmp_file.write(uploaded_file.read())
67
+ tmp_file_path = tmp_file.name
68
+
69
+ # Load image from temporary file
70
+ image = Image.open(tmp_file_path).convert("RGB")
71
  st.image(image, caption="Uploaded Image", use_column_width=True)
72
 
73
  # Preprocess
 
83
 
84
  if predicted_class in butterfly_info:
85
  st.info(butterfly_info[predicted_class]["description"])
86
+
87
+ # Clean up temporary file
88
+ os.unlink(tmp_file_path)
89
 
90
+ except Exception as e:
91
+ st.error(f"Error with file upload: {str(e)}")
92
+ st.info("If you continue to see this error, try refreshing the page or using a different browser.")