Janish7 commited on
Commit
48280a9
·
verified ·
1 Parent(s): 838736c

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +17 -6
src/streamlit_app.py CHANGED
@@ -49,8 +49,11 @@ def classify_input(model, preprocess, device, input_data, positive_prompts, nega
49
  response = requests.get(input_data, timeout=10)
50
  response.raise_for_status() # Raise an error for bad status codes
51
  image = Image.open(io.BytesIO(response.content))
52
- else: # Uploaded file - this is the key fix
53
- # For uploaded files, we need to read the bytes and convert to PIL Image
 
 
 
54
  if hasattr(input_data, 'read'):
55
  # It's a file-like object (UploadedFile)
56
  image_bytes = input_data.read()
@@ -180,14 +183,19 @@ def main():
180
  help="Upload an image file to classify"
181
  )
182
  if uploaded_file is not None: # More explicit check
183
- input_data = uploaded_file
184
  try:
 
 
 
 
185
  # Display the uploaded image
186
- st.image(uploaded_file, caption=f"Uploaded: {uploaded_file.name}", use_column_width=True)
187
  # Show file details
188
- st.info(f"File size: {len(uploaded_file.getvalue())} bytes")
 
189
  except Exception as e:
190
- st.error(f"Error displaying uploaded image: {e}")
 
191
 
192
  else: # URL
193
  image_url = st.text_input("Enter image URL:", placeholder="https://example.com/image.jpg")
@@ -296,6 +304,9 @@ def main():
296
  st.subheader("Debug Information")
297
  st.write(f"Device: {device}")
298
  st.write(f"Input type: {input_type}")
 
 
 
299
  st.write(f"Input data type: {type(input_data)}")
300
  st.write(f"Positive prompts count: {len(positive_prompts) if positive_prompts else 0}")
301
  st.write(f"Negative prompts count: {len(negative_prompts) if negative_prompts else 0}")
 
49
  response = requests.get(input_data, timeout=10)
50
  response.raise_for_status() # Raise an error for bad status codes
51
  image = Image.open(io.BytesIO(response.content))
52
+ elif isinstance(input_data, bytes): # Uploaded file as bytes
53
+ # Convert bytes directly to PIL Image
54
+ image = Image.open(io.BytesIO(input_data))
55
+ else:
56
+ # Fallback for other formats
57
  if hasattr(input_data, 'read'):
58
  # It's a file-like object (UploadedFile)
59
  image_bytes = input_data.read()
 
183
  help="Upload an image file to classify"
184
  )
185
  if uploaded_file is not None: # More explicit check
 
186
  try:
187
+ # Read the file data once and store it
188
+ file_bytes = uploaded_file.getvalue() # This gets the bytes without moving file pointer
189
+ input_data = file_bytes # Store bytes instead of file object
190
+
191
  # Display the uploaded image
192
+ st.image(file_bytes, caption=f"Uploaded: {uploaded_file.name}", use_column_width=True)
193
  # Show file details
194
+ st.info(f"File size: {len(file_bytes)} bytes")
195
+ st.success("✅ Image uploaded successfully!")
196
  except Exception as e:
197
+ st.error(f"Error processing uploaded image: {e}")
198
+ input_data = None
199
 
200
  else: # URL
201
  image_url = st.text_input("Enter image URL:", placeholder="https://example.com/image.jpg")
 
304
  st.subheader("Debug Information")
305
  st.write(f"Device: {device}")
306
  st.write(f"Input type: {input_type}")
307
+ st.write(f"Input data: {input_data is not None}")
308
+ if input_data and hasattr(input_data, '__len__'):
309
+ st.write(f"Input data length: {len(input_data)}")
310
  st.write(f"Input data type: {type(input_data)}")
311
  st.write(f"Positive prompts count: {len(positive_prompts) if positive_prompts else 0}")
312
  st.write(f"Negative prompts count: {len(negative_prompts) if negative_prompts else 0}")